如何通过重复元素划分列表

时间:2015-04-16 11:06:20

标签: python list

如何按重复元素n#39;模型'

拆分列表
list1=[{u'model': (u'AAAA', '')}, {u'des': (u'xx', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}, {u'model': (u'BBBB', '')}, {u'des': (u'yy', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}
for i, dictElem in enumerate (list1):
    print i, dictElem

0 {u'model': (u'AAAA', '')}
1 {u'des': (u'xx', '')}
2 {u'select': (u'1331', u'1')}
3 {u'select': (u'1332', u'2')}
4 {u'derived': (u'3444', u'2')}
5 {u'derived': (u'3445', u'1')}
6 {u'end': ('', '')}
7 {u'model': (u'BBBB', '')}
8 {u'des': (u'yy', '')}
9 {u'select': (u'1331', u'1')}
10 {u'select': (u'1332', u'2')}
11 {u'derived': (u'3444', u'2')}
12 {u'derived': (u'3445', u'1')}
13 {u'end': ('', '')}

我想获得一个新的list1,如下所示:

newlist1=[
[{u'model': (u'AAAA', '')}, {u'des': (u'xx', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}], 
[ {u'model': (u'BBBB', '')}, {u'des': (u'yy', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}]
]

“model”“end”的元素将作为新元素

3 个答案:

答案 0 :(得分:2)

稍短

def reductor(acc, item):
    if 'model' in item:
        acc.append([])
    acc[-1].append( item )
    return acc

listoflists = reduce(reductor, list1, [])

list1

list1=[{u'model': (u'AAAA', '')}, ..., {u'end': ('', '')}, {u'model': (u'BBBB', '')}, ...]

for l in listoflists:
    print "Found new list:\n",l

输出:

Found new list:
[{u'model': (u'AAAA', '')}, {u'des': (u'xx', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}]
Found new list:
[{u'model': (u'BBBB', '')}, {u'des': (u'yy', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}]

答案 1 :(得分:1)

这可以做到,我用你的清单测试了它。 它有点混乱,但有时混乱的字典/列表可能会很麻烦

dicts = []
list1=[{u'model': (u'AAAA', '')}, {u'des': (u'xx', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}, {u'model': (u'BBBB', '')}, {u'des': (u'yy', '')}, {u'select': (u'1331', u'1')}, {u'select': (u'1332', u'2')}, {u'derived': (u'3444', u'2')}, {u'derived': (u'3445', u'1')}, {u'end': ('', '')}]

index = -1
for dict in list1:
    for key, value in (dict.iteritems():
        if key == u'model':
            index += 1
            dicts.append([])
        dicts[index].append({})
        dicts[index][len(dicts[index])-1][key] = value

for dict in dicts:
    print(dict)

编辑: 当您遍历列表时,您将获得每个索引的dict。每次dict的键是u'model'时,你创建一个新列表,然后将这个词典和下一个词存储在新列表中。你没有必要。

答案 2 :(得分:1)

这是一种简单的方法。它依赖于哨兵“结束”词典的存在,例如, {u'end': ('', '')}这似乎是对所显示数据做出的有效假设。

list1 = [{u'model': (u'AAAA', '')},
         {u'des': (u'xx', '')},
         {u'select': (u'1331', u'1')},
         {u'select': (u'1332', u'2')},
         {u'derived': (u'3444', u'2')},
         {u'derived': (u'3445', u'1')},
         {u'end': ('', '')},
         {u'model': (u'BBBB', '')},
         {u'des': (u'yy', '')},
         {u'select': (u'1331', u'1')},
         {u'select': (u'1332', u'2')},
         {u'derived': (u'3444', u'2')},
         {u'derived': (u'3445', u'1')},
         {u'end': ('', '')}]

start = 0
newlist1 = []
for i, d in enumerate(list1):
    if 'model' in d:
        start = i
    elif 'end' in d:
        newlist1.append(list1[start:i+1])

>>> from pprint import pprint
>>> pprint(newlist1)
[[{u'model': (u'AAAA', '')},
  {u'des': (u'xx', '')},
  {u'select': (u'1331', u'1')},
  {u'select': (u'1332', u'2')},
  {u'derived': (u'3444', u'2')},
  {u'derived': (u'3445', u'1')},
  {u'end': ('', '')}],
 [{u'model': (u'BBBB', '')},
  {u'des': (u'yy', '')},
  {u'select': (u'1331', u'1')},
  {u'select': (u'1332', u'2')},
  {u'derived': (u'3444', u'2')},
  {u'derived': (u'3445', u'1')},
  {u'end': ('', '')}]]