[英]Tallying elements at index in a multidimensional array
I'm trying to figure out how to go through a multidimensional array and take a tally of the elements at each index我试图弄清楚如何通过多维数组 go 并计算每个索引处的元素
If I have an array that looks like the following:如果我有一个如下所示的数组:
letter_array = [[A,C,D],[C,F,R,L],[A,F,Q],[B,F,D]]
How would I tally each element at its index?我将如何在其索引处计算每个元素?
I'm aiming for an end result along the lines of something like this:我的目标是按照这样的方式获得最终结果:
Index 0 - A:2, C:1, B:1索引 0 - A:2, C:1, B:1
Index 1 - C:1, F:3索引 1 - C:1, F:3
Index 2 - D:2, R:1, Q:1索引 2 - D:2, R:1, Q:1
Index 3 - L:1索引 3 - L:1
Thank you谢谢
Easiest way will be this:最简单的方法是:
import pandas as pd
data = pd.DataFrame(letter_array)
dictionary = {}
for i in range(len(data)):
dictionary[i] = data[i].value_counts().to_dict()
dictionary
{0: {'A': 2, 'C': 1, 'B': 1},
1: {'F': 3, 'C': 1},
2: {'D': 2, 'Q': 1, 'R': 1},
3: {'L': 1}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.