Наложение списков
Есть списки a и b одинакового размера. Нужно наложить их друг на друга так, чтобы получился список c такого же размера, каждый элемент которого - комбинация элементов a и b, стоящих на том же месте. Например:
a = [
'color', 'blue', 'red',
'black', 'color', 'yellow',
'green', 'color', 'gray'
]
b = [
'', '', '1',
'', '', '2',
'', '', '3'
]
Результат:
c = [
'color', 'blue', '1ed',
'black', 'color', '2ellow',
'green', 'color', '3ray'
]
Ответы (3 шт):
Автор решения: Evgenii Evstafev
→ Ссылка
a = ['color', 'blue', 'red',
'black', 'color', 'yelow',
'gren', 'color', 'gray']
b = ['', '', '1',
'', '', '2',
'', '', '3']
c = []
for a_item, b_item in zip(a, b):
if b_item:
c.append(b_item + a_item[len(b_item):])
else:
c.append(a_item)
print(c) # >> ['color', 'blue', '1ed', 'black', 'color', '2elow', 'gren', 'color', '3ray']
Автор решения: u111
→ Ссылка
a = [
'color', 'blue', 'red',
'black', 'color', 'yellow',
'green', 'color', 'gray'
]
b = [
'', '', '1',
'', '', '2',
'', '', '3'
]
c = []
list_length = len(a)
for i in range(list_length):
string1 = a[i]
string2 = b[i]
result_string = string2 + string1[len(string2):]
c.append(result_string)
print(c)
Автор решения: CrazyElf
→ Ссылка
Ну, тут можно в одну строку, используя списковые сокращения и срезы:
c = [f'{y}{x[len(y):]}' for x, y in zip(a, b)]