Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否基于行将值从一个列表追加到另一个列表?_Python_String_List_Integer_Append - Fatal编程技术网

Python 是否基于行将值从一个列表追加到另一个列表?

Python 是否基于行将值从一个列表追加到另一个列表?,python,string,list,integer,append,Python,String,List,Integer,Append,我对python非常陌生,我想将一个列表中的每一行连接到python中另一个列表中的另一行。我想把列表中的整数放在字符串列表中每个字符串的前面。例如: int = [1, 2, 3] string = ['a', 'b', 'c'] final product: [1'a', 2'b', 3'c'] 我该怎么做呢?假设您想要字符串连接行为,您可以在列表中使用zip >>> nums = [1, 2, 3] >>> letters = ['a', 'b',

我对python非常陌生,我想将一个列表中的每一行连接到python中另一个列表中的另一行。我想把列表中的整数放在字符串列表中每个字符串的前面。例如:

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

final product: [1'a', 2'b', 3'c']

我该怎么做呢?

假设您想要字符串连接行为,您可以在列表中使用zip

>>> nums = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> [str(i) + j for i, j in zip(nums, letters)]
['1a', '2b', '3c']

你可以这样解决这个问题

final_product = []

for i in range(len(string)):
    final_product.append(zip(nums[i], string[i] ))

看看zipzip是实现这一点的正确方法,但值得注意的是,您的输出不是有效的python。您需要[1,'a',…或[1a'`