Here is how:
>>> import random,string >>> t= >>> for i in range(20): s='' for j in range(random.randint(3,10)): s=s+random.choice(string.ascii_lowercase) print s t.append(s) ktpwmhc oottmafdv mlalyaig redmmtv bmoflbd gsagulicx qzfvmdz lsviy bdffmd bdmk dtqjnwhla lufobzcqk lfnnf ppv tghkzqxtja bouvqq dbccxvuke vvmmvxl saelhkhgya knwjvdsv >>> s 'knwjvdsv' >>> t ['ktpwmhc', 'oottmafdv', 'mlalyaig', 'redmmtv', 'bmoflbd', 'gsagulicx', 'qzfvmdz', 'lsviy', 'bdffmd', 'bdmk', 'dtqjnwhla', 'lufobzcqk', 'lfnnf', 'ppv', 'tghkzqxtja', 'bouvqq', 'dbccxvuke', 'vvmmvxl', 'saelhkhgya', 'knwjvdsv'] >>> t.sort() >>> t ['bdffmd', 'bdmk', 'bmoflbd', 'bouvqq', 'dbccxvuke', 'dtqjnwhla', 'gsagulicx', 'knwjvdsv', 'ktpwmhc', 'lfnnf', 'lsviy', 'lufobzcqk', 'mlalyaig', 'oottmafdv', 'ppv', 'qzfvmdz', 'redmmtv', 'saelhkhgya', 'tghkzqxtja', 'vvmmvxl'] >>>
Are you looking for designing your own algorithm?
If not then use
Python has a pretty fast algorithm built-in called an adaptive merge sort. Here is an example:
# sort a list of names without changing the original list # uses the high speed sorting algorithm built-into Python name_list1 = ['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al'] name_list2 = sorted(name_list1) print "Original:" print name_list1 print "Sorted:" print name_list2 """my output --> Original: ['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al'] Sorted: ['Al', 'Bob', 'Carl', 'Frank', 'Hank', 'Mark', 'Zoe'] """
If you have to create your own sorting algorithm, go with a selection sort. It is relatively slow, but rather easy to understand.
A selection sort of a list of numbers is pretty simple. You start
with two lists, let's call the original unsorted list the start_list
and you have another list call it the end_list which is empty at
If you want to sort ascending (lowest value first) you get the lowest
value of start_list using lowest = min(start_list) and append it to
the end_list with end_list.append(lowest). Now remove this value from
the start_list with start_list.remove(lowest) and repeat until the
start_list is empty and return the end_list with the sorted values. The repeat can be done with a while loop.
If you have to make your own sort algorithm i would look at the wikipedia pages for sorts such as the bubble sort. It is a very simple sort and the pseudo-code can be very easily translated into python.
From wikipedia pseudo code
procedure bubbleSort( A : list of sortable items ) defined as: do swapped := false for each i in 0 to length(A) - 2 inclusive do: if A[i] > A[i+1] then swap( A[i], A[i+1] ) swapped := true end if end for while swapped end procedure
Hope that helps :)
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click Dim txtB1 As New TextBox Dim i For i = ...