从一个列表创建可变长度列表

时间:2017-06-03 23:31:28

标签: python json python-3.x

我想从单个列表中创建多个列表。如果有来自用户的数据,列表将具有1个值或3个。但是,由于数据附加到特定用户,因此必须维护该订单。

例如:

data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1', ...etc]

我希望输出为:

list1 = ['no data']
list2 = ['choice 1', 'choice4', 'choice 2']
list3 = ['no data']
list4 = ['choice 1'...]

我将如何实现这一目标?

2 个答案:

答案 0 :(得分:3)

不幸的是,itertools.groupby如果你需要将非'no-data'运行到3个组中,或者作为每个单独'no-data'的单个单元,就不会有效。所以,这是一个抛在一起的东西:

In [40]: def group_lists(data):
    ...:     final = []
    ...:     contains_data = False
    ...:     temp = []
    ...:     for sub in data:
    ...:         if sub == 'no data':
    ...:             if contains_data:
    ...:                 final.append(temp)
    ...:                 temp = []
    ...:                 final.append([sub])
    ...:                 contains_data = False
    ...:             else:
    ...:                 final.append([sub])
    ...:         else:
    ...:             contains_data = True
    ...:             if len(temp) < 3:
    ...:                 temp.append(sub)
    ...:             else:
    ...:                 final.append(temp)
    ...:                 temp = []
    ...:                 contains_data = False
    ...:     return final
    ...:

In [41]: data = ['choice 1', 'choice 2', 'choice 1', 'choice 1', 'choice 4', 'choice 1', 'no data']

In [42]: group_lists(data)
Out[42]: [['choice 1', 'choice 2', 'choice 1'], ['choice 4', 'choice 1'], ['no data']]

In [43]: data2 = ['choice 1', 'choice 2', 'choice 1', 'choice 1', 'choice 4', 'choice 1', 'choice1', 'no data']

In [44]: group_lists(data2)
Out[44]:
[['choice 1', 'choice 2', 'choice 1'],
 ['choice 4', 'choice 1', 'choice1'],
 ['no data']]

原始答案

使用itertools.groupby

In [15]: data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1']
    ...:

In [16]: import itertools

In [17]: grouped = [list(g) for _, g in itertools.groupby(data, lambda s: s == 'no data')]

In [18]: grouped[0]
Out[18]: ['no data']

In [19]: grouped[1]
Out[19]: ['choice 1', 'choice 4', 'choice 2']

In [20]: grouped[2]
Out[20]: ['no data']

In [21]: grouped[3]
Out[21]: ['choice 1']

打开那个相当笨重的单行包装:

In [26]: lists = []
    ...: for _, g in itertools.groupby(data, lambda s: s == 'no data'):
    ...:     lists.append(list(g))
    ...:

In [27]: lists[0]
Out[27]: ['no data']

In [28]: lists[1]
Out[28]: ['choice 1', 'choice 4', 'choice 2']

In [29]: lists[2]
Out[29]: ['no data']

In [30]: lists[3]
Out[30]: ['choice 1']

因此,结果是列表清单:

In [32]: lists
Out[32]: [['no data'], ['choice 1', 'choice 4', 'choice 2'], ['no data'], ['choice 1']]

答案 1 :(得分:0)

这似乎有效,而且相当容易理解。它不会创建单独的变量,而是创建一个名为lists的列表列表。

data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1', '...etc']

lists = []
tmp = []
for elem in data:
    if elem != 'no data':
        tmp.append(elem)
    else:
        if tmp:
            lists.append(tmp)
        lists.append([elem])
        tmp = []

if tmp:
    lists.append(tmp)

for i, sublist in enumerate(lists, start=1):
    print('list{}: {}'.format(i, sublist))

输出:

list1: ['no data']
list2: ['choice 1', 'choice 4', 'choice 2']
list3: ['no data']
list4: ['choice 1', '...etc']