Python 在for循环中追加字典
我想在for循环中附加dictionary,这样就可以得到一个连接的dictionary。而且,并没有必要所有字典的键都完全相同 对于eqPython 在for循环中追加字典,python,python-3.x,dictionary,for-loop,Python,Python 3.x,Dictionary,For Loop,我想在for循环中附加dictionary,这样就可以得到一个连接的dictionary。而且,并没有必要所有字典的键都完全相同 对于eq one={'a': '2', 'c': 't', 'b': '4'} two={'a': '3.4', 'c': '7.6'} three={'a': 1.2, 'c': 3.4, 'd': '2.3'} 输出: combined={'a':['2','3.4','1.2'],'b':'4','c':['t','7.6','3.4'],
one={'a': '2', 'c': 't', 'b': '4'}
two={'a': '3.4', 'c': '7.6'}
three={'a': 1.2, 'c': 3.4, 'd': '2.3'}
输出:
combined={'a':['2','3.4','1.2'],'b':'4','c':['t','7.6','3.4'],
'd':'2.3'}
现在回到原来的问题:
每次for循环迭代时,都会生成一个字典,我想附加它
比如:
emptydict={}
for x in z:
newdict=x.dict()
emptydict.append(newdict)
print(emptydict)
你可以试试这样的
one = {'a': '2', 'c': 't', 'b': '4'}
two = {'a': '3.4', 'c': '7.6'}
three = {'a': 1.2, 'c': 3.4, 'd': '2.3'}
new_dict = {}
list_dict = [one, two, three]
for d in list_dict:
for key in d:
if key not in new_dict:
new_dict[key] = []
new_dict[key].append(d[key])
print(new_dict)
输出:{'a':['2','3.4',1.2],'c':['t','7.6',3.4],'b':['4'],'d':['2.3']}您可以尝试类似的方法
one = {'a': '2', 'c': 't', 'b': '4'}
two = {'a': '3.4', 'c': '7.6'}
three = {'a': 1.2, 'c': 3.4, 'd': '2.3'}
new_dict = {}
list_dict = [one, two, three]
for d in list_dict:
for key in d:
if key not in new_dict:
new_dict[key] = []
new_dict[key].append(d[key])
print(new_dict)
输出:{'a':['2','3.4',1.2],'c':['t','7.6',3.4],'b':['4'],'d':['2.3']}您可以尝试听写理解和列表理解:
new_dict = {k : [j[k] for j in [one,two,three] if k in j] for k in set(list(one.keys())+list(two.keys())+list(three.keys())
# Output : { 'a': ['2', '3.4', 1.2], 'b': ['4'], 'c': ['t', '7.6', 3.4], 'd': ['2.3']}
如果希望列表中不包含仅包含一个元素作为可能值的键,请尝试以下操作:
new_dict = a = {k : [j[k] for j in [one,two,three] if k in j][0] if len([j[k] for j in [one,two,three] if k in j]) ==1 else [j[k] for j in [one,two,three] if k in j] for k in set(list(one.keys())+list(two.keys())+list(three.keys()))}
# Output : {'a': ['2', '3.4', 1.2], 'b': '4', 'c': ['t', '7.6', 3.4], 'd': '2.3'}
您可以尝试听写理解和列表理解:
new_dict = {k : [j[k] for j in [one,two,three] if k in j] for k in set(list(one.keys())+list(two.keys())+list(three.keys())
# Output : { 'a': ['2', '3.4', 1.2], 'b': ['4'], 'c': ['t', '7.6', 3.4], 'd': ['2.3']}
如果希望列表中不包含仅包含一个元素作为可能值的键,请尝试以下操作:
new_dict = a = {k : [j[k] for j in [one,two,three] if k in j][0] if len([j[k] for j in [one,two,three] if k in j]) ==1 else [j[k] for j in [one,two,three] if k in j] for k in set(list(one.keys())+list(two.keys())+list(three.keys()))}
# Output : {'a': ['2', '3.4', 1.2], 'b': '4', 'c': ['t', '7.6', 3.4], 'd': '2.3'}
试试这个
one={'a': '2', 'c': 't', 'b': '4'}
two={'a': '3.4', 'c': '7.6'}
three={'a': 1.2, 'c': 3.4, 'd': '2.3'}
df = pd.DataFrame([one,two,three])
a b c d
0 2 4 t NaN
1 3.4 NaN 7.6 NaN
2 1.2 NaN 3.4 2.3
df.to_dict(orient='list')
输出
试试这个
one={'a': '2', 'c': 't', 'b': '4'}
two={'a': '3.4', 'c': '7.6'}
three={'a': 1.2, 'c': 3.4, 'd': '2.3'}
df = pd.DataFrame([one,two,three])
a b c d
0 2 4 t NaN
1 3.4 NaN 7.6 NaN
2 1.2 NaN 3.4 2.3
df.to_dict(orient='list')
输出
我已经用了你们的例子-
one = {'a': '2', 'c': 't', 'b': '4'}
two = {'a': '3.4', 'c': '7.6'}
three = {'a': 1.2, 'c': 3.4, 'd': '2.3'}
dicts = [one, two, three]
for dictionary in dicts:
for key, value in dictionary.items():
try:
new[key].append(value)
except KeyError:
new[key] = [value]
O/p-
我已经用了你们的例子-
one = {'a': '2', 'c': 't', 'b': '4'}
two = {'a': '3.4', 'c': '7.6'}
three = {'a': 1.2, 'c': 3.4, 'd': '2.3'}
dicts = [one, two, three]
for dictionary in dicts:
for key, value in dictionary.items():
try:
new[key].append(value)
except KeyError:
new[key] = [value]
O/p-
伪代码:必须迭代newdict的槽键并查找和附加到组合的[thiskey],而不是组合。伪代码:必须迭代newdict的槽键并查找和附加到组合的[thiskey],而不是组合。