-1

I am trying to sort a list of random integer values like so: aList = [2241, 350] by starting at the last element in each individual integer (after converting the integer to a string) 241, and 350 and sorting into empty lists like so:

zeros = [350]
ones = [2241]
twos = []
threes = []
fours = []
fives = []
sixes = []
sevens = []
eights = []
nines = []

then returning a combined list like so: aList = [350, 2241] and iterating over it again to look at the next value in the integer and repeat the process until the list is sorted from smallest to largest integer.

This is my code so far:

zeros, ones, twos, threes, fours, fives, sixes, sevens, eights, nines = [], [], [], [], [], [], [], [], [], []
for x in range(-1, 0) :
    for val in aList :
        val = str(val)
        if val[x] == 0 :
            zeros.append(val)
        elif val[x] == 1 :
            ones.append(val)
        elif val[x] == 2 :
            twos.append(val)
        elif val[x] == 3 :
            threes.append(val)
        elif val[x] == 4 :
            fours.append(val)
        elif val[x] == 5 :
            fives.append(val)
        elif val[x] == 6 :
            sixes.append(val)
        elif val[x] == 7 :
            sevens.append(val)
        elif val[x] == 8 :
            eights.append(val)
        elif val[x] == 9 :
            nines.append(val)
    aList = zeros + ones + twos + threes + fours + fives + sixes + sevens + eights + nines
    del zeros[:], ones[:], twos[:], threes[:], fours[:], fives[:], sixes[:], sevens[:], eights[:], nines[:]
    return aList

which returns an empty list. Any help is appreciated, thanks.

Daniel
  • 5,095
  • 5
  • 35
  • 48

3 Answers3

2

The higher level problem you're trying to solve is "Sort a list of numbers by the reversed text of their base-10 representation." So this:

[2241, 350, 4, 53, 102]

Becomes this:

[350, 2241, 102, 53, 4]

You do not need an iterative approach where you sort first by the last digit, then by the second-last, and so on. Instead, you can do it all in one go using Python's built-in sorted() function, which takes a sequence (aList) and a key function and returns a new list sorted according to the values returned by the key function. This is the entire code needed:

aList = [2241, 350, 4, 53, 102]

def mySortKey(number):
    return str(number)[::-1]

print sorted(aList, key=mySortKey)

To compose our key we turn the numbers into strings, then reverse the text. So the first number becomes '1422'. For more on why we reverse with [::-1], see: Reverse a string in Python.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1
from collections import defaultdict
d = defaultdict(list)
for val in vals:
    d[val%10].append(val)

print d

might do what you want (although on closer examination I believe you want @JohnZwinck's solution)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Given this list:

aList = [2241, 350, 255, 12, 50]

You can use a dictionary to capture the relevant ending characters:

results = {str(n):list() for n in range(10)}
for x in aList:
    results[str(x)[-1]].append(x)
>>> results
{'1': [2241], '0': [350, 50], '3': [], '2': [12], '5': [255], '4': [], '7': [], '6': [], '9': [], '8': []}

Then you can use a list comprehension to put that into a list of lists:

rl=[results[str(x)] for x in range(10)]
>>> rl
[[350, 50], [2241], [12], [], [], [255], [], [], [], []]

Then you can use tuple unpacking if you really want the named variables to have then value of the lists:

zeros, ones, twos, threes, fours, fives, sixes, sevens, eights, nines=rl

But I think the last step is because you didn't know about dictionaries or list of lists yet ;-)

dawg
  • 98,345
  • 23
  • 131
  • 206