我正在尝试对包含如下数据的数据进行排序:
//add a swipe gesture
let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rotateDial(_:)))
downSwipe.direction = .down
self.view.addGestureRecognizer(downSwipe)
我希望我的排序按照第一列的降序排列,如果第一列和第二列之间有任何联系,则第二列按降序排列,如果还有任何联系需要处理,则以相同的降序方式排列第三列
三列排序完成后,数据应按此顺序排序:
col_a,col_b,col_c
100,100,100
10,0,0
20,30,30
70,20,80
22,88,45
37,18,73
60,72,6
15,18,56
71,67,78
93,48,74
4,93,73
这是我的程序:
100,45,35
87,74,91
71,84,52
70,6,97
70,2,80
5,55,83
5,55,5
但是,无论我尝试哪种方法,它似乎都没有排序。这是我的输出:
import csv
import operator
#create an empty list
combined_list = []
#read list of tests from test profile file
with open("data_file.csv") as input_file:
reader = csv.reader(input_file)
header = next(reader) #there is a header row, which I'm ignoring
a_row_list = [[int(row[0]),int(row[1]),int(row[2])] for row in reader]
#add each row read in to the new list
combined_list.append(a_row_list)
#show its type
print(type(combined_list))
#print records
print("1: Read in")
for row in combined_list:
print(row)
#sorted method #1
combined_list = sorted(combined_list, key = operator.itemgetter(1))
print("2: First sort")
for row in combined_list:
print(row)
#sorted method #2
combined_list = sorted(combined_list, key=lambda x: (x[0], x[1], x[2]))
print("3: Second sort")
for row in combined_list:
print(row)
#sorted method #3
combined_list.sort(key = lambda ele : ele[0],ele[1],ele[2])
print("4: Third sort")
for row in combined_list:
print(row)
您可以看到所有三个输出看起来都像原始的一样,我认为这意味着排序不起作用。
以下是一些环境信息:
$python3 TestSort.py
<class 'list'>
1: Read in
[[100, 45, 35], [5, 55, 5], [5, 55, 83], [70, 2, 80], [70, 6, 97], [87, 74, 91], [71, 84, 52]]
2: First sort
[[100, 45, 35], [5, 55, 5], [5, 55, 83], [70, 2, 80], [70, 6, 97], [87, 74, 91], [71, 84, 52]]
3: Second sort
[[100, 45, 35], [5, 55, 5], [5, 55, 83], [70, 2, 80], [70, 6, 97], [87, 74, 91], [71, 84, 52]]
4: Third sort
[[100, 45, 35], [5, 55, 5], [5, 55, 83], [70, 2, 80], [70, 6, 97], [87, 74, 91], [71, 84, 52]]
我已经将数据转换为int数据类型,以避免进行alpha排序,但我认为我仍然缺少某些内容。我认为列表是可变的,因此假定列表也是可变的。
此外,我将如何按多列对此进行排序:
python3 --version
Python 3.6.5 :: Anaconda, Inc.
sw_vers
ProductName: Mac OS X
ProductVersion: 10.13.6
BuildVersion: 17G2208
最后,我仍在学习使用Python编写代码,因此希望了解所有技巧。
谢谢