简体   繁体   English

如何将两个列表中的字符串项融合到新列表的新元素中?

[英]How do you fuse string items from two lists into new elements of a new list?

I am working with a set of numbers as strings, and I need to put them together but NOT add/subtract them. 我正在使用一组数字作为字符串,我需要将它们放在一起,但不能加/减它们。 Essentially I am working with this: 本质上,我正在与此:

a = ['12', '34', '56', '78']
b = ['78', '56', '34', '12']

And I need: 我需要:

c = ['1278', '3456', 5634', '7812']

Considering the above case you mentioned, you can simply do this as follows(assuming both the input arrays are of same length): 考虑到您提到的上述情况,您可以按照以下步骤简单地进行此操作(假设两个输入数组的长度相同):

for (int i=0; i<a.length; i++)
{
    c[i]=(a[i]*100)+b[i];
}

Note: This is only for the example case you mentioned. 注意:这仅适用于您提到的示例案例。

压缩两个列表,然后将每个元素中的两个字符串连接起来:

c = [x + y for x, y in zip(a, b)]

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

相关问题 在 Python 中,如果您有两个共享多个元素的列表,您如何创建具有匹配项的新列表? - In Python, if you have two lists that share a number of elements, how do you create a new list with the matches? 如何比较两个列表中的项目并根据结果创建一个新列表? - How to compare items in two lists and create a new list out of results? 使用python从两个列表中将元素分组到新列表中 - grouping elements into new list from two lists using python 如何合并两个列表并将它们作为新列表中的元组返回? - How do you merge two lists and return them as a tuple in a new list? 将两个列表的相应元素合并到新列表 - Merging corresponding elements of two lists to a new list 比较两个列表中的元素并创建一个新列表 - Compare elements in two lists and make a new List 将两个列表的总和作为新列表中的元素返回 - Return summation of two lists as elements in a new list 如何将列表作为新项目加入列表字典-python? - How do you join a list to a dictionary of lists as a new item - python? 如何从现有列表列表中创建第一个元素的新列表 - How to make a new list of first elements from existing list of lists 如何从两个列表创建元组,以便重复一个列表元素 - How do you create Tuples from two lists so that one list elements repeat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM