This question already has answers here:
Finding the index of an item in a list
(43 answers)
Closed 4 years ago.
I want to find the index of values that contain a keyword in an array.
For example:
A = ['a1','b1','a324']
keyword = 'a'
I want to get [0,2], which is the index of a1, a324
I tried this list(filter(lambda x:'a' in x, A))
But get ['a1','a324'] rather than the index.
Use enumerate with a list-comprehension:
A = ['a1','b1','a324']
keyword = 'a'
print([i for i, x in enumerate(A) if keyword in x])
# [0, 2]
Simply write:
A = ['a1','b1','a324']
keyword = 'a'
indices = [i for i in range(len(A)) if keyword in A[i]]
print(indices)
Related
This question already has answers here:
How to check if a tuple contains an element in Python?
(4 answers)
Closed 18 days ago.
how do i find a certain element in a tuple and print out the index of the element.
a = (1,2,3,4)
b = int(input('enter the element you want to find'))
if b in a :
print('found')
here is the simple program. now how do print the index of the element whic needs to be searched
Use .index:
if b in a:
print(a.index(b))
You can use enumerate as well:
enumerate
a = (1,2,3,4)
b = int(input('enter the element you want to find'))
for x,y in enumerate(a):
if b == y:
print(x)
Edit:
to find the multiple indices of an element:
Suppose we have b at 3 positions, now if you want to find all the three indices then:
a = (1,2,3,4,5,6,2,8,2)
b = 2
for i, x in enumerate(a):
if x == b:
print(i)
1
6
8
with list comprehension :
[i for i, x in enumerate(a) if x == b]
#[1, 6, 8]
Use:
print(a.index(b))
Use this link for working with tuples
This question already has answers here:
How to find char in string and get all the indexes?
(12 answers)
Closed 4 years ago.
I would like to find all the indexes of the letter 'a' in a string 'akacja'. However python always seems to return only the first index that it has found.
Any solutions?
Thanks for help.
You could use list comprehension since str.index() and str.find() will only return the first index:
s = 'akacja'
indexes = [i for i, c in enumerate(s) if c == 'a']
print(indexes)
# OUTPUT
# [0, 2, 5]
You could do this …
indexes = [i for i,c in enumerate('akacja') if c == 'a']
The above line uses list comprehension which is short hand for:
indexes = []
for i,c in enumerated('akacja'):
if c == 'a':
indexes.append(i)
You could also use regex like so:
import re
indexes = [f.start() for f in re.finditer('a', 'akacja')]
This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 4 years ago.
Am trying to print elements of index 1 like 4994 2993 100 20.5 from my tuple but it print ('dan', '2993').
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
print(b[1])
Have searched on this site and none of the answer provided gives me clue to solve this.
You can use unpacking in a list comprehension:
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
new_b = [i for _, i in b]
Output:
['4994', '2993', 100, 20.5]
Another list comprehension way:
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
new_b = [i[1] for i in b]
# ['4994', '2993', 100, 20.5]
b = (("ben","4994"),("dan","2993"),("Hans",100),("Frank",20.5))
for item in b:
print(item[1])
In this case you are trying to access the second element of a two-items tuple. Using slicing would be:
>>> b[0][1]
and you will get as output>
>>> '4994'
If you put this tuple into a for loop, the first element (item in my code) would be: ("ben","4994") and when you print(item[1]) you are having access to the second level of slicing, that is: '4994' in the first running of the loop and so on.
This question already has answers here:
Python: filter list of list with another list
(2 answers)
Closed 5 years ago.
Say I have a list of strings, mylist, from which I want to extract elements that satisfy a condition in a another list, idx:
mylist = ['a','b','c','d']
idx = ['want','want','dont want','want']
What I want as output is:
['a','b','d']
which is a list of elements that I 'want'
How can this by done?
You can use zip to stride through your two lists element-wise, then keep the elements from mylist if the corresponding element from idx equals 'want'
>>> [i for i, j in zip(mylist, idx) if j == 'want']
['a', 'b', 'd']
This question already has answers here:
How to find all occurrences of an element in a list
(18 answers)
Closed 10 years ago.
I have the following list:
lista = [1,2,3,5,0,5,6,0]
I know that print(lista.index(0)) will print the first index where it found the number i.e. 4
How do I get it to print the next index which should be 7 etc?
The classic way is to build a list of the indices:
eg:
>>> indices = [i for i, v in enumerate(a) if v == 0]
>>> print indices
[4, 7]
Of course - this can be turned into a generator expression instead. But in short - using enumerate is what you're after.