0

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 Answers 5

18
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)]
Sign up to request clarification or add additional context in comments.

Comments

8
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.

3 Comments

@Ignacio: which is a stable version available for over a year now.
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.
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?
5

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))

1 Comment

list comprehension one is better
5

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;-).

2 Comments

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, 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";-).
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.