简体   繁体   English

在Python中从字符串列表创建列表

[英]Create a list from strings-list in Python

I have a list in Python: 我在Python中有一个列表:

['first', 'second', 'foo']

I want to create a list of lists named after the list elements: 我想创建一个以list元素命名的列表列表:

newlist = ['first':[], 'second':[], 'foo':[]]

I have seen some proposals that use Dictionaries, but when I tried to do it with OrderedDict, I lost the order of the elements in the creation. 我看到了一些使用字典的建议,但是当我尝试使用OrderedDict进行操作时,我丢失了创建中元素的顺序。

Thanks in advance. 提前致谢。

You can use the method fromkeys() : 您可以使用fromkeys()方法:

l = ['first', 'second', 'foo']

dict.fromkeys(l, [])
# {'first': [], 'second': [], 'foo': []}

In Python 3.6 and below use OrderedDict instead of dict : 在Python 3.6及以下版本中,请使用OrderedDict而不是dict

from collections import OrderedDict

l = ['first', 'second', 'foo']
OrderedDict.fromkeys(l, [])
# OrderedDict([('first', []), ('second', []), ('foo', [])])

Since Python 3.7 regular Python's dict s are ordered: 由于Python 3.7常规Python的dict是有序的:

>>> dict((name, []) for name in ['first', 'second', 'third'])
{'first': [], 'second': [], 'third': []}

dict s in CPython 3.6 are also ordered, but it's an implementation detail. CPython 3.6中的dict也是有序的,但这是一个实现细节。

@ForceBru gave a nice answer for Python 3.7 (I learned myself), but for lower versions that would work: @ForceBru对于Python 3.7(我了解到自己)给出了一个很好的答案,但是对于可以使用的较低版本:

from collections import OrderedDict
l = ['first', 'second', 'foo']
d = OrderedDict([(x, []) for x in l])

The elements in the array you wanna end up having must be proper objects and the format that you've displayed in the example, doesn't make a lot of sense, but you can try to use dictionary elements inside your array where each elemnt has key (ei 'foo' ) and value (ie '[]' ). 您最终想要拥有的数组中的元素必须是正确的对象,并且您在示例中显示的格式没有多大意义,但是您可以尝试在数组中使用dictionary元素,其中每个元素都有键(ei'foo 'foo' )和值(即'[]' )。 So you will end with something like this: 因此,您将得到如下所示的结果:

newlist = [{'first':[]}, {'second':[]}, {'foo':[]}]

Now if you are happy with that, here is a map function with an anonymous lambda function which is gonna convert your initial array: 现在,如果您对此感到满意,这是一个带有匿名lambda函数的map函数,它将转换您的初始数组:

simplelist = ['first', 'second', 'foo']
newlist = list(map(lambda item: {item:[]}, simplelist))

Hope, you got your answer. 希望,你得到了答案。

Cheers! 干杯!

The structure that you have indicated, is a dictionary dict . 您指定的结构是字典dict The structure looks like: 结构如下:

test_dictionary = {'a':1, 'b':2, 'c':3}

# To access an element
print(test_dictionary['a'])   # Prints 1

To create a dictionary, as per your requirement: 根据您的要求创建字典:

test_dictionary = dict((name, []) for name in ['first', 'second', 'foo'])
print(test_dictionary)

The above line of code gives the following output: 上面的代码行给出以下输出:

{'first': [], 'second': [], 'foo': []}

The first problem is that you refer to the term "list", but you mean it as a word concept, not as a data type in Python language. 第一个问题是您指的是“列表”一词,但您将其表示为单词概念,而不是Python语言中的数据类型。 The second problem is that the result will no longer represent the data type <list> , but the data type of the <dict> (dictionary). 第二个问题是结果将不再代表数据类型<list> ,而是代表<dict> (字典)的数据类型。 A simple one-line for can convert your variable-type <list> to the desired dictionary-type variable. 一个简单的一个线for可以将可变类型的转换<list>到所需的字典式可变。 It works in Python 2.7.x 它适用于Python 2.7.x

>>> l = ['first', 'second', 'foo']
>>> type(l)
<type 'list'>
>>> d = {x:[] for x in l}
>>> type(d)
<type 'dict'>
>>> d
{'second': [], 'foo': [], 'first': []}

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

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