Home > Net >  How to find the next index of a charachter in python list
How to find the next index of a charachter in python list

Time:12-12

I was dealing with python list and I want to find the next index of the recurring character in the list how can I accomplish that

country = ['Beekeeper', 'Fly', 'Hornet']


pickedCountry = random.choice(country)
let the pickedCountry = 'Beekeeper'
print(pickedCountry .index('e'),)

it print out only 1

CodePudding user response:

pickedCountry = 'Beekeeper'
last_index = 0
while last_index < len(pickedCountry):
    current_index = pickedCountry.index('e', last_index)
    print(current_index)
    last_index = current_index   1
pickedCountry = 'Beekeeper'
last_index = 0
while last_index < len(pickedCountry):
    try:
        current_index = pickedCountry.index('e', last_index)
    except ValueError:
        break
    print(current_index)
    last_index = current_index   1
  • Related