Python 字典理解 - 在创建期间访问字典
Python dict comprehension - access dict during creation
我有这样的代码:
dictionary = {}
for x in dict_of_x:
if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5:
key = x['key']
value = x['value']
if key not in dictionary or value > dictionary[key]:
dictionary[key] = value
我想让这段代码成为字典理解:
dictionary = {x['key'] : x['value'] for x in dict_of_x \
if ( True if x['duration_to_cancel'] == None else x['duration_to_cancel'] > 5)}
如何将第一个代码框中的第二个 if 语句包含到字典理解中?
试试这个,它应该可以工作,因为较高的值会覆盖较低的值:
dictionary = {x['key'] : x['value'] for x in sorted(dict_of_x, key=lambda x: x['value']) \
if ( True if x['duration_to_cancel'] == None else x['duration_to_cancel'] > 5)}
因为你所做的只是 max
你可以使用 "max comprehension":
dict_of_x = (
{'key':0,'value':0,'duration_to_cancel':10},
{'key':0,'value':1,'duration_to_cancel':15},
{'key':0,'value':2,'duration_to_cancel':1},
{'key':1,'value':3,'duration_to_cancel':2},
{'key':2,'value':4,'duration_to_cancel':None}
)
def yours():
dictionary = {}
for x in dict_of_x:
if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5:
key = x['key']
value = x['value']
if key not in dictionary or value > dictionary[key]:
dictionary[key] = value
return dictionary
print yours()
{0: 1, 2: 4}
def mine():
dictionary = {key : max(x['value'] for x in dict_of_x if x['key'] == key) \
for key in set(x['key'] for x in dict_of_x if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5)}
return dictionary
print mine()
{0: 1, 2: 4}
但是请注意,list/dict/set/max 仅仅为了阅读而理解并不一定是一件好事。
同样在你的实现中你只通过你的字典一次,而我的代码会循环多次,这也会降低速度:
%timeit yours()
1000000 loops, best of 3: 855 ns per loop
%timeit mine()
100000 loops, best of 3: 2.32 µs per loop
我有这样的代码:
dictionary = {}
for x in dict_of_x:
if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5:
key = x['key']
value = x['value']
if key not in dictionary or value > dictionary[key]:
dictionary[key] = value
我想让这段代码成为字典理解:
dictionary = {x['key'] : x['value'] for x in dict_of_x \
if ( True if x['duration_to_cancel'] == None else x['duration_to_cancel'] > 5)}
如何将第一个代码框中的第二个 if 语句包含到字典理解中?
试试这个,它应该可以工作,因为较高的值会覆盖较低的值:
dictionary = {x['key'] : x['value'] for x in sorted(dict_of_x, key=lambda x: x['value']) \
if ( True if x['duration_to_cancel'] == None else x['duration_to_cancel'] > 5)}
因为你所做的只是 max
你可以使用 "max comprehension":
dict_of_x = (
{'key':0,'value':0,'duration_to_cancel':10},
{'key':0,'value':1,'duration_to_cancel':15},
{'key':0,'value':2,'duration_to_cancel':1},
{'key':1,'value':3,'duration_to_cancel':2},
{'key':2,'value':4,'duration_to_cancel':None}
)
def yours():
dictionary = {}
for x in dict_of_x:
if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5:
key = x['key']
value = x['value']
if key not in dictionary or value > dictionary[key]:
dictionary[key] = value
return dictionary
print yours()
{0: 1, 2: 4}
def mine():
dictionary = {key : max(x['value'] for x in dict_of_x if x['key'] == key) \
for key in set(x['key'] for x in dict_of_x if x['duration_to_cancel'] == None or x['duration_to_cancel'] > 5)}
return dictionary
print mine()
{0: 1, 2: 4}
但是请注意,list/dict/set/max 仅仅为了阅读而理解并不一定是一件好事。 同样在你的实现中你只通过你的字典一次,而我的代码会循环多次,这也会降低速度:
%timeit yours()
1000000 loops, best of 3: 855 ns per loop
%timeit mine()
100000 loops, best of 3: 2.32 µs per loop