How to avoid selecting the same elements in a list?
Question:
Assume there two lists:A=[1,2,3,4,5,6,7];B=[23,25,34,23,24,10,45]
. The elements in A List
represent the name of products, while those in B list
represent the price of products. For example, product 1 worths 23 USD.
I want to retrieve the products whose price is 23 USD.
What I tried was shown below:
Prod_23 = []
for i in B:
if i == 23:
index = B.index(i)
product = A[index]
Prod_23.append(product)
The result was:
Prod_23 = [1,1]
But the actual result should be like:
Prod_23 = [1,4]
Could you please give me some clues of avoiding selecting the first element twice and only selecting the first and fourth elements?
Answers:
why don’t do this:
a[b.index(23)]
Use zip()
to tie lists A and B together and use list-comprehension:
list_23 = [a for a, b in zip(A, B) if b == 23]
print(list_23)
Prints:
[1, 4]
You can also use any of the following methods for this purpose:
Python2 :
>>> indices = [A[i] for i in xrange(len(A)) if B[i] == 23]
>>> indices
[1, 4]
>>>
Python3:
Using a for-loop with range()
[Python-docs] :
>>> indices = [A[i] for i in range(len(A)) if B[i] == 23] # or [A[i] for i in A if B[i] == 23]
>>> indices
[1, 4]
>>>
Using for-loop and enumerate()
[Python-docs] :
>>> indices = [A[i] for i, x in enumerate(B) if x == 23]
>>> indices
[1, 4]
>>>
Using locate()
[more-itertools-docs] with for-loop:
>>> from more_itertools import locate
>>> indices = [A[i] for i in locate(B, lambda x: x == 23)]
>>> indices
[1, 4]
>>>
Using locate()
[more-itertools-docs] with itemgetter()
[Python-docs]
>>> from more_itertools import locate
>>> from operator import itemgetter
>>> indices = list(itemgetter(*locate(B, lambda x: x == 23))(A))
>>> indices
[1, 4]
>>>
Assume there two lists:A=[1,2,3,4,5,6,7];B=[23,25,34,23,24,10,45]
. The elements in A List
represent the name of products, while those in B list
represent the price of products. For example, product 1 worths 23 USD.
I want to retrieve the products whose price is 23 USD.
What I tried was shown below:
Prod_23 = []
for i in B:
if i == 23:
index = B.index(i)
product = A[index]
Prod_23.append(product)
The result was:
Prod_23 = [1,1]
But the actual result should be like:
Prod_23 = [1,4]
Could you please give me some clues of avoiding selecting the first element twice and only selecting the first and fourth elements?
why don’t do this:
a[b.index(23)]
Use zip()
to tie lists A and B together and use list-comprehension:
list_23 = [a for a, b in zip(A, B) if b == 23]
print(list_23)
Prints:
[1, 4]
You can also use any of the following methods for this purpose:
Python2 :
>>> indices = [A[i] for i in xrange(len(A)) if B[i] == 23]
>>> indices
[1, 4]
>>>
Python3:
Using a for-loop with range()
[Python-docs] :
>>> indices = [A[i] for i in range(len(A)) if B[i] == 23] # or [A[i] for i in A if B[i] == 23]
>>> indices
[1, 4]
>>>
Using for-loop and enumerate()
[Python-docs] :
>>> indices = [A[i] for i, x in enumerate(B) if x == 23]
>>> indices
[1, 4]
>>>
Using locate()
[more-itertools-docs] with for-loop:
>>> from more_itertools import locate
>>> indices = [A[i] for i in locate(B, lambda x: x == 23)]
>>> indices
[1, 4]
>>>
Using locate()
[more-itertools-docs] with itemgetter()
[Python-docs]
>>> from more_itertools import locate
>>> from operator import itemgetter
>>> indices = list(itemgetter(*locate(B, lambda x: x == 23))(A))
>>> indices
[1, 4]
>>>