简体   繁体   中英

Create a dictionary from two lists and then print the data in python

I am new to python and am trying to grasp a few things. I would like to create a dictionary from two lists. I have two sets of data:

Person  |    Name
--------|--------
1       |   Jimmy
2       |    Mike
3       | Stanley
4       |    Will

I have both of these in lists, one called "person" and one called "name". I do this in a script which runs through multiple files to pull the data. each list is a different length with different data in it. Essentially, what I want to do is print the data as like this:

1 \t Jimmy
2 \t Mike
3 \t Stanley
4 \t Will

I pull the data from an xml file and here is the code I use to pull the data:

for people in xml.iter('people'):
  person.append(people.find('person').text)
  name.append(people.find('name').text)

So far the script works great and I can print out the two lists separately. What I don't know how to do is print them out together and so my thought is to store them to a dictionary instead, but I don't really know how to do that.

If you just want to print the two lists you can use zip (doc)

for num,name in zip(num_list,name_list):
    print str(num) + '\t' + name

you can use the same form to put the pairs in a dictionary (after you have the lists)

name_dict = {}
for num,name in zip(num_list,name_list):
    name_dict[num] = name

and then print

for num,name in name_dict.iteritems():
    print str(num) + '\t' + name

I don't think you need a dict here. To pair the elements of lists together, just use zip :

In [27]: zip(person, name)
Out[27]: [(1, 'Jimmy'), (2, 'Mike'), (3, 'Stanley'), (4, 'Will')]

For example,

person = [1,2,3,4]
name = ['Jimmy', 'Mike', 'Stanley', 'Will']
for p, n in zip(person, name):
    print('{p}\t{n}'.format(p = p, n = n))

yields

1   Jimmy
2   Mike
3   Stanley
4   Will

PS. If, for some reason, you do want a dict, it is easy to form too:

In [28]: dict(zip(person, name))
Out[28]: {1: 'Jimmy', 2: 'Mike', 3: 'Stanley', 4: 'Will'}

In elementary python.

people_dict = {}
for people in xml.iter('people'):
  person = people.find('person').text
  name = people.find('name').text
  people_dict[name] = person

Create the dict using the zip builtin function:

dct = dict(zip(person, name))

Then to print the dict, you can do something like this:

for k in dct:
    print '%s\t%s' % (k, dct[k])

good luck

Mike

If all you need to do is print them out together, then a list of tuples might be more appropriate than a dictionary, since a dictionary does not maintain order. Here is how you could do this:

people = []
for person in xml.iter('people'):
    people.append((person.find('person').text, person.find('name').text))

Then to print:

for p, name in people:
    print p + '\t' + name

Use the zip builtin function.

In [3]: person = range(1,5)

In [4]: name = ['Jimmy', 'Mike', 'Stanley', 'Will']

In [5]: zip(person, name)
Out[5]: [(1, 'Jimmy'), (2, 'Mike'), (3, 'Stanley'), (4, 'Will')]

In [6]: for p,n in zip(person, name):
   ...:     print p, '\t', n
   ...:     
1   Jimmy
2   Mike
3   Stanley
4   Will

If you are in Python 2.7 or newere, Dict comprehensions are available. They may be overkill for your particular situation, but they are a good technique to know and resolve this issue gracefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM