Python 2D列表到字典
我有一个二维列表,必须从二维列表中获取两列,并将每列中的值作为键:值对放置 例如:Python 2D列表到字典,python,machine-learning,Python,Machine Learning,我有一个二维列表,必须从二维列表中获取两列,并将每列中的值作为键:值对放置 例如: table = [[15, 29, 6, 2], [16, 9, 8, 0], [7, 27, 16, 0]] def averages(table, col, by): columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it ca
table = [[15, 29, 6, 2],
[16, 9, 8, 0],
[7, 27, 16, 0]]
def averages(table, col, by):
columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it can be placed into dictionary
groupby = tuple(([table[i][by] for i in range(len(table))])) #Place groupby column into tuple so it can be placed into dictionary
avgdict = {}
avgdict[groupby] = [columns]
print(avgdict)
averages(table, 1, 3)
输出为:
{(2, 0, 0): [(29, 9, 27)]}
我试图使输出等于:
{0:36, 2:29}
因此,本质上,0的两个键的值相加
我很难理解如何将每个键与其值分开
然后,如果键相等,则将这些值相加
编辑:我只使用Python标准库,并没有针对这个问题实现numpy 您可以创建一个空字典,然后遍历groupby的每个元素。如果字典中存在groupby中的元素,则将列中相应的元素添加到字典中的值中。否则,将groupby中的元素添加为key,列中相应的元素添加为value,实现如下:
table = [[15, 29, 6, 2],
[16, 9, 8, 0],
[7, 27, 16, 0]]
def averages(table, col, by):
columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it can be placed into dictionary
groupby = tuple(([table[i][by] for i in range(len(table))])) #Place groupby column into tuple so it can be placed into dictionary
avgdict = {}
for x in range(len(groupby)):
key = groupby[x]
if key in avgdict:
avgdict[key] += columns[x]
else:
avgdict[key] = columns[x]
print(avgdict)
averages(table, 1, 3)
否则,如果要保留初始avgdict,则可以将平均值函数更改为
def averages(table, col, by):
columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it can be placed into dictionary
groupby = tuple(([table[i][by] for i in range(len(table))])) #Place groupby column into tuple so it can be placed into dictionary
avgdict = {}
avgdict[groupby] = [columns]
newdict = {}
for key in avgdict:
for x in range(len(key)):
if key[x] in newdict:
newdict[key[x]] += avgdict[key][0][x]
else:
newdict[key[x]] = avgdict[key][0][x]
print(newdict)
您可以创建一个空字典,然后遍历groupby的每个元素。如果字典中存在groupby中的元素,则将列中相应的元素添加到字典中的值中。否则,将groupby中的元素添加为key,列中相应的元素添加为value,实现如下:
table = [[15, 29, 6, 2],
[16, 9, 8, 0],
[7, 27, 16, 0]]
def averages(table, col, by):
columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it can be placed into dictionary
groupby = tuple(([table[i][by] for i in range(len(table))])) #Place groupby column into tuple so it can be placed into dictionary
avgdict = {}
for x in range(len(groupby)):
key = groupby[x]
if key in avgdict:
avgdict[key] += columns[x]
else:
avgdict[key] = columns[x]
print(avgdict)
averages(table, 1, 3)
否则,如果要保留初始avgdict,则可以将平均值函数更改为
def averages(table, col, by):
columns = tuple(([table[i][col] for i in range(len(table))])) #Place col column into tuple so it can be placed into dictionary
groupby = tuple(([table[i][by] for i in range(len(table))])) #Place groupby column into tuple so it can be placed into dictionary
avgdict = {}
avgdict[groupby] = [columns]
newdict = {}
for key in avgdict:
for x in range(len(key)):
if key[x] in newdict:
newdict[key[x]] += avgdict[key][0][x]
else:
newdict[key[x]] = avgdict[key][0][x]
print(newdict)
我花了一分钟的时间才弄明白您想要完成什么,因为您的函数和变量名引用平均值,但您的输出是一个总和 根据您的输出,您似乎试图通过另一列中的组来聚合给定列中的行值 这里有一个推荐的解决方案,通过列表理解,它可能会简化为一行。这将循环通过group by中的unique using set Value b,为正在处理的group by创建字典键agg_dict[b],如果正在处理group by,则对给定列col中的所有行进行求和表[i][by]==by
我花了一分钟的时间才弄明白您想要完成什么,因为您的函数和变量名引用平均值,但您的输出是一个总和 根据您的输出,您似乎试图通过另一列中的组来聚合给定列中的行值 这里有一个推荐的解决方案,通过列表理解,它可能会简化为一行。这将循环通过group by中的unique using set Value b,为正在处理的group by创建字典键agg_dict[b],如果正在处理group by,则对给定列col中的所有行进行求和表[i][by]==by
您也可以尝试以下答案。它不使用numpy,而是基于使用集合在groupby中查找唯一元素
您也可以尝试以下答案。它不使用numpy,而是基于使用集合在groupby中查找唯一元素
对不起,我不清楚你想要实现什么。也不清楚为什么这个问题被标记为机器学习对不起,我不清楚你想要实现什么。也不清楚为什么这个问题会被标记为机器学习我很抱歉让它不清楚,我把函数命名错了。没问题。我只是想帮你澄清这个问题。当你得到一个解决方案时,请记住投票表决有用的东西,并接受你最喜欢的答案,即使你必须自己写,这样堆栈溢出可以正确地存档该问题。我为不清楚表示歉意,我错命名了该函数。没问题。我只是想帮你澄清这个问题。当你得到一个解决方案时,请记住投票给有用的东西,并接受你最喜欢的答案,即使你必须自己写,这样堆栈溢出可以正确地存档问题。