How do i instead print out the position or the index number of i? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How do i instead print out the position or the index number of i?

https://code.sololearn.com/ciPc4HGOvCW5/?ref=app

1st Mar 2022, 1:37 AM
Lenoname
4 Antworten
+ 3
You can use enumerate() function def poke(): pokex = [] dontx = [] for index, person in enumerate( personList ): gender = person.sex.capitalize() age = person.age if gender == 'Male': if 0 < age <= 35: pokex.append( index ) elif age > 35 : dontx.append( index ) elif gender == 'Female': if 0 < age <= 40 : pokex.append( index ) elif age > 40 : dontx.append(index) print('poke: ', pokex) print('dont poke: ', dontx)
1st Mar 2022, 3:41 AM
Ipang
+ 3
Lenoname You could try something along the lines of the following logic. Lets assume you have a list of the names of 30 people, and you want to poke everybody that has a 'v' in their name. pokeable = [] people = [your list of 30 names] for name in people: if 'v' in name: pokeable.append(people.index(name)) print(pokeable) #this list will hold the index numbers of the names that meet the criteria
1st Mar 2022, 5:16 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
FF9900 im trying to print who to poke and not to poke, three peoples names,sexes and ages are put in a list of objects, i iterate through the list and based on the conditions i put them in new lists, one for the people to be poked and the other for nonpokeable people. The thing is i just want their positions in the personList to be appended to the poke or dontpokelists and not their whole information.
1st Mar 2022, 2:42 AM
Lenoname
0
So instead of pokex.append(i) it should be pokex.append(index of i in personList)
1st Mar 2022, 2:46 AM
Lenoname