# python 列表操作

-2

list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]

list3 = [1,2,45,3,4,49,5,6, 62, 7,8, 66,9,10, 79]

ojojk | 初学一级 | 园豆：102

0

``````list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]
list3 = []
list3Length = len(list1) + len(list2)
for x in range(list3Length):
if x % 3 != 2:
if len(list1) > 0:
list3.append(list1.pop(0))
else:
if len(list2) > 0:
list3.append(list2.pop(0))

print(list3)
``````

0
``````list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]
list3 =[]
for i, j in zip([list1[i: i+2] for i in range(0, len(list1), 2)], list2):
list3 += i
list3 += [j]
``````
Sunny-King | 园豆：363 (菜鸟二级) | 2023-06-07 17:07
0

from itertools import zip_longest

list1 = [1,2,3,4,5,6,7,8,9,10]
list2 = [45, 49, 62, 66, 79]

# 使用 zip_longest 将两个列表配对，没有配对到的元素填充为 None

pairs = zip_longest(list1, list2, fillvalue=None)

# 将配对的元素展开并过滤掉 None，得到最终的列表

list3 = [x for pair in pairs for x in pair if x is not None]

print(list3) # 输出：[1, 2, 45, 3, 4, 49, 5, 6, 62, 7, 8, 66, 9, 10, 79]

0

python
Copy code
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [45, 49, 62, 66, 79]

list3 = []
min_len = min(len(list1), len(list2))

for i in range(min_len):
list3.append(list1[i])
list3.append(list2[i])

# 处理剩余的列表元素

if len(list1) > len(list2):
list3.extend(list1[min_len:])
else:
list3.extend(list2[min_len:])

print(list3)

csharp
Copy code
[1, 2, 45, 3, 4, 49, 5, 6, 62, 7, 8, 66, 9, 10, 79]

Technologyforgood | 园豆：6772 (大侠五级) | 2023-06-07 22:14
0
``````list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]

list3 = []
i = 0
j = 0

while i < len(list1) or j < len(list2):
if i < len(list1):
list3.append(list1[i])
i += 1
if j < len(list2):
list3.append(list2[j])
j += 1

print(list3)
``````

0

LiveCoding | 园豆：497 (菜鸟二级) | 2023-06-10 15:47
0
``````你可以使用 zip 函数和列表推导式来实现：

list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]

list3 = [x for pair in zip(list1, list2) for x in pair] + list1[len(list2):]

``````
ycyzharry | 园豆：25673 (高人七级) | 2023-06-11 03:29
0

``````list1= [1,2,3,4,5,6,7,8,9,10]
list2= [45, 49, 62, 66, 79]

# 向 list1 中插入 list2 中的元素
for i, num in enumerate(list2):
list1.insert(i*2+2, num)

# 将 list1 拆分为两个列表
list3a = list1[:5]
list3b = list1[5:]

# 合并两个列表
list3 = [item for items in zip(list3a, list3b) for item in items]
list3 += list3b[len(list3a):]

print(list3)
``````

``````[1, 2, 45, 3, 4, 49, 5, 6, 62, 7, 8, 66, 9, 10, 79]
``````

您需要登录以后才能回答，未注册用户请先注册