如何在Python中使用for循环插入字典中的值?

huangapple go评论105阅读模式
英文:

how I can inssert valuees in the dict in python using for loop

问题

如何将person字典的所有值添加到all字典中,因为当运行代码时,结果将如下所示:
{0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}}
我需要将person的所有值插入all字典中。

要将person字典的所有值添加到all字典中,你可以使用以下代码:

for j in range(8):
    N = []
    for i in range(4):
        N.append(randint(min(data), max(data)))

    person['cluster'] = N
    person['obj'] = FCM.objfunc(FCM.rlistes(data, person['cluster']), person['cluster'])
    all[j] = person.copy()  # Use the copy method to add a copy of the 'person' dictionary to 'all'
    N.clear()
    person.clear()
print(all)

通过使用person.copy()方法,你将'person'字典的副本添加到'all'字典中,而不是将同一字典添加多次。这将产生以下结果:
{0: {'cluster': [...], 'obj': ...}, 1: {'cluster': [...], 'obj': ...}, 2: {'cluster': [...], 'obj': ...}, 3: {'cluster': [...], 'obj': ...}, 4: {'cluster': [...], 'obj': ...}, 5: {'cluster': [...], 'obj': ...}, 6: {'cluster': [...], 'obj': ...}, 7: {'cluster': [...], 'obj': ...}}

英文:
from random import *
from PIL import Image
import numpy as np
import FCM as FCM
from collections import defaultdict

ii=Image.open("dataset/image11.jpg").convert("L")
data = list(ii.getdata())

#print(aa)
person = {}
N=[]
all={}

for j in range(8):
    for i in range(4):
        N.append(randint(min(data),max(data)))
    
    person['cluster'] = N
    person['obj']=FCM.objfunc(FCM.rlistes(data,person['cluster']),person['cluster'])
    all[j]=person
    N.clear()
    person.clear()
print(all)


How I can add all values of person dictionary in the all dictionary because when I run the code the result will be as
{0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}}
I need to insert all values of person in all dictionary

答案1

得分: 1

为了解决这个问题,您可以在以下代码中使用for循环将值插入到'person'字典中,用以下代码替换:

for j in range(8):
    N = [randint(min(data),max(data)) for i in range(4)]
    
    person = {
        'cluster': N,
        'obj': FCM.objfunc(FCM.rlistes(data, N), N)
    }
    
    all[j] = person

这样,我们使用一个列表创建包含来自'data'范围内的4个随机整数的'N'列表。然后,我们创建一个新的'person'字典,包括键'cluster'和'obj'以及'objfunc()'结果的值。最后,我们使用循环索引'j'将'person'字典插入到'all'字典中。

如果这对您有用,请告诉我:D

英文:

To solve this you can insert values into the 'person' dictionary in the following code using a for loop by replacing with these lines:

for j in range(8):
for i in range(4):
    N.append(randint(min(data),max(data)))

person['cluster'] = N
person['obj']=FCM.objfunc(FCM.rlistes(data,person['cluster']),person['cluster'])
all[j]=person
N.clear()
person.clear()

with

for j in range(8):
N = [randint(min(data),max(data)) for i in range(4)]

person = {
    'cluster': N,
    'obj': FCM.objfunc(FCM.rlistes(data, N), N)
}

all[j] = person

this way we use a list to create the list 'N' with 4 random integers from the range of 'data'. Then we create a new 'person' dict with keys 'cluster' and 'obj' and the values of the result of the 'objfunc()'. Finally, we insert the 'person' dictionary into the 'all' dictionary using the loop index 'j'.

Let me know if this worked for you 如何在Python中使用for循环插入字典中的值?

huangapple
  • 本文由 发表于 2023年3月7日 03:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定