使用Python操作列表中的多个字典



python中的输入字符串:列表中的多个字典

input = [{'a': '1', 'b':'2','c':'10'},{'a': '1', 'b':'3','c':'11'},{'a':'2','b':'19','c':'100']
output = [ {'1':{'b': ('2','3'),'c':('10','11')},'2':{'b':(19),'c':(100)}}] 

def dic_to_output (dic,坳):

df = pd.DataFrame(dic)
colname = list(df.columns)
#print(colname)
colname.remove(str(col))
df = df.groupby('a').agg(lambda x: list(x)).reset_index().set_index('a').T
return df.to_dict()

输入= [{' a ': ' 1 ', ' b ': ' 2 ', ' c ':"10"},{' a ': ' 1 ', ' b ': ' 3 ', ' c ': ' 11 '}, {' a ': ' 2 ', ' b ': ' 19 ', ' c ': ' 100 '})

dic_to_output (dic, a)

输出:{' 1 ': {' b ': ' 2 ', ' 3 ', ' c ':[‘10’,‘11’]},' 2 ':{"b":"19","c":[‘100’]}}

def func(mylist):
t = {}
for i in d:
itr = iter(i)
k = i[next(itr)]
tmp = t.get(k, {})
for m in itr:
n = i[m]
if (tmp.get(m, None) == None):
tmp[m] = tuple()
if (i[m] not in set(tmp[m])):
tmp[m] += (n,)
t[k] = tmp
print([t])
d = [{'a': '1', 'b':'2','c':'10'},{'a': '1', 'b':'3','c':'11'},{'a':'2','b':'19','c':'100'}]
func(d)
d = [{'a': '1', 'b':'2','c':'10'},{'a': '1', 'b':'3','c':'11'}]
func(d)
d = [{'a': '1', 'd': 4, 'b':'2','c':'10'},{'a': '1', 'd': 4, 'b':'3','c':'11'}]
func(d)
d = [{'a': '1', 'd': 4, 'b':'2','c':'10'},{'a': '1', 'b':'3', 'd': 4,'c':'11'}]
func(d)

最新更新