1

I have a list a = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2',]

How can I get list b = ['a1,', 'a2', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2'] without using .sorted()?

Thanks!

2
  • If you don't want to use sorted() then sort it yourself. :) Commented Mar 6, 2011 at 8:12
  • And why would you not want to use sorted? Commented Apr 29, 2013 at 18:28

6 Answers 6

5

There is no .sorted() method for lists, though there is the sorted() function, as S.Mark pointed out (which returns a new sorted list), and a .sort() method (which sorts a list in place and returns None). If you meant to not use the sorted() function, then:

a = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2',]
a.sort()
b = a

otherwise, maybe you can clarify your question further.

Sign up to request clarification or add additional context in comments.

3 Comments

Okey. But if I have a list of playing card, then .sorted() doesn't work correctly!
There's no playing cards in the question, but you can use the optional argument 'key' to specify the sorting order.
@Bob, I don't understand what you're looking for. Are you looking for a way to sort things that are not letters or numbers? A way to sort strings by their meaning rather than their alphabetic order?
3

It seems a bit arbitrary, not to use sorted(). I think you mean, that you don't want to sort the list in the (default) alphanumerical order.

Here is how you define a key for sorting strings that represent playing cards (a1 through d13) by suit, then rank:

>>> def cardsortkey(card):
...     return (card[0], int(card[1:]))
... 
>>> cardsortkey('a1')
('a', 1)
>>> a = ['a1', 'b1', 'c1', 'd1',
...      'a2', 'b2', 'c2', 'd2',
...      'a11', 'b11', 'c11', 'd11']
>>> sorted(a, key=cardsortkey)
['a1', 'a2', 'a11', 'b1', 'b2', 'b11', 'c1', 'c2', 'c11', 'd1', 'd2', 'd11']

Is that what you need?

1 Comment

+1. I agree, your answers is much more Pythonic and much more elegant. But I don't think that it is more obvious for noob python coder.
2

without using sorted, but expensive way.

a = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2',]

split it to 2 parts

['a1', 'b1', 'c1', 'd1',] ['a2', 'b2', 'c2', 'd2',]

zip it

[('a1', 'a2'), ('b1', 'b2'), ('c1', 'c2'), ('d1', 'd2')]

and flatten it (with itertools here)

import itertools
itertools.chain(*zip(a[:len(a)/2],a[len(a)/2:]))

itertools returns iterator, so If you need list, wrapped it with list(), and assigned it to b

b = list(itertools.chain(*zip(a[:len(a)/2],a[len(a)/2:])))
=> ['a1', 'a2', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2']

Comments

1

just b = sorted(a) ?

1 Comment

note: original question does not say not to use sorted(), so posted this, but I have no idea now.
0
l = ['a1', 'b1', 'c1', 'd1', 'a2', 'b2', 'c2', 'd2']
numbersPerLetter = 2
lsorted = []
for i in range(len(l) / numbersPerLetter):
   lsorted.extend([l[x+i] for x in range(0, len(l), len(l) / numbersPerLetter)])
print(lsorted)

Output:

['a1', 'a2', 'b1', 'b2', 'c1', 'c2', 'd1', 'd2']

In Python 3.X you have to change / to // in order to make it work.

6 Comments

Could you please explain the loop inside the first loop?
It is called a list comprehension. More here: docs.python.org/tutorial/…. For each iteration it creates a little list with the same letters. I mean indexes 0,4 next 1,5 next 2,6 and finally 3,7. More on range with 3 arguments here: docs.python.org/library/functions.html#range
what the…? how can one make python look so ugly?
at the very least you could replace that list comprehension with a simple slice (i think… very hard to read your code): lsorted.extend(l[i::len(l)/numbersPerLetter])
@hop: By the way, I've just noticed that your answer is not really answer to the question. Funny.
|
0

You can also sort it this way

for i1, e1 in enumerate(a):
   for i2, e2 in enumerate(a):
      if e2 > e1:
         e1 = a[i2]
         a[i2] = a[i1]
         a[i1] = e1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.