简体   繁体   English

python从字符串列表和整数列表创建字典

[英]python create dictionary from list of strings and list of integers

I have two lists: one contain the name of pages like ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)'] and the other list contain the corresponding page number like [1, 2, 3] 我有两个列表:一个包含页面名称,例如['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)'] ,而另一个列表则包含相应的页码,例如[1, 2, 3]

I want to create a dictionary from two lists such that the splitted words from list one as a key and corresponding value from list two as value. 我想从两个列表中创建一个字典,以便将列表一中的拆分词作为键,并将列表二中的对应值作为值。 And if key already is in dictionary then append value to it. 如果密钥已经在字典中,则将值附加到字典中。

For the above example I want a dictionary like: 对于上面的示例,我想要一个像这样的字典:

{
 'Barrel': [1],
 '-': [1],
 'Part': [1],
 '1': [1],
 'Petit': [2],
 'Trees': [2],
 # '(sketch)': [2],
 'Island': [3],
 '(sketch)':[2, 3]   #in this line the value appended as the key already has a value 2                                     
}

You can use zip() to loop through the two lists simultaneously. 您可以使用zip()同时遍历两个列表。 If you don't need the dict to be in order, it's much easier to use collections.defaultdict() than a normal dictionary: 如果您不需要字典,那么使用collections.defaultdict()比普通字典要容易得多:

import collections

titles =  ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)']
pages =  [1, 2, 3]

d = collections.defaultdict(list)

for title, page in zip(titles, pages):
    for word in title.split():
        d[word].append(page)

Although since your pages is just a list of subsequent numbers, it's probably better to use enumerate so you don't have to update the pages list every time you make changes: 尽管由于pages只是后续编号的列表,所以最好使用enumerate这样就不必在每次更改时都更新pages列表:

import collections

titles =  ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)']
d = collections.defaultdict(list)

for page, title in enumerate(titles, start=1):
    for word in title.split():
        d[word].append(page)

Now if you do need the dict to be in order, you can use OrderedDict combined with @Keatinge's answer: 现在,如果您确实需要按顺序排列字典,则可以将OrderedDict与@Keatinge的答案结合使用:

import collections

titles =  ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)']
d = collections.OrderedDict()

for title, page in enumerate(titles, start=1):
    for word in title.split():
        if word not in d:
            d[word] = [page]
        else:
            d[word].append(page)

Or if you only need the output to be sorted, use the earlier defaultdict solution and throw in sorted() when outputting the values: 或者,如果只需要对输出进行排序,请使用较早的defaultdict解决方案,并在输出值时放入sorted()

for key in sorted(d.keys()):
    print('{0}: {1}'.format(key, d[key]))

Finally, you could use an OrderedDefaultDict , but most would argue that this is a bit of an overkill for such a simple program. 最后,您可以使用OrderedDefaultDict ,但是大多数人会认为,对于这样一个简单的程序,这有点过头了。

You might be surprised that the results are out of order, but that's because dicts in python don't have an order. 您可能会对结果不正常感到惊讶,但这是因为python中的字典没有顺序。 If you want them ordered you'll need to use something other than a vanilla dict. 如果您要他们订购,则需要使用除香草字典以外的其他东西。

titles =  ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)']
pages =  [1, 2, 3]

finalDict = {}
for title, page in zip(titles, pages):
    for word in title.split(" "):
        if word not in finalDict.keys():
            finalDict[word] = [int(page)]
        else:
            finalDict[word] += [int(page)]

print(finalDict)

This outputs: 输出:

{'Part': [1], '1': [1], 'Trees': [2], 'Island': [3], 'Barrel': [1], '-': [1], '(sketch)': [2, 3], 'Petit': [2]}

List comprehension approach. 列表理解方法。

Basically double iteration in list comprehension is used here (look more pythonic to me). 基本上,这里使用列表理解中的双迭代(对我来说,看起来更pythonic)。 Another way of iterating would be using itertools.chain . 迭代的另一种方法是使用itertools.chain

from collections import defaultdict
d = defaultdict(list)
page_names =  ['Barrel - Part 1', 'Petit Trees (sketch)', 'Island (sketch)']
pages =  [1, 2, 3]

for k, v in [(y, x[1]) for x in zip(page_names, pages) for y in x[0].split(' ')]:
    d[k].append(v)

And to convert the a list with duplicated keys, if the order is not a concern. 如果顺序无关紧要,请转换具有重复键的列表。 Then coolections.defaultdict would be quite useful. 然后coolections.defaultdict将非常有用。 Though pure base python approach would also work, and it will be something like this: 虽然纯基本的python方法也可以使用,但它是这样的:

d = {}
for x in l:
    if x.key not in l:
        d[x.key] = []
    d[x.key].append(x.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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