在 Python 中,您可以将列表中的元素添加到字典的值中吗?
In Python can you add elements from a list to the values of a dictionary?
我正在尝试将列表列表的元素添加到字典的值中。
我创建了一个列表,其中包含如下文件中的元素:
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]....]
我正在尝试将此列表添加到我制作的字典中:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]....]
我试过以下代码:
x = 1
for x in d2.values():
d2.append(list_of_list)
print(d2)
我什至不确定这是可能的,但我正在努力使字典成为:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011, [966]], 'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]....]
我怎样才能做到这一点?
我知道有更多方法可以做到这一点,但我认为这是更易读和理解的代码。
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]]
dict_ = {'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]}
i = 0
# list(dict_.items())[1:] is the list of all keys and values except first one.
for key,value in list(dict_.items())[1:]:
dict_[key] = value+[list_of_lists[i]]
i+=1
print(dict_)
您可以使用 itertools.islice()
跳过第一个元素,然后使用 zip()
将每个列表值与要追加的列表配对:
from itertools import islice
for lst_value, lst_to_append in zip(islice(d2.values(), 1, None), list_of_lists):
lst_value.append(lst_to_append)
print(d2)
这输出:
{
'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]
}
你做 x = 1
,然后你马上做 for x in d2.values()
。这会用 d2.values()
的每个元素覆盖 x
。如果您想从 d2.values()
中的第二项开始,您需要创建一个迭代器并跳过第一个值:
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item in d2_iter: # Iterate over the remaining iterator
# Do what you want here.
另一个问题是您将 整个 list-of-lists 附加到 d2
中的每个值 。不要那样做。而是使用 zip()
同时迭代 list-of-lists 和 d2
中的值
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item_from_dict, item_to_append in zip(d2_iter, list_of_lists):
item_from_dict.append(item_to_append)
还剩下:
{'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]}
请注意,像这样附加只是因为列表是可变的。如果你有一个像元组这样的不可变类型作为 d2
的值,你必须创建一个新的元组并将它分配给键:
d3 = {'Youngstown': ('OH', 4110, 8065, 115436), 'Yankton': ('SD', 4288, 9739, 12011), 'Yakima': ('WA', 4660, 12051, 49826)}
d3_iter = iter(d2.keys())
next(d3_iter) # Consume one element
for key_from_dict, item_to_append in zip(d3_iter, list_of_lists):
new_item = d3[key_from_dict] + (item_to_append,) # Create a new item
d3[key_from_dict] = new_item
你会得到
{'Youngstown': ('OH', 4110, 8065, 115436),
'Yankton': ('SD', 4288, 9739, 12011, [966]),
'Yakima': ('WA', 4660, 12051, 49826, [1513, 2410])}
我正在尝试将列表列表的元素添加到字典的值中。
我创建了一个列表,其中包含如下文件中的元素:
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]....]
我正在尝试将此列表添加到我制作的字典中:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]....]
我试过以下代码:
x = 1
for x in d2.values():
d2.append(list_of_list)
print(d2)
我什至不确定这是可能的,但我正在努力使字典成为:
{'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011, [966]], 'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]....]
我怎样才能做到这一点?
我知道有更多方法可以做到这一点,但我认为这是更易读和理解的代码。
list_of_lists = [[966], [1513, 2410], [2964, 1520, 604]]
dict_ = {'Youngstown': ['OH', 4110, 8065, 115436], 'Yankton': ['SD', 4288, 9739,
12011], 'Yakima': ['WA', 4660, 12051, 49826]}
i = 0
# list(dict_.items())[1:] is the list of all keys and values except first one.
for key,value in list(dict_.items())[1:]:
dict_[key] = value+[list_of_lists[i]]
i+=1
print(dict_)
您可以使用 itertools.islice()
跳过第一个元素,然后使用 zip()
将每个列表值与要追加的列表配对:
from itertools import islice
for lst_value, lst_to_append in zip(islice(d2.values(), 1, None), list_of_lists):
lst_value.append(lst_to_append)
print(d2)
这输出:
{
'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]
}
你做 x = 1
,然后你马上做 for x in d2.values()
。这会用 d2.values()
的每个元素覆盖 x
。如果您想从 d2.values()
中的第二项开始,您需要创建一个迭代器并跳过第一个值:
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item in d2_iter: # Iterate over the remaining iterator
# Do what you want here.
另一个问题是您将 整个 list-of-lists 附加到 d2
中的每个值 。不要那样做。而是使用 zip()
同时迭代 list-of-lists 和 d2
中的值
d2_iter = iter(d2.values())
next(d2_iter) # Consume one element
for item_from_dict, item_to_append in zip(d2_iter, list_of_lists):
item_from_dict.append(item_to_append)
还剩下:
{'Youngstown': ['OH', 4110, 8065, 115436],
'Yankton': ['SD', 4288, 9739, 12011, [966]],
'Yakima': ['WA', 4660, 12051, 49826, [1513, 2410]]}
请注意,像这样附加只是因为列表是可变的。如果你有一个像元组这样的不可变类型作为 d2
的值,你必须创建一个新的元组并将它分配给键:
d3 = {'Youngstown': ('OH', 4110, 8065, 115436), 'Yankton': ('SD', 4288, 9739, 12011), 'Yakima': ('WA', 4660, 12051, 49826)}
d3_iter = iter(d2.keys())
next(d3_iter) # Consume one element
for key_from_dict, item_to_append in zip(d3_iter, list_of_lists):
new_item = d3[key_from_dict] + (item_to_append,) # Create a new item
d3[key_from_dict] = new_item
你会得到
{'Youngstown': ('OH', 4110, 8065, 115436),
'Yankton': ('SD', 4288, 9739, 12011, [966]),
'Yakima': ('WA', 4660, 12051, 49826, [1513, 2410])}