[英]Appending additional values to existing keys of a dictionary in for loop in python
我有一本字典dic = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
我會就像 append 值在每個循環之后將這些現有鍵作為數組而不覆蓋現有值。 像這樣:
list = [......]
keys_set = list(range(10))
dic = {key: 0 for key in keys_set}
for index in range(len(data)):
condition = list[index].numpy()
for x in (condition):
for key in keys_set:
if x == key:
dic[key].append[index]
但我收到錯誤AttributeError: 'int' object has no attribute 'append'
我試過(擴展,append,更新)但它們不起作用。 我希望返回如下所示的內容:
{0: ([24121, 4849, 40312, ..., 8343, 17503, 15662]),
1: ([25202, 37384, 53119, ..., 42502, 11939, 54489]),
2: ([47883, 45099, 6391, ..., 17220, 45904, 1735]),
3: ([36057, 24066, 36887, ..., 2587, 36021, 39961]),
4: ([23270, 45773, 10778, ..., 33301, 55598, 53125]),
5: ([40253, 30402, 24574, ..., 37823, 53769, 51425]),
6: ([48568, 4923, 1986, ..., 6178, 27890, 39086]),
7: ([35919, 36136, 38468, ..., 39877, 31991, 24692]),
8: ([23147, 40398, 1745, ..., 16440, 40957, 53170]),
9: ([ 3785, 583, 15312, ..., 42293, 58852, 46889])}
您需要在字典列表中創建值:
keys_set = list(range(10))
dic = {key: [] for key in keys_set}
for index in range(len(data)):
condition = list[index].numpy()
for x in (condition):
for key in keys_set:
if x == key:
dic[key].append[index]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.