开发者

Python: create a list containing dicts

how would you turn this string:

str='ldap:alberthwang,eeid:67739|ldap:meng,eeid:107,building:开发者_JAVA技巧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
  1. List comprehensions are a very convenient way to construct lists such as this.
  2. A dict can be constructed from an iterable of key-value pairs. The iterable used above was a generator expression.
  3. str is a built-in type, so assigning a string to str overwrites the builtin. 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. In the solution above, this is text.split('|').

(2): for variable in causes variable to be assigned to the values in iterable, one at a time.

(3): Finally, expression can be any Python expression, (usually) using 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 lists.

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.

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.


The key insight here is that dict can take a list of key/value pairs. 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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜