Jean-Michel Pichavant wrote:
Gilles Ganault wrote:
On Fri, 22 Jan 2010 14:09:43 +0100, Jean-Michel Pichavant
<jeanmic...@sequans.com> wrote:
Sorry, the code I provided produce this output:

['1a', 'a', 'ac', 'av', 'b', 'c']
['a', 'ac', 'av', 'b', 'c', '1a']
['b', 'c', '1a', 'a', 'ac', 'av']
['c', '1a', 'a', 'ac', 'av', 'b']
['1a', 'a', 'ac', 'av', 'b', 'c']

which is actually what you are searching for. I just messed up with my ipython shell history :o)

Thanks for the help. I'm a Python newbie, and have a difficult time
understanding what the [] + [] line does :-/

I'll simplify things by using a list instead of a dictionary:

============
connected = []
connected.append("0test")
connected.append("aa")
connected.append("bb")
connected.append("cc")

for start in '1abcd':
    result = [name for name in connected if name[0] >= start] + [name
for name in connected if name[0] < start]
    print result
============
C:\>test.py
['aa', 'bb', 'cc', '0test']
['aa', 'bb', 'cc', '0test']
['bb', 'cc', '0test', 'aa']
['cc', '0test', 'aa', 'bb']
['0test', 'aa', 'bb', 'cc']
============

Pretty close to what I need to do but..

1. Why is the first iteration done twice?

2. How can I have just one line, save the character that I used as
starting point, increment it, and save it into a file so it can be
read the next time this program runs? For instance, let's say we used
"b" to start looking for items, I'll save "c" in a file for the next
time.

Thank you.

1/ [] + [] is using 2 python lists comprehension, Google it for details. It is quite difficult to read until you become familiar with it. Once you get it, you can write magical stuff :o) Basically, list comrehension allows to map functions to list elements and / or filter those elements.

So what I'm using is the filter feature of list comprehension:
[list of names for which the first char is greater that 'start'] + [list of names for which the first char is less than 'start']

2/ Are you sure you want to do that ? looks like you are using a hammer to smash a fly. As someone has suggested before, maybe you want to pickup some starting index random.

connected = ['aa', 'bb', 'cc', '0test']

import random

def getNewOrder(myList):
    index = random.randint(0,len(myList)-1)
    print index
return myList[index:] + myList[:index] # using slicing instead of list comprehension (suggested by DaveA)

print getNewOrder(connected)

JM


Ok I realized that picking up a random index prevent from grouping names starting with the same letter (to ease visual lookup).
Then go for the random char, and use char comparison (my first example).

JM
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to