python在list相加 求助
高洛峰
高洛峰 2017-04-18 10:24:28
0
4
583

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....]

求大神指导~

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
阿神

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...

Ty80
[x+y for y in b for x in a]
阿神
c = list(map(lambda i: a[i] + b[i], range(len(a)))
PHPzhong
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)]
>>>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!