是什么让你认为你不能使用 for 循环?
flight_lst = [
['>KOR1231','Seoul','HKU', '1992/3/21'],
['>KOR9743','Paris','CDG', '1990/1/12']
]
for flight in flight_lst:
flight[0] = flight[0][1:]
print(flight_lst)
# [['KOR1231', 'Seoul', 'HKU', '1992/3/21'],
# ['KOR9743', 'Paris', 'CDG', '1990/1/12']]
或者,如果您想在不修改旧列表的情况下生成新列表:
old_lst = [
['>KOR1231','Seoul','HKU', '1992/3/21'],
['>KOR9743','Paris','CDG', '1990/1/12']
]
new_lst = [[flight[0][1:]] + flight[1:] for flight in old_lst]
print(new_lst)
# [['KOR1231', 'Seoul', 'HKU', '1992/3/21'],
# ['KOR9743', 'Paris', 'CDG', '1990/1/12']]
或者,如果您对扩星感到满意:
old_lst = [
['>KOR1231','Seoul','HKU', '1992/3/21'],
['>KOR9743','Paris','CDG', '1990/1/12']
]
new_lst = [[name[1:]] + remainder for name,*remainder in old_lst]
print(new_lst)
# [['KOR1231', 'Seoul', 'HKU', '1992/3/21'],
# ['KOR9743', 'Paris', 'CDG', '1990/1/12']]