简体   繁体   English

将列表的子列表拆分为其他子列表

[英]Split sublist of a list into other sublists

I am having problems with 'splitting' a larger list into several of it's combinations. 我在将较大的列表“拆分”为多个组合时遇到问题。 Here is an example: 这是一个例子:

Let's say I have this list: 假设我有以下列表:

  x = [['a','b'],['c','f'],['q','w','t']]

and I want to end up with 我想结束

  x = [['a','b'],['c','f'],['q','w'],['q','t'],['w','t']]

so essentially 所以本质上

 ['q','w','t']

becomes 变成

 ['q','w'],['q','t'],['w','t']

I see how I can convert 我知道如何转换

 ['q','w','t']

to

 [['q','w'],['q','t'],['w','t']] #notice the extra brackets

with itertools combinations, but then I am stuck with 使用itertools组合,但后来我被困在

 x = [['a','b'],['c','f'],[['q','w'],['q','t'],['w','t']]] #notice the extra brackets

Which is not what I want. 这不是我想要的。

How should I do this? 我应该怎么做?

EDIT: 编辑:

Here is the "solution", that does not give me the result that I want: 这是“解决方案”,没有给我想要的结果:

from itertools import combinations

x = [['a','b'],['c','f'],['q','w','t']]

new_x = []

for sublist in x:
    if len(sublist) == 2:
        new_x.append(sublist)
    if len(sublist) > 2:
        new_x.append([list(ele) for ele in (combinations(sublist,2))])

Thank You 谢谢

I generally use a nested list comprehension to flatten a list like this: 我通常使用嵌套列表理解来扁平化这样的列表:

>>> x = [['a','b'],['c','f'],['q','w','t']]
>>> [c for s in x for c in itertools.combinations(s, 2)]
[('a', 'b'), ('c', 'f'), ('q', 'w'), ('q', 't'), ('w', 't')]

Not the best way to do it but pretty clear for understanding: 这不是最好的方法,但很容易理解:

from itertools import combinations

a = [['a','b'],['c','f'],['q','w','t']]

def get_new_list(x):

    newList = []
    for l in x:
        if len(l) > 2:
            newList.extend([list(sl) for sl  in combinations(l, 2)])
        else:
            newList.append(l)
    return newList

print get_new_list(a)
>>> [['a','b'],['c','f'],['q','w'],['q','t'],['w','t']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串列表拆分为len(“”。join(sublist))&lt;= N的子列表? - How to split list of strings into sublists where len(“”.join(sublist)) <= N? 将列表拆分为相等的块和 Append 最后一个子列表到上一个子列表 - Split List into Equal Chunks and Append Last sublist to Previous sublists Python在不更改列表中其他子列表索引的情况下删除列表中的子列表 - Python Removing a sublist in a list without changing the index of other sublists in the list 如何将列表分为相等的子列表,最后一个子列表由列表的第一个可能的元素组成 - How to split list into equal sublists with the last sublist made up from the first possible elements of the list 当子列表的大小取决于数据值(种子和扩展)时,将列表拆分为子列表 - Split a list into sublists, when size of sublist relies on data values (Seed and extend) 基于python中的子列表项将子列表合并到列表中 - Merging sublists into a list based on sublist item in python Python使用子列表拆分列表 - Python Split a list with sublists 生成具有A子列表和每个子列表中的B元素的列表 - Generating a List with A sublists and B elements in each sublist 如何将列表拆分为连续 6 个零和其他值的子列表 - How to split a list into sublists of 6 zeros in a row and other values 将排序后的数组拆分为带有子列表的列表 - Split sorted array into list with sublists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM