简体   繁体   English

如何在Python中将列表中的每个项目附加到列表中的列表?

[英]How to append each item from a list to a list in a list in Python?

Sorry for the puzzling title. 抱歉,标题令人困惑。 For example, I have a list: [a, b, c, d] 例如,我有一个列表: [a, b, c, d]

I want to generate a ranked list with different combinations in this format: 我想以此格式生成具有不同组合的排名列表:

[a]
[b]
[c]
[d]
[a, b]
[a, c]
[a, d]
[a, b, c]
[a, b, d]
[a, b, c, d]

I'm having trouble in generating this list. 我在生成此列表时遇到了麻烦。 So far what I did first was generate each list by adding an element per iteration: 到目前为止,我首先要做的是通过每次迭代添加一个元素来生成每个列表:

[]
[a]
[a, b]
[a, b, c]

Then I generated the lengths of how the ranked list should look like: 然后,我生成了排名列表的长度:

[]
[]
[]
[]
[a]
[a]
[a]
[a, b]
[a, b]
[a, b, c]

Now I'm stuck from here. 现在我被困在这里。 Is there a library in Python that allows me to do this or can you only do this manually in code? Python中是否有一个允许我执行此操作的库,或者您只能在代码中手动执行此操作? The last thing I have to do is to do a one-to-one list appending from the original list that I generated at the top. 我要做的最后一件事是从我在顶部生成的原始列表中追加一个一对一的列表。

Here's the code of what I have attempted, assume original_list is the original list I made at the top and new_list is the list that I have generated right above this text: 这是我尝试过的代码,假设original_list是我在顶部new_list的原始列表,而new_list是我在new_list本上new_list的列表:

for x in range(0, len(original_list)):
    new_list[x].append(original_list[x])

This doesn't work apparently since it appends every item from original_list to the first 4 items in new_list . 这显然不起作用,因为它会将original_list每个项目附加到new_list的前4个项目中。

EDIT: The elements should be alphabetical with only the last element having different combinations with no repeating element since I'm attempting this on a list with 21 items. 编辑:元素应该是字母顺序的,只有最后一个元素具有不同的组合,没有重复的元素,因为我尝试在具有21个项目的列表上进行尝试。

Using the powerset recipe from itertools recipes , you could do: 使用itertools配方中的powerset配方,您可以执行以下操作:

from itertools import chain, combinations


def powerset(iterable):
    s = list(iterable)
    it = chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
    return map(list, (e for e in it if e))


result = sorted(powerset(['a', 'b', 'c', 'd']), key=lambda x: (len(x), ''.join(x)))
for s in result:
    print(s)

Output 输出量

['a']
['b']
['c']
['d']
['a', 'b']
['a', 'c']
['a', 'd']
['b', 'c']
['b', 'd']
['c', 'd']
['a', 'b', 'c']
['a', 'b', 'd']
['a', 'c', 'd']
['b', 'c', 'd']
['a', 'b', 'c', 'd']

UPDATE 更新

Given the updated requirements you could do: 鉴于已更新的要求,您可以执行以下操作:

lst = ['a', 'b', 'c', 'd']
length = len(lst)


def patterns(l):
    for i in range(length):
        for c in l[i:]:
            yield l[:i] + [c]


for pattern in sorted(patterns(lst), key=lambda x: (len(x), ''.join(x))):
    print(pattern)

Output 输出量

['a']
['b']
['c']
['d']
['a', 'b']
['a', 'c']
['a', 'd']
['a', 'b', 'c']
['a', 'b', 'd']
['a', 'b', 'c', 'd']

Use simple iteration through list appending required to a new list: 通过将新列表所需的列表追加操作简单迭代:

lst = ['a', 'b', 'c', 'd', 'e']

nlst = []
for i in range(len(lst)):
    for y in lst[i:]:
        nlst.append(lst[:i] + list(y))

for x in nlst:
    print(x)

# ['a']
# ['b']
# ['c']
# ['d']
# ['e']
# ['a', 'b']
# ['a', 'c']
# ['a', 'd']
# ['a', 'e']
# ['a', 'b', 'c']
# ['a', 'b', 'd']
# ['a', 'b', 'e']
# ['a', 'b', 'c', 'd']
# ['a', 'b', 'c', 'e']
# ['a', 'b', 'c', 'd', 'e']

Try this: 尝试这个:

from itertools import combinations

a = ['a', 'b', 'c', 'd']
result = [list(combinations(a,i))for i in range(1,len(a)+1)]

and for printing it like that: 为了这样打印:

for i in result:
    print(*list(i), sep='\n')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM