给出播放列表:
energy_playlist = [{u'track': u'Nude', u'feature': u'energy', u'value': 0.342, u'artist': u'Radiohead'}, {u'track': u'Kaleidoscope', u'feature': u'energy', u'value': 0.285, u'artist': u'Coldplay'}, {u'track': u'Faust Arp', u'feature': u'energy', u'value': 0.289, u'artist': u'Radiohead'}, {u'track': u'Running Up That Hill', u'feature': u'energy', u'value': 0.356, u'artist': u'Placebo'}, {u'track': u'Codex', u'feature': u'energy', u'value': 0.128, u'artist': u'Radiohead'}, {u'track': u'True Love Waits', u'feature': u'energy', u'value': 0.132, u'artist': u'Radiohead'}, {u'track': u'Asleep - 2011 Remastered Version', u'feature': u'energy', u'value': 0.255, u'artist': u'The Smiths'}, {u'track': u'Glass Eyes', u'feature': u'energy', u'value': 0.11, u'artist': u'Radiohead'}, {u'track': u'Pyramid Song', u'feature': u'energy', u'value': 0.335, u'artist': u'Radiohead'}, {u'track': u'Life On Mars? - 2015 Remastered Version', u'feature': u'energy', u'value': 0.384, u'artist': u'David Bowie'}, {u'track': u'Exit Music (For a Film)', u'feature': u'energy', u'value': 0.276, u'artist': u'Radiohead'}, {u'track': u'Videotape', u'feature': u'energy', u'value': 0.384, u'artist': u'Radiohead'}, {u'track': u'High And Dry', u'feature': u'energy', u'value': 0.383, u'artist': u'Radiohead'}]
和
tempo_playlist = [{u'track': u'Codex', u'feature': u'tempo', u'value': 58.993, u'artist': u'Radiohead'}, {u'track': u'Pyramid Song', u'feature': u'tempo', u'value': 77.078, u'artist': u'Radiohead'}, {u'track': u'Videotape', u'feature': u'tempo', u'value': 77.412, u'artist': u'Radiohead'}]
我可以通过这种方式找到匹配的tracks
:
for d1 in energy_playlist:
for d2 in tempo_playlist:
if d1['track'] == d2['track']:
print (d2['track'])
我如何设法对{{1}}分配给list comprehension
variable
的一行final_playlist
做同样的事情?
答案 0 :(得分:2)
这有帮助吗?
energy_tracks = [p["track"] for p in energy_playlist]
tempo_tracks = [p["track"] for p in tempo_playlist]
print set(energy_tracks).intersection(tempo_tracks)
好的,在一行中,你现在想要它:-D?我会问为什么,但只是为了好玩...
result = set(p["track"] for p in energy_playlist).intersection(p["track"] for p in tempo_playlist)
实际上,这个单线程可能比上面的三线程要快一些,因为轨道列表没有明确保存在内存中,而是作为设置对象使用的迭代器。
答案 1 :(得分:2)
你走了:
[x['track'] for x in tempo_playlist if x['track'] in [y['track'] for y in energy_playlist]]
虽然我同意其他人在一行中填写的可读性低于多行。
答案 2 :(得分:2)
将您的整个代码转换为列表理解
final_playlist = [d2['track'] for d1 in energy_playlist for d2 in tempo_playlist if d1['track'] == d2['track']]
答案 3 :(得分:1)
除了a)你的代码更具可读性,所以不要看到一行的需要,但最重要的是b)一个衬里不能真正改善性能:)...让我演示:
import timeit
energy_playlist = [{u'track': u'Nude', u'feature': u'energy', u'value': 0.342, u'artist': u'Radiohead'}, {u'track': u'Kaleidoscope', u'feature': u'energy', u'value': 0.285, u'artist': u'Coldplay'}, {u'track': u'Faust Arp', u'feature': u'energy', u'value': 0.289, u'artist': u'Radiohead'}, {u'track': u'Running Up That Hill', u'feature': u'energy', u'value': 0.356, u'artist': u'Placebo'}, {u'track': u'Codex', u'feature': u'energy', u'value': 0.128, u'artist': u'Radiohead'}, {u'track': u'True Love Waits', u'feature': u'energy', u'value': 0.132, u'artist': u'Radiohead'}, {u'track': u'Asleep - 2011 Remastered Version', u'feature': u'energy', u'value': 0.255, u'artist': u'The Smiths'}, {u'track': u'Glass Eyes', u'feature': u'energy', u'value': 0.11, u'artist': u'Radiohead'}, {u'track': u'Pyramid Song', u'feature': u'energy', u'value': 0.335, u'artist': u'Radiohead'}, {u'track': u'Life On Mars? - 2015 Remastered Version', u'feature': u'energy', u'value': 0.384, u'artist': u'David Bowie'}, {u'track': u'Exit Music (For a Film)', u'feature': u'energy', u'value': 0.276, u'artist': u'Radiohead'}, {u'track': u'Videotape', u'feature': u'energy', u'value': 0.384, u'artist': u'Radiohead'}, {u'track': u'High And Dry', u'feature': u'energy', u'value': 0.383, u'artist': u'Radiohead'}]
tempo_playlist = [{u'track': u'Codex', u'feature': u'tempo', u'value': 58.993, u'artist': u'Radiohead'}, {u'track': u'Pyramid Song', u'feature': u'tempo', u'value': 77.078, u'artist': u'Radiohead'}, {u'track': u'Videotape', u'feature': u'tempo', u'value': 77.412, u'artist': u'Radiohead'}]
def foo1():
ret = []
for d1 in energy_playlist:
for d2 in tempo_playlist:
if d1['track'] == d2['track']:
ret.append(d1["track"])
return ret
def foo2():
ret = []
for d1 in energy_playlist:
for d2 in tempo_playlist:
if d1['track'] == d2['track']:
pass
return None
def foo3():
return [d2['track'] for d1 in energy_playlist for d2 in tempo_playlist if d1['track'] == d2['track']]
def bar():
tempo_tracks = [i["track"] for i in tempo_playlist]
return [i["track"] for i in energy_playlist if i["track"] in tempo_tracks]
print("foo1:", timeit.timeit(foo1))
print("foo2:", timeit.timeit(foo2))
print("foo3:", timeit.timeit(foo3))
print("bar:", timeit.timeit(bar))
# foo1: 5.550314342981437
# foo2: 5.025758317991858
# foo3: 5.3763819159939885
# bar: 2.86007208598312