【发布时间】:2020-08-13 14:07:22
【问题描述】:
这是我抓取字典的尝试:
pairs = {'EMOTIONS': ['happy', 'angry', 'sad', 'calm'], 'TRAITS': ['impatient', 'persistent', 'meek']}
然后把它变成一个字典,它的值被写成一个短语,所以:
pairs = {'EMOTIONS': ['I am happy', 'I am angry', 'I am sad', 'I am calm'], 'TRAITS': ['I am impatient', 'I am persistent', 'I am stubborn']}
这是目前为止的代码:
pairs = {'EMOTIONS': ['happy', 'angry', 'sad', 'calm'], 'TRAITS': ['impatient', 'persistent', 'meek']}
I_am = 'I am '
for title, words in pairs.items():
words = [I_am+word for word in words]
我显然做错了,因为当我请求 print(pairs) 时,它返回的字典与我开始时完全相同。我要进行哪些更改才能完成这项工作?
【问题讨论】:
标签: python list loops dictionary list-comprehension