无法在 Python 中正确合并列表 - 最后一个元素未合并
Can't Merge Lists properly in Python - last element isn't getting merged
有两个列表:
MAIN: ['Lists ', '\nThe first list', '\n 4. Empty', '\n 5. Orange', '\n 6. Lemon', '\n 7. 2 Tangerines']
FOR REPLACEMENT: ['The second list is', '\n3. Apple ', '\n4. Banana ', '\n5. Pear', '\n8. Milk']
如何在这段代码中写一个条件,使第8个元素不丢失?
a = ['Lists', 'The first list', '4. Empty', '5. Empty', '6. Lemon', '7. 2 Tangerines']
b = ['The second list is', '3. Apple ', '4. Banana ', '5. Pear', '8. Other']
w = []
for i in range(len(a)): # find all the unnumbered elements of the first list, add them to the final one in order
if a[i][0] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
w.append('0. ' + a[i])
else:
n = i
break
for i in range(len(b)): # find all the unnumbered elements of the second list, also add them to the final one in order
if b[i][0] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
w.append('0. ' + b[i])
else:
m = i
break
w += a[n:] # adding the numbered elements of the first list
for i in range(m, len(b)): # we check the numbered elements of the second list, do the necessary action(replace/add)
for j in range(len(w)):
if b[i][:b[i].index('.')] == w[j][:w[j].index('.')]:
w[j] = b[i]
break
elif int(b[i][:b[i].index('.')]) < int(w[j][:w[j].index('.')]):
w = w[:j] + [b[i]] + w[j:]
break
k = 0 # beautiful output
while w[k][0] == '0':
w[k] = w[k][3:]
k += 1
print('\n'.join(w))
需要用第二个列表中的元素替换第一个列表中的元素。
在第一个列表中,数字始终按顺序排列。在第二种情况下,这可能不是。如果第二个列表中有一个数字不在主列表中,则应分别将其插入顶部或底部。
我无法输出元素 8. 9. 10. 等等。简单地说,末尾的元素不会插入到主列表中。
预期结果:
Lists
The first list
The second list is
3. Apple
4. Banana
5. Pear
6. Lemon
7. 2 Tangerines
8. Other
试试这个:
a = ['Lists', 'The first list', '4. Empty', '5. Empty', '6. Lemon', '7. 2 Tangerines']
b = ['The second list is', '3. Apple ', '4. Banana ', '5. Pear', '8. Other']
x = sorted([
x for x in a+b
if x[0].isdigit() and x[3:] != 'Empty'
], key=lambda x: int(x[0]))
for x in x: print(x)
有两个列表:
MAIN: ['Lists ', '\nThe first list', '\n 4. Empty', '\n 5. Orange', '\n 6. Lemon', '\n 7. 2 Tangerines']
FOR REPLACEMENT: ['The second list is', '\n3. Apple ', '\n4. Banana ', '\n5. Pear', '\n8. Milk']
如何在这段代码中写一个条件,使第8个元素不丢失?
a = ['Lists', 'The first list', '4. Empty', '5. Empty', '6. Lemon', '7. 2 Tangerines']
b = ['The second list is', '3. Apple ', '4. Banana ', '5. Pear', '8. Other']
w = []
for i in range(len(a)): # find all the unnumbered elements of the first list, add them to the final one in order
if a[i][0] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
w.append('0. ' + a[i])
else:
n = i
break
for i in range(len(b)): # find all the unnumbered elements of the second list, also add them to the final one in order
if b[i][0] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9']:
w.append('0. ' + b[i])
else:
m = i
break
w += a[n:] # adding the numbered elements of the first list
for i in range(m, len(b)): # we check the numbered elements of the second list, do the necessary action(replace/add)
for j in range(len(w)):
if b[i][:b[i].index('.')] == w[j][:w[j].index('.')]:
w[j] = b[i]
break
elif int(b[i][:b[i].index('.')]) < int(w[j][:w[j].index('.')]):
w = w[:j] + [b[i]] + w[j:]
break
k = 0 # beautiful output
while w[k][0] == '0':
w[k] = w[k][3:]
k += 1
print('\n'.join(w))
需要用第二个列表中的元素替换第一个列表中的元素。
在第一个列表中,数字始终按顺序排列。在第二种情况下,这可能不是。如果第二个列表中有一个数字不在主列表中,则应分别将其插入顶部或底部。
我无法输出元素 8. 9. 10. 等等。简单地说,末尾的元素不会插入到主列表中。 预期结果:
Lists
The first list
The second list is
3. Apple
4. Banana
5. Pear
6. Lemon
7. 2 Tangerines
8. Other
试试这个:
a = ['Lists', 'The first list', '4. Empty', '5. Empty', '6. Lemon', '7. 2 Tangerines']
b = ['The second list is', '3. Apple ', '4. Banana ', '5. Pear', '8. Other']
x = sorted([
x for x in a+b
if x[0].isdigit() and x[3:] != 'Empty'
], key=lambda x: int(x[0]))
for x in x: print(x)