第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python中使用x和y坐標(biāo)排序字典

在python中使用x和y坐標(biāo)排序字典

我有這個(gè)問題。我需要訂購這點(diǎn) 1-7 1(4,2), 2(3, 5), 3(1,4), 4(1,1), 5(2,2), 6(1,3), 7(1,5)并得到這個(gè)結(jié)果 4 , 6 , 3 , 5 , 2 , 1 , 7。我正在使用 python 腳本進(jìn)行帶有 x 引用的排序并且沒問題,但是 y 中的排序是錯(cuò)誤的。我試過 sorted(dicts,key=itemgetter(1,2))有人可以幫助我嗎?
查看完整描述

3 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

嘗試這個(gè):

sorted(dicts,key=itemgetter(1,0))

python中的索引從0開始。itemgetter(1,0)按第二個(gè)元素排序,然后按第一個(gè)元素排序


查看完整回答
反對(duì) 回復(fù) 2022-06-28
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

由于您是從上到下,然后從左到右進(jìn)行視覺搜索,因此此代碼要簡單得多并提供正確的結(jié)果。它基本上相當(dāng)于視覺掃描,通過檢查每個(gè)“y=n”位置的所有元組,然后根據(jù)第二個(gè)數(shù)字(從左到右)對(duì)任何“y=n”元組進(jìn)行排序。


為了與笛卡爾數(shù)系統(tǒng)更加一致,我已將圖表上的點(diǎn)轉(zhuǎn)換為 (x,y) 坐標(biāo),其中 X 為正(向右增加)和 y 為負(fù)(隨著它們下降而減少)。


d = {(2,-4):1, (5,-3):2, (4,-1):3, (1,-1):4, (2,-2):5, (3,-1):6, (1,-5):7}

l = [(2,-4), (5,-3), (4,-1), (1,-1), (2,-2), (3,-1), (1,-5)]


results = []

# Use the length of the list. Its more than needed, but guarantees enough loops

for y in range(0, -len(l), -1):

    # For ONLY the items found at the specified y coordinate 

    temp_list = []

    for i in l: # Loop through ALL the items in the list

        if i[1] == y: # If tuple is at this "y" coordinate then...

            temp_list.append(i) # ... append it to the temp list

    # Now sort the list based on the "x" position of the coordinate

    temp_list = sorted(temp_list, key=lambda x: x[0])

    results += temp_list # And just append it to the final result list

# Final TUPLES in order

print(results)

# If you need them correlated to their original numbers

by_designator_num = []

for i in results: # The the first tupele value

    by_designator_num.append(d[i]) # Use the tuple value as the key, to get the original designator number from the original "d" dictionary

print(by_designator_num)

或者如果你想要它更快更緊湊


d = {(2,-4):1, (5,-3):2, (4,-1):3, (1,-1):4, (2,-2):5, (3,-1):6, (1,-5):7}

l = [(2,-4), (5,-3), (4,-1), (1,-1), (2,-2), (3,-1), (1,-5)]


results = []

for y in range(0, -len(l), -1):

    results += sorted([i for i in l if i[1] == y ], key=lambda x: x[0])

print(results)


by_designator_num = [d[i] for i in results]

print(by_designator_num)

輸出:


[(1, -1), (3, -1), (4, -1), (2, -2), (5, -3), (2, -4), (1, -5)]

[4, 6, 3, 5, 2, 1, 7]


查看完整回答
反對(duì) 回復(fù) 2022-06-28
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

這基于對(duì)元組的第一個(gè)坐標(biāo)進(jìn)行排序,然后按元組的第二個(gè)坐標(biāo)對(duì)代碼進(jìn)行排序。即按字母順序,“Aa”,然后是“Ab”,然后是“Ba”,然后是“Bb”。更字面意思是 (1,1)、(1,2)、(2,1)、(2,2) 等。


如果(且僅當(dāng))與 #7 關(guān)聯(lián)的元組值對(duì)在您的問題中實(shí)際上是亂序的(并且實(shí)際上應(yīng)該在 #3 和 #5 之間),這將起作用。


如果不是這種情況,請(qǐng)參閱我的其他答案。


# Make it a dictionary, with the VALUETUPLES  as the KEYS, and the designator as the value

d = {(1,1):4, (1,3):6, (1,4):3, (2,2):5, (3,5):2, (4,2):1,(1,5):7}

# ALSO make a list of just the value tuples

l = [ (1,1), (1,3), (1,4), (2,2), (3,5), (4,2), (1,5)]


# Sort the list by the first element in each tuple. ignoring the second

new = sorted(l, key=lambda x: x[0])


# Create a new dictionary, basically for temp sorting

new_d = {}

# This iterates through the first sorted list "new"

# and creates a dictionary where the key is the first number of value tuples

count = 0

# The extended range is because we don't know if any of the Tuple Values share any same numbers

for r in range(0, len(new)+1,1): 

    count += 1

    new_d[r] = []

    for item in new:

        if item[0] == r:

            new_d[r].append(item)


print(new_d) # So it makes sense


# Make a final list to capture the rdered TUPLES VALUES

final_list = []

# Go through the same rage as above

for r in range(0, len(new)+1,1):

    _list = new_d[r] # Grab the first list item from the dic. Order does not matter here

    if len(_list) > 0: # If the list has any values...

        # Sort that list now by the SECOND tuple value

        _list = sorted(_list, key=lambda x: x[1])

        # Lists are ordered. So we can now just tack that ordered list onto the final list. 

        # The order remains

        for item in _list: 

            final_list.append(item)


# This is all the tuple values in order

print(final_list)

# If you need them correlated to their original numbers

by_designator_num = []

for i in final_list: # The the first tupele value

    by_designator_num.append(d[i]) # Use the tuple value as the key, to get the original designator number from the original "d" dictionary


print(by_designator_num)

輸出:


[(1, 1), (1, 3), (1, 4), (1, 5), (2, 2), (3, 5), (4, 2)]

[4, 6, 3, 7, 5, 2, 1]         


查看完整回答
反對(duì) 回復(fù) 2022-06-28
  • 3 回答
  • 0 關(guān)注
  • 372 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)