![](/img/trans.png)
[英]In Python, if you have two lists that share a number of elements, how do you create a new list with the matches?
[英]How do you fuse string items from two lists into new elements of a new list?
我正在使用一組數字作為字符串,我需要將它們放在一起,但不能加/減它們。 本質上,我正在與此:
a = ['12', '34', '56', '78']
b = ['78', '56', '34', '12']
我需要:
c = ['1278', '3456', 5634', '7812']
考慮到您提到的上述情況,您可以按照以下步驟簡單地進行此操作(假設兩個輸入數組的長度相同):
for (int i=0; i<a.length; i++)
{
c[i]=(a[i]*100)+b[i];
}
注意:這僅適用於您提到的示例案例。
壓縮兩個列表,然后將每個元素中的兩個字符串連接起來:
c = [x + y for x, y in zip(a, b)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.