我试图将字典中的值添加到新列表中,但没有任何花哨的地方,包括value方法。我能够通过for循环中的newList.append(item)将密钥添加到新列表中。但是我正在尝试将值添加到单独的列表中。到目前为止,这是我正在解决的问题以及我的代码:
如果您还记得,.items()字典方法会生成一个元组序列。牢记这一点,我们为您提供了一个名为pokemon的字典。对于每个键值对,将键附加到列表p_names,然后将值附加到列表p_number。不要使用.keys()或.values()方法。
pokemon = {'Rattata': 19, 'Machop': 66, 'Seel': 86, 'Volbeat': 86, 'Solrock': 126}
p_names = []
p_number = []
for item in pokemon:
p_names.append(item)
You need to use
items
that returns both the key and the value.As the question prompt says you need to use
.items
method which returns the key-value pair as a tuple: