简体   繁体   中英

Given two list of strings, how can I convert them into a into a dict?

I have the following list of strings:

content = [['a list with a lot of strings and chars 1'], ['a list with a lot of strings and chars 2'], ['a list with a lot of strings and chars 3'], ['a list with a lot of strings and chars 4']]

labels = ['label_1','label_2','label_3','label_4']

How can I create a dictionary from them:

{
'label_1': ['a list with a lot of strings and chars 1']
'label_2': ['a list with a lot of strings and chars 2']
'label_3': ['a list with a lot of strings and chars 3']
'label_4': ['a list with a lot of strings and chars 4']
}
dictionary = dict(zip(labels, content))

Various versions:

def f1(labels, content):
    return dict(zip(labels, content))

def f2(labels, content):
    d = {}

    for i, label in enumerate(labels):
        d[label] = content[i]
    return d

def f3(labels, content):
    d = {}
    for l, c in zip(labels, content):
        d[l] = c
    return d

def f4(labels, content):
    return {l : c for (l, c) in zip(labels, content)}

def f5(labels, content):
    dictionary = {}

    for i in range(len(content)):
       dictionary[labels[i]] = content[i]
    return dictionary

def f6(labels, content):
    return {l : content[i] for (i, l) in enumerate(labels)}

Timing

These are tested using Python 3.6.7 . Note that different versions of Python might have different performance, so you should probably re-run the benchmarks on your intended platform.

In [20]: %timeit f1(labels, content)
637 ns ± 4.17 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [21]: %timeit f2(labels, content)
474 ns ± 4.44 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [22]: %timeit f3(labels, content)
447 ns ± 2.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [23]: %timeit f4(labels, content)
517 ns ± 4.44 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [24]: %timeit f5(labels, content)
529 ns ± 8.04 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit f6(labels, content)
602 ns ± 0.64 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Fastest

Fastest is f3 , a modification of the answer by @Michael_MacAskill to use zip instead of using the index to extract the value from content .

Interestingly, a dictionary comprehension of @Michael_MacAskill 's answer did not perform better than the one using plain for-loops. Perhaps the implementers of the language realized that people still stuck with for-loops most of the time and implemented some performance hack for them.

Most Pythonic

Most experienced Python programmers will probably go with the dict(zip(labels, content)) option if the differences in speed is not critical since it is a common idiom in the language.

dictionary = {} for i in range(len(content)): dictionary[labels[i]] = content[i] #setting each element in labels list as key and each corresponding index of content list's content as value

Maybe this could be done more efficiently with a dictionary comprehension but here is a quick and dirty way:

d = {}                                                                                                                

for i, label in enumerate(labels): 
    d[label] = content[i] 

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