我想用Python 3來使用list_one對list_two進行排序並輸出到list_sorted。如果字段的值丟失,那麼我希望看到一個空值。輸出將具有相同數量的項目作爲list_one使用一個列表來排序另一個
list_one = ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
list_two = ['seven', 'five', 'four', 'three', 'one']
list_sorted = []
for i in list_one:
for field in list_two:
if i == field:
list_sorted.append(field)
else:
list_sorted.append(None)
print ('final output:', list_sorted)
所需的輸出是:
['one', None, 'three', 'four', 'five', None, 'seven']
但它的實際輸出:
[None, None, None, None, 'one', None, None, None, None, None, None, None, None, 'three', None, None, None, 'four', None, None, None, 'five', None, None, None, None, None, None, None, None, 'seven', None, None, None, None]
我現在的想法是答案涉及enumerate但我不確定。任何幫助將大大讚賞。
看起來並不像你正在排序的東西。 –