how to get index of a giving string in liste python?

phœnix

my list is like this, in example the string is 'a' and 'b' ; i want to return the index of string 'a' and for 'b' then i want to calculate how many time is 'a' repeated in the list1 :

list1=['a','a','b','a','a','b','a','a','b','a','b','a','a']

i want to return the order of evry 'a' in list1 the result should be like this :

a_position=[1,2,4,5,7,8,10,12,13]

and i want to calculate how many time 'a' is repeated in list1: a_rep=9

SomeDude

You could do below:

a_positions = [idx + 1 for idx, el in enumerate(list1) if el == 'a']
a_repitition = len(a_positions)

print(a_positions):

[1, 2, 4, 5, 7, 8, 10, 12, 13]

print(a_repitition):

9

If you need repititions of each element you can also use collections.Counter

from collections import Counter
counter = Counter(list1)

print(counter['a']):

9

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get item from list by giving index number?

How to get index position from a string in python without repetitive results

How to get the whole word starting at an index in a string in Python

How to get a specific index of a string?

How to get the index of a string in an array?

How to get String Index in SwiftUI?

How do I get the String type or timestamp type by giving a year

How to get value from List<> giving String as variable name

How to get the index of Differences in String from index - to index?

How to get the index in the 'in' statement in Python

Search an array for a string to get its index in python

Python get number sequence from a string with a index

accessing index of string but it is not giving right answer

how to index a string in C like a string in python?

How to get the index of each specified character in a string?

How to get the last index from a String in a for loop

How to get the (n) index of character inside String?

How to get the match index in the string using javascript?

How to get char from string by index?

How to get an Int from a string.index?

How to get String with specific index - swift

How to get a substring based on index of another string

How to get index of a text in a string in swift 4?

How to get the start and last index of a string match

how to get the index of a particular string in dropdownlist

How to get element of array with string index path

How to get the index of numpy string array?

How can I get the index of a string in Python list, which contains a certain character?

how can I get the index of the item in a list that contains the string being search (within the list) in python