为了账号安全,请及时绑定邮箱和手机立即绑定

在python中使用x和y坐标排序字典

在python中使用x和y坐标排序字典

料青山看我应如是 2022-06-28 17:18:29
我有这个问题。我需要订购这点 1-7 1(4,2), 2(3, 5), 3(1,4), 4(1,1), 5(2,2), 6(1,3), 7(1,5)并得到这个结果 4 , 6 , 3 , 5 , 2 , 1 , 7。我正在使用 python 脚本进行带有 x 引用的排序并且没问题,但是 y 中的排序是错误的。我试过 sorted(dicts,key=itemgetter(1,2))有人可以帮助我吗?
查看完整描述

3 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

尝试这个:

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

python中的索引从0开始。itemgetter(1,0)按第二个元素排序,然后按第一个元素排序


查看完整回答
反对 回复 2022-06-28
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

由于您是从上到下,然后从左到右进行视觉搜索,因此此代码要简单得多并提供正确的结果。它基本上相当于视觉扫描,通过检查每个“y=n”位置的所有元组,然后根据第二个数字(从左到右)对任何“y=n”元组进行排序。


为了与笛卡尔数系统更加一致,我已将图表上的点转换为 (x,y) 坐标,其中 X 为正(向右增加)和 y 为负(随着它们下降而减少)。


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]


查看完整回答
反对 回复 2022-06-28
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

这基于对元组的第一个坐标进行排序,然后按元组的第二个坐标对代码进行排序。即按字母顺序,“Aa”,然后是“Ab”,然后是“Ba”,然后是“Bb”。更字面意思是 (1,1)、(1,2)、(2,1)、(2,2) 等。


如果(且仅当)与 #7 关联的元组值对在您的问题中实际上是乱序的(并且实际上应该在 #3 和 #5 之间),这将起作用。


如果不是这种情况,请参阅我的其他答案。


# 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]         


查看完整回答
反对 回复 2022-06-28
  • 3 回答
  • 0 关注
  • 322 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信