Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用第二列对二维数组进行排序,如果第二列中的元素相同,则按第一列进行排序_Python_Python 3.x_Sorting_Optimization_Multidimensional Array - Fatal编程技术网

Python 使用第二列对二维数组进行排序,如果第二列中的元素相同,则按第一列进行排序

Python 使用第二列对二维数组进行排序,如果第二列中的元素相同,则按第一列进行排序,python,python-3.x,sorting,optimization,multidimensional-array,Python,Python 3.x,Sorting,Optimization,Multidimensional Array,我编写了一个python程序,使用第二列对二维数组进行排序,如果第二列中的元素与第一列的排序相同,则使用第二列中的元素进行排序。虽然我用基本的python知识解决了这个问题。 我认为这是可以改进的。有人能帮助优化它吗? 还请建议是否使用其他数据类型进行排序将是一个好的选择 #created a two dimensional array two_dim_array=[[2, 5], [9, 1], [4, 8], [10, 0], [50, 32], [33, 31],[1, 5], [12,

我编写了一个python程序,使用第二列对二维数组进行排序,如果第二列中的元素与第一列的排序相同,则使用第二列中的元素进行排序。虽然我用基本的python知识解决了这个问题。 我认为这是可以改进的。有人能帮助优化它吗? 还请建议是否使用其他数据类型进行排序将是一个好的选择

#created a two dimensional array
two_dim_array=[[2, 5], [9, 1], [4, 8], [10, 0], [50, 32], [33, 31],[1, 5], [12, 5], [22, 5], [32, 5], [9, 5],[3, 31], [91, 32] ] 
#get the length of the array
n_ship=len(two_dim_array)
#sorting two dimensional array by using second column
sort_by_second_column=sorted(two_dim_array, key=lambda x: x[1], reverse=False)
#declared new variable for storing second soeted array
new_final_data=[]
#parameter used to slice the two dimensional column  
first_slice=0
    #tmp=[]
index=[0]
for m in range(1, n_ship):
        #print('m is: '+str(m)+'final_data[m-1][1] is: '+str(final_data[m-1][1])+'final_data[m][1] is: '+str(final_data[m][1]))
  #subtracting second column elements to detect  changes and saved to array
   if(abs(sort_by_second_column[m-1][1]-sort_by_second_column[m][1])!=0):
        index.append(m)
        # print(index)
        l=1
# used the above generated index to slice the data
for z in range(len(index)):
    tmp=[]
    if(l==1):
        first_slice=0
        last=index[z+1]
        mid_start=index[z]
        # print('l is start'+ 'first is '+str(first_slice)+'last is'+str(last))
        v=sort_by_second_column[:last]

    elif l==len(index):
        first_slice=index[z]
        # print('l is last'+str(1)+ 'first is '+str(first_slice)+'last is'+str(last))
        v=sort_by_second_column[first_slice:]
    else:
        first_slice=index[z]
        last=index[z+1]
        #print('l is middle'+str(1)+ 'first is '+str(first_slice)+'last is'+str(last))
        v=sort_by_second_column[first_slice:last]


    tmp.extend(v)
    tmp=sorted(tmp, key=lambda x: x[0], reverse=False)
        #print(tmp)
    new_final_data.extend(tmp)
       # print(new_final_data)
    l+=1


for l in range(n_ship):
        print(str(new_final_data[l][0])+' '+str(new_final_data[l][1]))


''' Input
    2 5
    9 1
    4 8
    10 0
    50 32
    33 31
    1 5
    12 5
    22 5
    32 5
    9 5
    3 31
    91 32

    Output
    10 0
    9 1
    1 5
    2 5
    9 5
    12 5
    22 5
    32 5
    4 8
    3 31
    33 31
    50 32
    91 32'''

您应该阅读
sorted()
上的文档,因为这正是您需要使用的:

产出:

[10, 0]
[9, 1]
[1, 5]
[2, 5]
[9, 5]
[12, 5]
[22, 5]
[32, 5]
[4, 8]
[3, 31]
[33, 31]
[50, 32]
[91, 32]

@谢谢你,特雷泽维尔。我将根据您的建议进行处理。您还可以使用
itemgetter(1,0)
作为键,这实际上与lambda相同。
[10, 0]
[9, 1]
[1, 5]
[2, 5]
[9, 5]
[12, 5]
[22, 5]
[32, 5]
[4, 8]
[3, 31]
[33, 31]
[50, 32]
[91, 32]