Python 仅使用列表理解构建列表,不使用函数
给定 获取所有乐队的列表:Python 仅使用列表理解构建列表,不使用函数,python,list-comprehension,Python,List Comprehension,给定 获取所有乐队的列表: userplays = { "Alice" : { "AC/DC" : 2, "The Raconteurs" : 3, "Mogwai" : 1 }, "Bob" : { "The XX" : 4, "Lady
userplays = { "Alice" : { "AC/DC" : 2,
"The Raconteurs" : 3,
"Mogwai" : 1
},
"Bob" : { "The XX" : 4,
"Lady Gaga" : 3,
"Mogwai" : 1,
"The Raconteurs" : 1
},
"Charlie" : { "AC/DC" : 7,
"Lady Gaga" : 7
}
}
我能行
['Lady Gaga', 'Mogwai', 'AC/DC', 'The Raconteurs', 'The XX']
如果展平
来自哪里,但不使用展平
,是否可以只使用列表/目录理解?这样做:
list(set(flatten([ [ band
for band
in playcounts.keys() ]
for playcounts
in userplays.values() ] ) ) )
产生:
set(b for v in userplays.values() for b in v.keys())
另一种方法是使用dict理解(Python 2.7+): 产生:
{k:v for v in userplays.values() for k in v.keys()}.keys()
至少在Python 3.3中,这也是更快的:
['Lady Gaga', 'Mogwai', 'AC/DC', 'The Raconteurs', 'The XX']
输出:
import timeit
userplays = { "Alice" : { "AC/DC" : 2,
"The Raconteurs" : 3,
"Mogwai" : 1
},
"Bob" : { "The XX" : 4,
"Lady Gaga" : 3,
"Mogwai" : 1,
"The Raconteurs" : 1
},
"Charlie" : { "AC/DC" : 7,
"Lady Gaga" : 7
}
}
def f1():
set(b for v in userplays.values() for b in v.keys())
def f2():
{k:v for v in userplays.values() for k in v.keys()}.keys()
t1=timeit.Timer(f1).timeit(10000)
t2=timeit.Timer(f2).timeit(10000)
faster=abs(t1-t2) / max(t1,t2)
print("""
set: {:.4} seconds
dict: {:.4} seconds
faster of those is {:.4%} faster
""".format(t1,t2,faster))
编辑
纯粹出于好奇,我比较了在一行程序中实现这一点的各种方法
结果如下:
set: 0.02448 seconds
dict: 0.01988 seconds
faster of those is 18.7907% faster
你可以看到集合理解最快,其次是听写理解
以下是生成Perl风格基准的代码:
f1: set from a generator expression
f2: keys from a dict comprehension
f3: set comprehension
f4: set from a list comprehension
rate/s f4 f1 f2 f3
f4 358,650 0.0% -13.4% -31.7% -41.3%
f1 414,246 15.5% 0.0% -21.1% -32.2%
f2 525,230 46.4% 26.8% 0.0% -14.1%
f3 611,158 70.4% 47.5% 16.4% 0.0%
为什么?这似乎是一个相当武断的要求。因为我一直在思考,找不到答案。这让我感到困惑。但如果你只是想忽略这些值并将其视为一组键,为什么要生成一个键值映射呢?这个想法与Ned的想法相同,但我不确定它是否一定具有相同的运行时行为:Ned可能会创建整个列表,然后使用
set()
,对其进行过滤,虽然此解决方案将项目逐个添加到dict中,因此它们可能会立即被过滤,而不会出现包含重复项的大型中间列表。@FelixDombek否,Ned是一个生成器表达式(不是列表理解),然后将其传递给集
,因此它不会构建整个列表。可能是发电机的表达机制比听写理解慢。@carrot top足够公平了。在我看来,dict压缩是该语言的一个奇妙的补充,也是构建词典的一种美丽方式。但在我看来,建立一本词典而忽视它的价值观是建立一套词典的丑陋方式。如果它恰好更快,那么有理由这样做(如果这是一个性能瓶颈,并且程序不够快)。@FelixDombek:你也可以使用一个集合理解:{b for v in userplays.values()for b in v.keys()}
如果像Ben(和我)一样怀疑发电机正在使内德减速。
f1: set from a generator expression
f2: keys from a dict comprehension
f3: set comprehension
f4: set from a list comprehension
rate/s f4 f1 f2 f3
f4 358,650 0.0% -13.4% -31.7% -41.3%
f1 414,246 15.5% 0.0% -21.1% -32.2%
f2 525,230 46.4% 26.8% 0.0% -14.1%
f3 611,158 70.4% 47.5% 16.4% 0.0%
import timeit
import locale
locale.setlocale(locale.LC_NUMERIC, "")
userplays = { "Alice" : { "AC/DC" : 2,
"The Raconteurs" : 3,
"Mogwai" : 1
},
"Bob" : { "The XX" : 4,
"Lady Gaga" : 3,
"Mogwai" : 1,
"The Raconteurs" : 1
},
"Charlie" : { "AC/DC" : 7,
"Lady Gaga" : 7
}
}
def f1():
"""set from a generator expression"""
set(b for v in userplays.values() for b in v.keys())
def f2():
"""keys from a dict comprehension"""
{k:v for v in userplays.values() for k in v.keys()}.keys()
def f3():
"""set comprehension"""
{b for v in userplays.values() for b in v.keys()}
def f4():
"""set from a list comprehension"""
set([b for v in userplays.values() for b in v.keys()])
def test_table(funcs, c):
results={k.__name__:timeit.Timer(k).timeit(c) for k in funcs}
fastest=sorted(results,key=results.get, reverse=True)
table=[]
table.append([' ','rate/s']+fastest)
for e in fastest:
temp=[]
temp.append(e)
temp.append(int(round(float(c)/results[e])))
t2=['{:.1%}'.format((results[x]-results[e])/results[e]) for x in fastest]
table.append(temp+t2)
print()
for e in funcs:
print('{}: {}'.format(e.__name__, e.__doc__))
print()
pprint_table(table)
def format_num(num):
"""Format a number according to given places.
Adds commas, etc. Will truncate floats into ints!"""
try:
inum = int(num)
return locale.format("%.*f", (0, inum), True)
except (ValueError, TypeError):
return str(num)
def get_max_width(table, index):
"""Get the maximum width of the given column index"""
return max([len(format_num(row[index])) for row in table])
def pprint_table(table):
col_paddings = []
for i in range(len(table[0])):
col_paddings.append(get_max_width(table, i))
for row in table:
# left col
print(row[0].ljust(col_paddings[0] + 1),end=' ')
# rest of the cols
for i in range(1, len(row)):
col = format_num(row[i]).rjust(col_paddings[i] + 2)
print (col,end=' ')
print()
test_table([f1,f2,f3,f4],100000)