tags:

views:

226

answers:

5

i have two seperate lists

list1 = ["Infantry","Tanks","Jets"]
list2 = [ 10, 20, 30]

so in reality, I have 10 Infantry, 20 Tanks and 30 Jets

I want to create a class so that in the end, I can call this:

for unit in units:
  print unit.amount
  print unit.name

#and it will produce:  
#  10 Infantry  
#  20 Tanks  
#  30 Jets  

so the goal is to sort of combine list1 and list2 into a class that can be easily called.

been trying many combinations for the past 3 hrs, nothing good turned out :(

+5  A: 

This should do it:

class Unit:
  """Very simple class to track a unit name, and an associated count."""
  def __init__(self, name, amount):
   self.name = name
   self.amount = amount

# Pre-existing lists of types and amounts.    
list1 = ["Infantry", "Tanks", "Jets"]
list2 = [ 10, 20, 30]

# Create a list of Unit objects, and initialize using
# pairs from the above lists.    
units = []
for a, b in zip(list1, list2):
  units.append(Unit(a, b))
unwind
`units.add(...)`?
Dominic Rodger
@Dominic: oops, sorry about that. Fixed, and thanks.
unwind
list comprehension one is better
Claudiu
+16  A: 
class Unit(object):
  def __init__(self, amount, name):
    self.amount = amount
    self.name = name

units = [Unit(a, n) for (a, n) in zip(list2, list1)]
Ignacio Vazquez-Abrams
+8  A: 
from collections import namedtuple

Unit = namedtuple("Unit", "name, amount")
units = [Unit(*v) for v in zip(list1, list2)]

for unit in units:
  print "%4d %s" % (unit.amount, unit.name)

Alex pointed out a few details before I could.

Roger Pate
Python 2.6+ only.
Ignacio Vazquez-Abrams
@Ignacio: which is a stable version available for over a year now.
SilentGhost
There are still Enterprise Linux distro versions in place that run 2.5 or even 2.4, and I believe that observers should be aware of the version requirement, even if it doesn't end up disqualifying the solution regarding the OP.
Ignacio Vazquez-Abrams
Is it unreasonable to expect the OP to read the docs (which I should've linked) and be at least aware if their version is behind the majority of users?
Roger Pate
N​o​t a​t all​.
Ignacio Vazquez-Abrams
+5  A: 

In Python 2.6, I'd recommend a named tuple -- less code than writing the simple class out and very frugal in memory use too:

import collections

Unit = collections.namedtuple('Unit', 'amount name')

units = [Unit(a, n) for a, n in zip(list2, list1)]

When a class has a fixed set of fields (doesn't need its instances to be "expandable" with new arbitrary fields per-instance) and no specific "behavior" (i.e., no specific methods necessary), consider using a named tuple type instead (alas, not available in Python 2.5 or earlier, if you're stuck with that;-).

Alex Martelli
What, I beat Alex Martelli to a Python answer? How did this happen? I think I need a minute to sober up and make sure it's not 2012 or 2038 yet.
Roger Pate
@Roger, heh, yep -- I took the extra time to provide the link, to carefully point out the 2.6 dependency to avoid Ignacio's usual whining about it (which punctually arrived for yours;-), etc, so your answer arrived while mine was still "in-flight";-).
Alex Martelli
+2  A: 

How about a dictionary:

units = dict(zip(list1,list2))

for type,amount in units.iteritems():
    print amount,type

Endlessly expandable for additional information, and easily manipulated. If a basic type will do the job, think carefully about not using it.

Phil H