python - 将字典转换为元组列表
问题描述
我是 Python 的初学者,我正在尝试解决中文邮递员问题并遵循本教程。
我在一步:计算最小重量匹配。我需要将字典转换为元组列表
下面的代码是我到目前为止所拥有的
# Compute min weight matching.
# Note: max_weight_matching uses the 'weight' attribute by default as the attribute to maximize.
odd_matching_dupes = nx.algorithms.max_weight_matching(g_odd_complete, True)
print('Number of edges in matching: {}'.format(len(odd_matching_dupes)))
# Preview of matching with dupes
odd_matching_dupes
# Convert matching to list of deduped tuples
odd_matching = list(pd.unique([tuple(sorted([k, v])) for k, v in
odd_matching_dupes.items()]))
我得到的输出odd_matching_dupes
是:
{('rep1', 'rep2'),
('rep12', 'rep16'),
('rep17', 'rep13'),
('rep27', 'rep19'),
('rep7', 'rep10'),
('rep8', 'rep5')}
这是我收到的错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-84-7da9b4bbe8a5> in <module>
142
143 # Convert matching to list of deduped tuples
--> 144 odd_matching = list(pd.unique([tuple(sorted([k, v])) for k, v in
odd_matching_dupes.items()]))
AttributeError: 'set' object has no attribute 'items'
解决方案
推荐阅读
- r - 有没有办法将矩阵的大多数列绘制为一种颜色,但指定几列以独特的颜色绘制?
- flutter - 如何每 15 天运行一次函数?
- c++builder-6 - 如何在 C++Builder 6 中制作适用于任何计算机的应用程序?
- android - 尝试在android中创建文件夹并保存mp3文件失败
- html - 如何为所需的消息输入和文本区域设置样式
- c - 您将如何更改代码以使子级和父级同时运行?
- linq - 如何在 Uipath 中的 DataTable 上使用 LINQ
- python - 我正在尝试从组合框中获取当前选定的项目(键)
- c++ - Qt SQL Server - 散列密码
- rust - 生锈的特征。是否习惯于在依赖库中定义它们,或者您可以将 Traits 添加到使用它们的二进制代码中的类型中?