+ 1
How do i instead print out the position or the index number of i?
5 Answers
+ 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)
+ 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
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.
0
So instead of pokex.append(i) it should be pokex.append(index of i in personList)