#I know there's an easier approach to this problem but right now I have a list of objects and a list of data that needs to be added to each object individually. My code looks like this:
data=[1,2,3,4,5,5,5,6,6,6.5]
class Person(object):
def __init__(self,name):
self.name=name
self.data=0.0
Aiden=Person("Aiden")
AidenTwo=Person("AidenTwo")
AidenThree=Person("AidenThree")
AidenFour=Person("AidenFour")
AidenFive=Person("AidenFive")
AidenSix=Person("AidenSix")
AidenSeven=Person("AidenSeven")
AidenEight=Person("AidenEight")
AidenNine=Person("AidenNine")
AidenTen=Person("AidenTen")
list_of_people=[]
list_of_people.append(Aiden)
list_of_people.append(AidenTwo)
list_of_people.append(AidenThree)
list_of_people.append(AidenFour)
list_of_people.append(AidenFive)
list_of_people.append(AidenSix)
list_of_people.append(AidenSeven)
list_of_people.append(AidenEight)
list_of_people.append(AidenNine)
list_of_people.append(AidenTen)
for p in range(0,len(list_of_people)):
Person.data=data[p]
#This line/ for loop right here is the problem.
CodePudding user response:
Is this what you're looking for?
for p, d in zip(list_of_people, data):
p.data = d
CodePudding user response:
You can zip-and-assign the data to the people like this:
for person, person.data in zip(list_of_people, data):
pass
Try it online! (see the "Output" section where I print the updated people, supported by an added __str__
method)