在python中列出与彼此元素映射

时间:2021-01-04 11:25:40

I have a list like this : ['A,'B','C'] and I want output like this

我有一个这样的列表:['A,'B','C'],我想要这样的输出

['AB','BC','CA']

How can I get this output . Any help appreciated . I am getting the input list as the iteration from a for loop. Thanks

我怎样才能得到这个输出。任何帮助赞赏。我将输入列表作为for循环的迭代。谢谢

3 个解决方案

#1


0  

If you don't care about the order between and within pairs:

如果您不关心对之间和对之间的顺序:

import itertools
list(map(''.join, itertools.combinations(['A', 'B', 'C', 'D'], 2)))
['AB', 'AC', 'AD', 'BC', 'BD', 'CD']

If you do care:

如果你关心:

L = list('ABXD')
N = len(L)
[L[i] + L[(i+d) % N] for d in range(1, N//2 + 1) for i in range(N if 2*d < N else d)]
['AB', 'BX', 'XD', 'DA', 'AX', 'BD']

To make the logic of this a bit more transparent here are the answers for [], ['A'], ['A', 'B'], ... ['A', 'B', 'C', 'D', 'E', 'F']:

为了使这个逻辑更透明,这里的答案是[],['A'],['A','B'],...... ['A','B','C', 'D','E','F']:

[]
[]
['AB']
['AB', 'BC', 'CA']
['AB', 'BC', 'CD', 'DA', 'AC', 'BD']
['AB', 'BC', 'CD', 'DE', 'EA', 'AC', 'BD', 'CE', 'DA', 'EB']
['AB', 'BC', 'CD', 'DE', 'EF', 'FA', 'AC', 'BD', 'CE', 'DF', 'EA', 'FB', 'AD', 'BE', 'CF']

#2


1  

Use zip with list comprehension, note lst[1:] + lst[:1] is used to roll the list by one element:

使用zip和list comprehension,注意lst [1:] + lst [:1]用于将列表滚动一个元素:

lst = ['A','B','C']
[x + y for x, y in zip(lst, lst[1:] + lst[:1])]
# ['AB', 'BC', 'CA']

#3


0  

fully lazy version using the great more-itertools package

完全懒惰的版本使用更多的itertools包

import itertools
from more_itertools import pairwise

def wrapped_pairwise(it):
    it = iter(it)
    try:
        element = next(it)
    except StopIteration:
        return iter(())
    return (a+b for a, b in pairwise(itertools.chain((element,), it, (element,))))

assert list(wrapped_pairwise("")) == []
assert list(wrapped_pairwise("A")) == ["AA"]
assert list(wrapped_pairwise("ABC")) == ["AB","BC","CA"]
assert list(wrapped_pairwise(["A","B","C"])) == ["AB","BC","CA"]

#1


0  

If you don't care about the order between and within pairs:

如果您不关心对之间和对之间的顺序:

import itertools
list(map(''.join, itertools.combinations(['A', 'B', 'C', 'D'], 2)))
['AB', 'AC', 'AD', 'BC', 'BD', 'CD']

If you do care:

如果你关心:

L = list('ABXD')
N = len(L)
[L[i] + L[(i+d) % N] for d in range(1, N//2 + 1) for i in range(N if 2*d < N else d)]
['AB', 'BX', 'XD', 'DA', 'AX', 'BD']

To make the logic of this a bit more transparent here are the answers for [], ['A'], ['A', 'B'], ... ['A', 'B', 'C', 'D', 'E', 'F']:

为了使这个逻辑更透明,这里的答案是[],['A'],['A','B'],...... ['A','B','C', 'D','E','F']:

[]
[]
['AB']
['AB', 'BC', 'CA']
['AB', 'BC', 'CD', 'DA', 'AC', 'BD']
['AB', 'BC', 'CD', 'DE', 'EA', 'AC', 'BD', 'CE', 'DA', 'EB']
['AB', 'BC', 'CD', 'DE', 'EF', 'FA', 'AC', 'BD', 'CE', 'DF', 'EA', 'FB', 'AD', 'BE', 'CF']

#2


1  

Use zip with list comprehension, note lst[1:] + lst[:1] is used to roll the list by one element:

使用zip和list comprehension,注意lst [1:] + lst [:1]用于将列表滚动一个元素:

lst = ['A','B','C']
[x + y for x, y in zip(lst, lst[1:] + lst[:1])]
# ['AB', 'BC', 'CA']

#3


0  

fully lazy version using the great more-itertools package

完全懒惰的版本使用更多的itertools包

import itertools
from more_itertools import pairwise

def wrapped_pairwise(it):
    it = iter(it)
    try:
        element = next(it)
    except StopIteration:
        return iter(())
    return (a+b for a, b in pairwise(itertools.chain((element,), it, (element,))))

assert list(wrapped_pairwise("")) == []
assert list(wrapped_pairwise("A")) == ["AA"]
assert list(wrapped_pairwise("ABC")) == ["AB","BC","CA"]
assert list(wrapped_pairwise(["A","B","C"])) == ["AB","BC","CA"]