[英]Python - Count number of rows/columns value appears in
Say I have a 2-dimensional array of strings like so: 假设我有一个二维数组的字符串,如下所示:
A = [['a', 'b', 'b'],
['c', 'c', 'a'],
['d', 'c', 'a']]
and I want to find out how many rows a given element appears in, so that I get the output: 我想知道给定元素出现的行数,以便我得到输出:
In [1]: get_number_rows('a')
Out[1]: 3
In [2]: get_number_rows('b')
Out[2]: 1
In [1]: get_number_rows('c')
Out[1]: 2
In [2]: get_number_rows('d')
Out[2]: 1
Note that I don't want the total number of occurrences of 'a', but the number of rows it appears in. 请注意,我不想要'a'的总出现次数,而是它出现的行数。
I have tried looping over the rows and simply counting, but I'm dealing with a very large dataset (1000s x 1000s), so it's very slow. 我试过循环遍历行并简单计数,但我正在处理一个非常大的数据集(1000s x 1000s),所以它非常慢。 Any faster solutions would be appreciated.
任何更快的解决方案将不胜感激
You can use the following get_number_rows()
method to sum if the number of arrays that that contain the character: 您可以使用以下
get_number_rows()
方法来get_number_rows()
包含该字符的数组的数量:
A = [['a', 'b', 'b'],
['c', 'c', 'a'],
['d', 'c', 'a']]
def get_number_rows(char):
return len([x for x in A if char in x])
get_number_rows('a')
>> 3
get_number_rows('b')
>> 1
get_number_rows('c')
>> 2
get_number_rows('d')
>> 1
For rows, try something like 对于行,尝试类似的东西
len([x for x in A if 'a' in x])
This list comprehension makes a list of all lists x
in A
satisfying the condition that 'a' in x
. 该列表理解列出了
A
满足x
中'a' in x
条件的所有列表'a' in x
。 You then take the length of that list to get the total number of them. 然后,您可以使用该列表的长度来获取它们的总数。
if __name__ == "__main__":
A = [['a', 'b', 'b'],
['c', 'c', 'a'],
['d', 'c', 'a']]
def get_number_rows(A, desired_element):
"""
Pass in two dimensional array, A as first parameter
Pass in desired char element, desired_element as 2nd parameter.
Function will return number of occurrences of desired_element in A.
"""
element_count = 0 # Int to keep track of occurrences
for group in A: # For nested array in A
if desired_element in group: # If our desired element is in the sub array
element_count += 1 # Increment our counter
return element_count # After completion, return the counter
print(get_number_rows(A, 'a'))
print(get_number_rows(A, 'b'))
print(get_number_rows(A, 'c'))
print(get_number_rows(A, 'd'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.