【发布时间】:2020-03-27 04:12:04
【问题描述】:
我在 python 中有一个列表字典:
content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
我想把它变成一个唯一值列表
[58,64,80,130]
我写了一个手动解决方案,但它是一个手动解决方案。我知道有更简洁和更优雅的方式来使用列表推导、 map/reduce 、 itertools 等。有人知道吗?
content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
result = set({})
for k in content.keys() :
for i in content[k]:
result.add(i)
# and list/sort/print just to compare the output
r2 = list( result )
r2.sort()
print r2
【问题讨论】:
-
3分钟内有4个很好的答案,都证明我需要阅读更多的标准库文档。
-
Chuckles 这种感觉永远不会消失 - 标准库中包含 很多 电池。
标签: python