搜索其他2d列表中的数组元素并计算python

时间:2018-04-14 16:32:05

标签: python python-3.x python-2.7

我是python中的新手, 我有一个列表:A=['a','b','c'] 以及B=[['a','c'],['a','c'],['b','b']]之类的列表 我希望有一个像C=[2,1,2]这样的列表 C存储A的每个元素都出现在B中的子列表的出现 这意味着'a'在2个子列表中 'b'在1个子列表中 'c'在2个子列表中, 我该怎么做到这一点? 感谢

6 个答案:

答案 0 :(得分:5)

您可以使用grep "mike" file.csv | cut -d, -f4 Its the best of the best.

sum

输出:

a=['a','b','c']
b=[['a','c'],['a','c'],['b','b']]
final_list = [sum(i in c for c in b) for i in a]

答案 1 :(得分:3)

您可以循环b并为每个子列表更新collections.Counter,使用set删除重复项:

from collections import Counter

a = ['a','b','c']
b = [['a','c'],['a','c'],['b','b']]

counter = Counter()
for sublist in b:
    counter.update(set(sublist))
c = [counter[x] for x in a]

# result: [2, 1, 2]

答案 2 :(得分:1)

您可以在两个列表中循环和比较

a=['a','b','c']
b=[['a','c'],['a','c'],['b','b']]
result = []

for letter in a:
    count = 0
    for l in b:
        if letter in l:
            count += 1
    result.append(count)

答案 3 :(得分:1)

您可以使用list comprehensionsum构建C

C = [sum(elem in sub for sub in B) for elem in A]

这与使用嵌套for循环具有相同的效果:

C = []
for elem in A:
    sum = 0
    for sub in B:
        sum += elem in sub
    C.append(sum)

答案 4 :(得分:1)

您可以尝试使用dict方法:

A=['a','b','c']

B=[['a','c'],['a','c'],['b','b']]

d={}

for i in A:
    for j in B:
        if i in j:
            if i not in d:
                d[i]=1
            else:
                d[i]+=1
print(d)

输出:

{'c': 2, 'b': 1, 'a': 2}

答案 5 :(得分:0)

以下是collections.defaultdict的解决方案。

from collections import defaultdict

a = ['a','b','c']
b = [['a','c'],['a','c'],['b','b']]

# initialise defaultdict
d = defaultdict(int)

# convert to sets for performance
a_set = set(a)
b_sets = list(map(set, b))

# loop through list of sets
for item in b_sets:
    for i in item & a_set:
        d[i] += 1

# retrieve counts in correct order
res = list(map(d.get, a))

print(res)
# [2, 1, 2]

效果记录

这可能无关紧要,但性能差异很有意思,因为它清楚地显示Counter开销(慢4倍)。

from collections import defaultdict, Counter

a = ['a','b','c']
b = [['a','c'],['a','c'],['b','b']]

b = b*100000

def dd(a, b):
    d = defaultdict(int)
    a_set = set(a)
    b_sets = list(map(set, b))
    for item in b_sets:
        for i in item & a_set:
            d[i] += 1
    return list(map(d.get, a))

def counter(a, b):
    counter = Counter()
    for sublist in b:
        counter.update(set(sublist))
    return [counter[x] for x in a]

assert dd(a, b) == counter(a, b)

%timeit dd(a, b)       # 414 ms
%timeit counter(a, b)  # 1.65 s