Python 3.x 保持字典值的长度大于5

Python 3.x 保持字典值的长度大于5,python-3.x,string,list,loops,dictionary,Python 3.x,String,List,Loops,Dictionary,我有一个dictd,我只想保留长度为5或更大的元素。所以我希望我的输出是 d = {'key1':['hello', 'ab', 'a', 'morning', 'sunset', 'metaphysics'], 'key2':['hi', 'morning', 'sunset', 'metaphysics'], 'key3':['hey', 'sunset', 'metaphysics']} 我看了这里,但这确实很有帮助 有人能帮忙吗 d = {'key1':['hel

我有一个dict
d
,我只想保留长度为5或更大的元素。所以我希望我的输出是

d = {'key1':['hello', 'ab', 'a', 'morning', 'sunset', 'metaphysics'],
     'key2':['hi', 'morning', 'sunset', 'metaphysics'], 
     'key3':['hey', 'sunset', 'metaphysics']}
我看了这里,但这确实很有帮助

有人能帮忙吗

d = {'key1':['hello', 'morning', 'sunset', 'metaphysics'],
     'key2':['morning', 'sunset', 'metaphysics'], 
     'key3':['sunset', 'metaphysics']}
{'key1':['hello','morning','sunset','smothetics'],'key2':['morning','sunset','smothetics'],'key3':['sunset','smothetics']}


{'key1':['hello','morning','sunset','smothetics','key2':['morning','sunset','smothetics','key3':['sunset','smothetics']}

仅使用理解

d = {'key1':['hello', 'ab', 'a', 'morning', 'sunset', 'metaphysics'],
     'key2':['hi', 'morning', 'sunset', 'metaphysics'],
     'key3':['hey', 'sunset', 'metaphysics']}

for key, val in d.items():
    d[key] = [v for v in val if len(v) > 4]

print (d)

{'key1':['hello','morning','sunset','smothetics','key2':['morning','sunset','smothetics','key3':['sunset','smothetics']}

仅使用理解

d = {'key1':['hello', 'ab', 'a', 'morning', 'sunset', 'metaphysics'],
     'key2':['hi', 'morning', 'sunset', 'metaphysics'],
     'key3':['hey', 'sunset', 'metaphysics']}

for key, val in d.items():
    d[key] = [v for v in val if len(v) > 4]

print (d)

{'key1':['hello'、'morning'、'sunset'、'smothetics']、'key2':['morning'、'sunset'、'smothetics']、'key3':['sunset'、'smothetics']、'key3':['sunset'、'smothetics']}

这与其他答案有何不同?输出是一样的,唯一的是它使用理解。这与其他答案有何不同?输出是一样的,唯一的是它使用理解。