Python 交叉排序题

简介: 在计蒜客遇到的一道题: 输入一行 k 个用空格分隔开的整数,依次为 n1, n2 … nk。请将所有下标不能被 3 但可以被 2 整除的数在这些数字原有的位置上进行升序排列,此外,将余下下标能被 3 整除的数在这些数字原有的位置上进行降序排列。

在计蒜客遇到的一道题:

输入一行 k 个用空格分隔开的整数,依次为 n1, n2 … nk。请将所有下标不能被 3 但可以被 2 整除的数在这些数字原有的位置上进行升序排列,此外,将余下下标能被 3 整除的数在这些数字原有的位置上进行降序排列。

输出包括一行,与输入相对应的若干个整数,为排序后的结果,整数之间用空格分隔。


我的思路如下:

1.根据原列表下标判断,把列表的元素增添到新列表

2.新列表排序后

3.再根据原列表下标判断,把新列表增添到最后的列表输出


方法一:

list = [int(x) for x in raw_input().split(' ')]
list1 = []#You can't use "list1 = list2 = list3 = []" here!!!
list2 = []
list3 = []
list_sorted = []
num_list1 = num_list2 = num_list3 = 0
 
for x in range(len(list)):
    if (x+1) % 3 != 0 and (x+1) % 2 == 0:
        list1.append(list[x])
    elif (x+1) % 3 == 0:
        list2.append(list[x])
    else:
        list3.append(list[x])
list1 = sorted(list1)
list2 = sorted(list2, reverse = True)

for x in range(len(list)):
    if (x+1) % 3 != 0 and (x+1) % 2 == 0:
        list_sorted.append(list1[num_list1])
        num_list1 = num_list1 + 1
    elif (x+1) % 3 == 0:
        list_sorted.append(list2[num_list2])
        num_list2 = num_list2 + 1
    else:
        list_sorted.append(list3[num_list3])
        num_list3 = num_list3 + 1
print ' '.join(str(x) for x in list_sorted)
 
 

方法二:
list = [int(x) for x in raw_input().split(' ')]
list1 = []#You can't use "list1 = list2 = list3 = []" here!!!
list2 = []
list3 = []
list_sorted = []
num_list1 = num_list2 = num_list3 = 0
 
 
for x in list:
    if (list.index(x)+1) % 3 != 0 and (list.index(x)+1) % 2 == 0:
        list1.append(x)
    elif (list.index(x)+1) % 3 == 0:
        list2.append(x)
    else:
        list3.append(x)
list1 = sorted(list1)
list2 = sorted(list2, reverse = True)
for x in list:
    if (list.index(x)+1) % 3 != 0 and (list.index(x)+1) % 2 == 0:
        list_sorted.append(list1[num_list1])
        num_list1 = num_list1 + 1
    elif (list.index(x)+1) % 3 == 0:
        list_sorted.append(list2[num_list2])
        num_list2 = num_list2 + 1
    else:
        list_sorted.append(list3[num_list3])
        num_list3 = num_list3 + 1
print ' '.join(str(x) for x in list_sorted)
 
 
 
 

虽然做了出来,但我觉得方法还不完善,希望大家可以提提意见

目录
相关文章
|
28天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by='A', ascending=False)`进行降序排序;用`rank()`进行排名,如`df['A'].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`。
22 6
|
3月前
|
Python
python实现简单排序
python实现简单排序
|
3月前
|
NoSQL MongoDB Python
深入了解 Python MongoDB 操作:排序、删除、更新、结果限制全面解析
使用 sort() 方法对结果进行升序或降序排序。 sort() 方法接受一个参数用于“字段名”,一个参数用于“方向”(升序是默认方向)。
67 0
|
1月前
|
数据可视化 数据处理 索引
Python如何对数据进行排序和排名操作?
Python如何对数据进行排序和排名操作?
29 0
|
3天前
|
存储 索引 Python
python学习5-列表的创建、增删改查、排序
python学习5-列表的创建、增删改查、排序
|
21天前
|
算法 Python
数据结构与算法 经典排序方法(Python)
数据结构与算法 经典排序方法(Python)
23 0
|
1月前
|
Python
Python系列(22)—— 排序函数
Python系列(22)—— 排序函数
|
1月前
|
存储 搜索推荐 索引
Python中的列表怎么排序
Python中的列表怎么排序
15 0
|
1月前
|
Python
Python生成列表进行排序
Python生成列表进行排序
14 0
|
1月前
|
Python
Python列表排序
Python列表排序
16 0

热门文章

最新文章