For example, I have two lists:
A=['a','b','c']
B=['a','a','b','c']
I want my output to look like:
[2,1,1]
Here is what I tried:
P=np.ndarray(shape=3, dtype=int)
count=0
d=0
for i in A:
for j in B:
if i==j:
count+=1
P[d]=count
d+=1
But I am getting
[2,3,4]
as output. What is going wrong?
答案 0 :(得分:4)
Just use count
method:
A=['a','b','c']
B=['a','a','b','c']
[B.count(x) for x in A]
答案 1 :(得分:3)
The methods as suggested by @CarlesMitjans or @WillenVanOnsem are better solutions than this, but the reason your method does not work is that you need to initialise your count
variable inside the first loop (and also indent the d+=1
line):
P = np.ndarray(shape=3, dtype=int)
count = 0
d = 0
for i in A:
count = 0
for j in B:
if i == j:
count += 1
P[d] = count
d += 1
which gives:
>>> P
array([2, 1, 1])
答案 2 :(得分:2)
If the elements are hashable, you can simply use a counter you then transform back into a list. Like:
from collections import Counter
ctr = Counter(B)
result = [ctr[x] for x in A]
This generates:
>>> [ctr[x] for x in A]
[2, 1, 1]
This works in O(|A|+|B|) with |A| the numbers in A
and |B| the numbers in B
(given dictionary lookup works in O(1), which is nearly always the case).
If the elements can not be hashed (for instance list
s, dict
ionaries, object
s,...), then you can use @CharlesMitjans answer, which runs in O(|A|×|B|) (which tends to be less efficient, but then you cannot take advantage of hashing).