Python-使用列表理解简化代码

Python-使用列表理解简化代码,python,for-loop,list-comprehension,Python,For Loop,List Comprehension,给定播放列表: 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'energ

给定播放列表:

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'}]

我可以通过以下方式找到匹配的轨迹:

for d1 in energy_playlist:
    for d2 in tempo_playlist:
        if d1['track'] == d2['track']:
            print (d2['track'])
我如何在一行中完成列表理解,分配给可变的最终播放列表?

这有帮助吗

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)
实际上,这个一行程序可能比上面的三行程序快一点,因为轨迹列表并没有显式地保存在内存中,而是作为集合对象使用的迭代器

给你:

[x['track'] for x in tempo_playlist if x['track'] in [y['track'] for y in energy_playlist]]

尽管我同意其他人的看法,但一行代码的可读性不如多行代码。

将整个代码转换为列表理解

final_playlist = [d2['track'] for d1 in energy_playlist for d2 in tempo_playlist if d1['track'] == d2['track']]

除此之外,您的代码更具可读性,因此不需要一行代码,但最重要的是,一行代码不能真正提高性能:。。。让我示范一下:

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

你不能在列表中打印理解,我知道,但我可以把它分配给一个变量并打印出来,对吧?你可以把理解分配给一个变量,是的。你试过写这个了吗?@cricket_007是的,我试过了。V我想写一个单子。好的,对不起,这就是我在下午1:00的时候写的:-D。我现在不知道如何把它压缩成一个理解。祝你好运。现在是否会为节奏播放列表中的每个x执行能量播放列表中y的[y['track']?这将是缓慢的,虽然很明显,你得到它在一个理解。我相信我的要快得多。。但我可能错了。我知道,我正在努力训练我的可读性,而学习一行一行地阅读会在大脑中产生一个不同的突触-D
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