list根據另外乙個list進行排序

2021-10-03 03:14:19 字數 944 閱讀 4466

現在有兩個list物件,ba的子集:

a =

['市轄區'

,'文峰區'

,'北關區'

,'殷都区'

,'龍安區'

,'安陽縣'

,'湯陰縣'

,'內黃縣'

,'林州市'

,'高新區'

]b =

['內黃縣'

,'北關區'

,'安陽縣'

,'文峰區'

,'林州市'

,'殷都区'

,'湯陰縣'

,'高新區'

,'龍安區'

]

想要讓b依照a中的順序進行重新排序

分步實現

# 先生成b中每個元素在a中位置的序列

c =[a.index(x)

for x in b]

# [7, 2, 5, 1, 8, 3, 6, 9, 4]

# 使用zip()將位置序列與原序列打包成元組物件後,再使用sorted() 函式對所有可迭代的物件進行排序操作得到序列

b =[x for _,x in

sorted

(zip

(c,b))]

# ['文峰區', '北關區', '殷都区', '龍安區', '安陽縣', '湯陰縣', '內黃縣', '林州市', '高新區']

一行實現

b =

[x for _,x in

sorted

(zip

([a.index(x)

for x in b]

,b))

]

sql中傳入乙個list,返回乙個list

傳入陣列 返回list string sendpersonidarr sendpersonid.split listlist staffinfoservice.listphonesbyids sendpersonidarr select h.telphone from hr staff info h...

兩個List巢狀Map,如何實現乙個給另外乙個賦值

上個公司的同事有個關於頁面運算元據庫的需求,涉及到這一塊,問了我下如何實現這個功能,在此做個筆記。下面展示一些內聯 片。list list1 newarraylist map map1 newhashmap map1.put 1 1 null map1.put 1 2 null map map2 n...

自己寫乙個LIST

pragma once forward declarations templateclass clistnode templateclass clist template class clistnode void insertafter t data template void clistnode ...