[英]Python: create a list containing dicts
how would you turn this string: 你怎么把这个字符串:
str='ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:CL5'
into a list that give you this: 到一个列表,给你这个:
print x[1]['building']=CL5
which would be: 可能是:
x=[{'ldap':'alberthwang','eeid':'67739'},{'ldap':'meng','eeid':'107','building':'CL5'}]
i've tried to split the string first and append to a list: 我试过先分割字符串并追加到列表中:
sample=[]
for s in str.split('|'):
sample.append(s)
But i'm stuck on how to turn the list items into a dictionary that i can then use to populate another list. 但是我仍然坚持如何将列表项转换成字典,然后可以用来填充另一个列表。
text='ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:CL5'
sample=[
dict(item.split(':') for item in part.split(','))
for part in text.split('|')]
print(sample)
# [{'eeid': '67739', 'ldap': 'alberthwang'}, {'building': 'CL5', 'eeid': '107', 'ldap': 'meng'}]
print(sample[1]['building'])
# CL5
str
is a built-in type, so assigning a string to str
overwrites the builtin. str
是内置类型,因此将字符串分配给str
覆盖内置函数。 It's better to choose some other variable name to avoid future surprising bugs. 最好选择其他一些变量名,以避免将来出现意外错误。 I read and write list comprehensions backwards: 我向后读写列表理解:
[ expression # (3)
for variable in # (2)
iterable # (1)
]
(1): First, understand the iterable. (1):首先,了解迭代。 In the solution above, this is text.split('|')
. 在上面的解决方案中,这是text.split('|')
。
(2): for variable in
causes variable
to be assigned to the values in iterable
, one at a time. (2): for variable in
variable
,将variable
一次赋给iterable
的值。
(3): Finally, expression
can be any Python expression, (usually) using variable
. (3):最后, expression
可以是任何Python表达式,(通常)使用variable
。
The syntax for generator expressions is almost the same. 生成器表达式的语法几乎相同。 The difference between a list comprehension and a generator expression is that a list comprehension returns a list, while a generator expression returns an iterator -- an object that yields its contents on-demand (as it is looped over, or when next
is called) instead of generating all the items at once as is the case with list
s. 列表推导和生成器表达式之间的区别在于,列表推导返回一个列表,而生成器表达式返回一个迭代器-一个对象,该对象按需产生其内容(在循环时或在调用next
时)而不是像list
那样一次生成所有项目。
A list can consume a lot of memory if the list is long. 如果列表很长,则列表可能会占用大量内存。 A generator expression will consume less memory (and can even be infinite) because not all elements have to exist in memory at the same time. 生成器表达式将消耗更少的内存(甚至可以是无限的),因为并非所有元素都必须同时存在于内存中。
Using str
as a variable name is a bad idea, since it overshadows the built-in str
. 使用str
作为变量名是一个坏主意,因为它使内置的str
黯然失色。
s ='ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:CL5'
res = [dict(colonStr.split(':') for colonStr in ds.split(','))
for ds in s.split('|')]
stores the result you what in res
. 将结果存储在res
。
The key insight here is that dict
can take a list of key/value pairs. 这里的关键见解是dict
可以列出键/值对的列表。 So, you can do this using a comprehension like this: 因此,您可以使用如下理解方法进行此操作:
string = 'ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:CL5'
list_of_dicts = [dict(item.split(':') for item in items.split(',')) for items in string.split('|')]
print list_of_dicts
Maybe this way: 也许这样:
>>> s = 'ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:CL5'
>>> x = [dict([d.split(':') for d in ls.split(',')]) for ls in s.split('|')]
>>> x[1]['building']
>>> 'CL5'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.