[英]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. 我是python的新手,正在尝试掌握一些东西。 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: 我从xml文件中提取数据,这是我用来提取数据的代码:
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) 如果您只想打印两个列表,则可以使用
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 :
要将列表中的元素配对在一起,只需使用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. 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. 在基本的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: 使用内置的zip函数创建字典:
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. 使用zip内置功能。
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. 如果您使用的是Python 2.7或更高版本,则可以使用Dict理解 。 They may be overkill for your particular situation, but they are a good technique to know and resolve this issue gracefully.
对于您的特定情况,它们可能会显得过大,但它们是很好地了解和解决此问题的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.