简体   繁体   English

如何使用 Python 将两个 arrays 与特殊参数结合起来

[英]How can I combine two arrays with special parameters using Python

I have two arrays:我有两个 arrays:

a = ['a', 'b', 'c']
b = ['1', '2', '3']

how can I combine them in one array called let's say "c" and which have the following view:如何将它们组合在一个名为“c”的数组中,并具有以下视图:

c = ['a1', 'b2', 'c3']

you can use the the built-in functions map and zip with str.join :您可以将内置函数mapzipstr.join一起使用:

list(map(''.join, zip(a, b)))

output: output:

['a1', 'b2', 'c3']

zip will allow you to match the corresponding indexes: zip将允许您匹配相应的索引:

result = [''.join(x) for x in zip(a,b)]

You can also try something like this ( Other than using zip function )您也可以尝试这样的事情(除了使用 zip function

>>>[j + b[i] for i, j in enumerate(a)]
['a1', 'b2', 'c3']

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

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