sorting

Sorting a list of lists in Python

ぐ巨炮叔叔 提交于 2020-01-09 19:56:32
问题 c2=[] row1=[1,22,53] row2=[14,25,46] row3=[7,8,9] c2.append(row2) c2.append(row1) c2.append(row3) c2 is now: [[14, 25, 46], [1, 22, 53], [7, 8, 9]] how do i sort c2 in such a way that for example: for row in c2: sort on row[2] the result would be: [[7,8,9],[14,25,46],[1,22,53]] the other question is how do i first sort by row[2] and within that set by row[1] 回答1: The key argument to sort specifies a function of one argument that is used to extract a comparison key from each list element. So

Sorting Groups of Rows Excel VBA Macro

我的梦境 提交于 2020-01-09 10:49:08
问题 I am having trouble figuring out how to create a sorting algorithm in VBA that sorts and swaps groups of rows (several rows at a time). I wrote a successful sorting algorithm using an array below: Function SortArray(ByRef arrToSort As Variant) Dim aLoop As Long, aLoop2 As Long Dim str1 As String Dim str2 As String For aLoop = 1 To UBound(arrToSort) For aLoop2 = aLoop To UBound(arrToSort) If UCase(arrToSort(aLoop2)) < UCase(arrToSort(aLoop)) Then str1 = arrToSort(aLoop) str2 = arrToSort(aLoop2

Python: How to custom order a list?

爱⌒轻易说出口 提交于 2020-01-09 10:45:28
问题 Obs: I know lists in python are not order-fixed, but think that this one will be. And I'm using Python 2.4 I have a list, like (for example) this one: mylist = [ ( u'Article', {"...some_data..."} ) , ( u'Report' , {"...some_data..."} ) , ( u'Book' , {"...another_data..."} ) , ...#continue ] This variable mylist is obtained from a function, and the 'order' of the list returned will vary. So, sometimes it will be like on the example. Sometimes, the 'Report' will come before 'Article', etc. I

Sorting a text file alphabetically (Python)

让人想犯罪 __ 提交于 2020-01-09 10:42:47
问题 I would like to sort the file 'shopping.txt' in alphabetical order shopping = open('shopping.txt') line=shopping.readline() while len(line)!=0: print(line, end ='') line=shopping.readline() #for eachline in myFile: # print(eachline) shopping.close() 回答1: Just to show something different instead of doing this in python, you can do this from a command line in Unix systems: sort shopping.txt -o shopping.txt and your file is sorted. Of course if you really want python for this: solution proposed

Sorting a text file alphabetically (Python)

Deadly 提交于 2020-01-09 10:41:05
问题 I would like to sort the file 'shopping.txt' in alphabetical order shopping = open('shopping.txt') line=shopping.readline() while len(line)!=0: print(line, end ='') line=shopping.readline() #for eachline in myFile: # print(eachline) shopping.close() 回答1: Just to show something different instead of doing this in python, you can do this from a command line in Unix systems: sort shopping.txt -o shopping.txt and your file is sorted. Of course if you really want python for this: solution proposed

An array of length N can contain values 1,2,3 … N^2. Is it possible to sort in O(n) time?

放肆的年华 提交于 2020-01-09 10:09:52
问题 Given an array of length N. It can contain values from ranging from 1 to N^2 (N squared) both inclusive, values are integral. Is it possible to sort this array in O(N) time? If possible how? Edit: This is not a homework. 回答1: Write each integer in base N, that is each x can be represented as (x1, x2) with x = 1 + x1 + x2*N. Now you can sort it twice with counting sort, once on x1 and once on x2, resulting in the sorted array. 回答2: Yes, you can, using radix sort with N buckets and two passes.

How do I sort a multi-dimensional array by value?

偶尔善良 提交于 2020-01-09 08:09:07
问题 I have an array as following and I want to order that array by the value of the key "attack". First keys of the arrays (15, 13, 18) are ID of some certain item from database, so I don't want these keys to be changed when the array is sorted. Any help would be greatly appreciated. This is the array: $data = array( '15' => array( 'attack' => '45', 'defence' => '15', 'total' => '10' ), '13' => array( 'attack' => '25', 'defence' => '15', 'total' => '10' ), '18' => array( 'attack' => '35',

javascript array.sort with undefined values

ぃ、小莉子 提交于 2020-01-09 07:52:07
问题 How does Array.prototype.sort handle undefined values in an array? var array = [1,undefined,2,undefined,3,undefined,4]; var array2 = []; array2[0] = 1;array2[2] = 2;array2[4] = 3;array2[6] = 4; When calling array.sort(function(l,r) { ... }); The values undefined are never passed in as l or r . Can I guarantee that all the undefined values will always go to the end of the array for all browsers? Would the following loop handle all the non undefined data in an array array.sort(); for (var i = 0

Assembly - bubble sort for sorting string

与世无争的帅哥 提交于 2020-01-09 05:36:48
问题 I am writing a program in assembly using tasm. My task is to write a program that will use bubble sort to sort entered string alphabetically. Ex. if you enter "hello" it should write "ehllo". I have writened the begging to enter string and to sort it (I think it works okey until the end where it should print out the result, but at the end it just writes my .data once and the finisheds its work) P.S sorry for bad english .model small .stack 100h .data request db 'This program is using

Custom sorting with Pandas

旧街凉风 提交于 2020-01-09 05:35:29
问题 I have the following dataframe that I would like to sort first by Criticality and then by Name: Name Criticality baz High foo Critical baz Low foo Medium bar High bar Low bar Medium ... I've been trying to do this using the answer provided in this post but I just can't get it to work. The end result should be like this Name Criticality bar High bar Medium bar Low baz High baz Low foo Critical foo Medium 回答1: One approach would be to use a custom dict to create a 'rank' column, we then use to