2010-12-13 202 views
2

刚刚开始使用python,知道知道我什么都不知道。我想找到另一种方式将列表分成一系列的字典。例如列表:Python:数组中的分割列表

data = ['**adjective:**', 'nice', 'kind', 'fine', 
     '**noun:**', 'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal', 
     '**adverb:**', 'well', 'nicely', 'fine', 'right', 'okay'] 

我将能够获得:

[{'**adjective**': ('nice', 'kind', 'fine'), 
'**noun**': ('benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'), 
'**adverb**': ('well', 'nicely', 'fine', 'right', 'okay')}] 
+0

不可能得到像你发布的第二个列表/字典结构。它应该更像这样:'''''形容词':['nice','kind','fine'],'名词':['benefit','profit','advantage','avail', ''''''','''','''','副词':'好','好','好','右','好']}' – 2010-12-13 18:23:08

+0

列表是大多数语言称为数组的东西,PHP调用数组是数组和字典组合。在Python中没有'{key1:val1,val2,val3}'这样的薄弱环节。 – delnan 2010-12-13 18:23:35

+0

您的输出不太有效。你想要{'形容词':['nice','kind'],'名词':['benefit',profit',...]}? – kevpie 2010-12-13 18:24:31

回答

10

这可能是接近它到达你问:

d = collections.defaultdict(list) 
for s in data: 
    if s.endswith(":"): 
     key = s[:-1] 
    else: 
     d[key].append(s) 
print d 
# defaultdict(<type 'list'>, 
#  {'adjective': ['nice', 'kind', 'fine'], 
#  'noun': ['benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'], 
#  'adverb': ['well', 'nicely', 'fine', 'right', 'okay']}) 

编辑:只是为了好玩另一种双线型灵感来自SilentGhost的答案:

g = (list(v) for k, v in itertools.groupby(data, lambda x: x.endswith(':'))) 
d = dict((k[-1].rstrip(":"), v) for k, v in itertools.izip(g, g)) 
5
>>> data = ['adjective:', 'nice', 'kind', 'fine', 'noun:', 'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal', 'adverb:', 'well', 'nicely', 'fine', 'right', 'okay'] 
>>> from itertools import groupby 
>>> dic = {} 
>>> for i, j in groupby(data, key=lambda x: x.endswith(':')): 
    if i: 
     key = next(j).rstrip(':') 
     continue 
    dic[key] = list(j) 

>>> dic 
{'adjective': ['nice', 'kind', 'fine'], 'noun': ['benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'], 'adverb': ['well', 'nicely', 'fine', 'right', 'okay']} 
+0

该团队的+1由 – 2010-12-13 18:30:47

+0

+1年。 @SilentGhost,key = next(j).rstrip(':')? – kevpie 2010-12-13 18:35:38

+0

yups ... should'nt it rstrip .... – 2010-12-13 18:38:04

0

如果假定内是单词的列表,你可以有这样的代码

data = ['adjective:', 'nice', 'kind', 'fine', 'noun:', 'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal', 'adverb:', 'well', 'nicely', 'fine', 'right', 'okay'] 

dict = {} 

for x in data: 

    if x[-1] == ':' : 

     start = x.rstrip(':') 

     dict[start] = [] 

    else: 

     dict[start].append(x) 

print dict 

这将打印以下字典

{'adjective': ['nice', 'kind', 'fine'], 'noun': ['benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'], 'adverb': ['well', 'nicely', 'fine', 'right', 'okay']} 
1

下面的代码会给你一个字典每个单词后面都带有一个冒号。

data = ['adjective:', 'nice', 'kind', 'fine', 'noun:', 'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal', 'adverb:', 'well', 'nicely', 'fine', 'right', 'okay'] 
result = {} 
key = None 
for item in data: 
if item.endswith(":"): 
    key = item[:-1] 
    result[key] = [] 
    continue 
result[key].append(item) 
0

如果有钥匙没有列表中的元素后面呢? , 我想。 所以我在前面添加了'nada:','没有:'在中间,'oops:'在名为数据的列表的末尾。

然后,在这些条件下, 与groupy的代码1(在以下)出现,得到完全错误的结果, 代码2 defaultdict让在其中键的结果“纳达:”,“无:”,和'oops:'缺席。 它们也比简单的解决方案(代码3:卡梅伦,user506710)少快

我有一个想法=>码4和5 结果OK和处决更快。

from time import clock 

data = ['nada:', # <<<============= 
    'adjective:', 
    'nice', 'kind', 'fine', 
    'noun:', 
    'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal', 
    'nothing:', # <<<============= 
    'adverb:', 
    'well', 'nicely', 'fine', 'right', 'okay', 
    'oops:'  # <<<============= 
    ] 

#------------------------------------------------------------ 
from itertools import groupby 

te = clock() 
dic1 = {} 
for i, j in groupby(data, key=lambda x: x.endswith(':')): 
    if i: 
     key = next(j).rstrip(':') 
     continue 
    dic1[key] = list(j) 
print clock()-te,' groupby' 
print dic1,'\n' 

#------------------------------------------------------------ 
from collections import defaultdict 
te = clock() 
dic2 = defaultdict(list) 
for s in data: 
    if s.endswith(":"): 
     key = s[:-1] 
    else: 
     dic2[key].append(s) 
print clock()-te,' defaultdict' 
print dic2,'\n\n===================' 

#============================================================= 
te = clock() 
dic4 = {} 
for x in data: 
    if x[-1] == ':' : 
     start = x.rstrip(':') 
     dic4[start] = [] 
    else: 
    dic4[start].append(x) 
print clock() - te 
print dic4,'\n' 

#------------------------------------------------------------ 
te = clock() 
dic3 = {} 
der = len(data) 
for i,y in enumerate(data[::-1]): 
    if y[-1]==':': 
     dic3[y[0:-1]] = data[len(data)-i:der] 
     der = len(data)-i-1 
print clock()-te 
print dic3,'\n' 

    #------------------------------------------------------------ 
te = clock() 
dic5 = {} 
der = len(data) 
for i in xrange(der-1,-1,-1): 
    if data[i][-1]==':': 
     dic5[data[i][0:-1]] = data[i+1:der] 
     der = i 
print clock() - te 
print dic5 

print '\ndic3==dic4==dic5 is',dic3==dic4==dic5