a = [A,B,C,D,E,F]
b = [a,b,c,d,e,f]
如何变成
c = [A+a,B+a,C+a,E+a,D+a,F+a, A+a,B+b,C+b,D+b,E+b,F+b,A+c,B+c,C+c,D+c,E+c,F+c....]
求大神指导~
阿神2017-04-18 10:26:28
First, you have to enclose the letter elements in the List in single quotes, and then what you need to do is a list generation, a multi-level expression.
>>> a = ['A','B','C','D','E','F']
>>> b = ['a','b','c','d','e','f']
>>> [x+y for y in b for x in a]
['Aa', 'Ba', 'Ca', 'Da', 'Ea', 'Fa', 'Ab', 'Bb', 'Cb', 'Db', 'Eb', 'Fb', 'Ac', 'Bc', 'Cc', 'Dc', 'Ec', 'Fc', 'Ad', 'Bd', 'Cd', 'Dd', 'Ed', 'Fd', 'Ae', 'Be', 'Ce', 'De', 'Ee', 'Fe', 'Af', 'Bf', 'Cf', 'Df', 'Ef', 'Ff']
List generation can be used for operations and string concatenation
Here is a detailed introduction in the document https://docs.python.org/2/tut...
PHPz2017-04-18 10:26:28
from itertools import product
>>> a = ['a', 'b', 'c']
>>> b=[1, 2, 3]
>>> c = list(product(a, b))
>>> print(c)
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]
>>>