Reputation: 275
Just beginning with python and know enough to know I know nothing. I would like to find alternative ways of splitting a list into a list of dicts. Example list:
data = ['**adjective:**', 'nice', 'kind', 'fine',
'**noun:**', 'benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal',
'**adverb:**', 'well', 'nicely', 'fine', 'right', 'okay']
I would be able to get:
[{'**adjective**': ('nice', 'kind', 'fine'),
'**noun**': ('benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'),
'**adverb**': ('well', 'nicely', 'fine', 'right', 'okay')}]
Upvotes: 2
Views: 2133
Reputation:
If you assume inner to be the list of words you could have this as the code
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
This prints the following dictionary
{'adjective': ['nice', 'kind', 'fine'], 'noun': ['benefit', 'profit', 'advantage', 'avail', 'welfare', 'use', 'weal'], 'adverb': ['well', 'nicely', 'fine', 'right', 'okay']}
Upvotes: 0
Reputation: 601529
This might be as close at it gets to what you have asked:
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']})
Edit: Just for fun an alternative two-liner inspired by the answer by 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))
Upvotes: 10
Reputation: 27575
And if there were keys without elements of a list after them ? , I thought. So I added 'nada:' in front, 'nothing:' in the middle, and ’oops:’ at the end of the list named data.
Then, in these conditions, the code 1 (in following) with groupy appears to give a completely false result, the code 2 with defaultdict give a result in which keys 'nada:' , 'nothing:' , and ’oops:’ are absent. They are also less fast than the simplest solution (code 3: Cameron, user506710)
I had an idea => codes 4 and 5. Results are OK and executions are faster.
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
Upvotes: 0
Reputation: 319551
>>> 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']}
Upvotes: 5
Reputation: 56
The code below will give you a dictionary with one entry for each word with a colon after it.
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)
Upvotes: 1