Need help with python list manipulation -
i have 2 seperate lists
list1 = ["infantry","tanks","jets"] list2 = [ 10, 20, 30]
so in reality, have 10 infantry, 20 tanks , 30 jets
i want create class in end, can call this:
for unit in units: print unit.amount print unit.name #and produce: # 10 infantry # 20 tanks # 30 jets
so goal sort of combine list1 , list2 class can called.
been trying many combinations past 3 hrs, nothing turned out :(
this should it:
class unit: """very simple class track unit name, , associated count.""" def __init__(self, name, amount): self.name = name self.amount = amount # pre-existing lists of types , amounts. list1 = ["infantry", "tanks", "jets"] list2 = [ 10, 20, 30] # create list of unit objects, , initialize using # pairs above lists. units = [] a, b in zip(list1, list2): units.append(unit(a, b))
Comments
Post a Comment