eri*_*ja 5 python python-2.7 python-3.x
我想从字典中获取唯一值。
输入:
{320: [167], 316: [0], 319: [167], 401: [167], 319: [168], 380: [167], 265: [166]}
Run Code Online (Sandbox Code Playgroud)
所需输出:
[167,0,168,166]
Run Code Online (Sandbox Code Playgroud)
我的代码:
unique_values = sorted(set(pff_dict.itervalues()))
Run Code Online (Sandbox Code Playgroud)
但是我得到这个错误: TypeError: unhashable type: 'list'
目前尚不清楚为什么将单项列表映射为值,但您可以使用列表理解来提取元素。
foobar = {320: [167], 316: [0], 319: [167], 401: [167], 319: [168], 380: [167], 265: [166]}
print list(set([x[0] for x in foobar.values()]))
Run Code Online (Sandbox Code Playgroud)
如果您从直接映射到值开始,代码会简单得多。
foobar = {320: 167, 316: 0, 319: 167, 401: 167, 319: 168, 380: 167, 265: 166}
print list(set(foobar.values()))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9605 次 |
| 最近记录: |