Question

my doubt is if it's possible order a list of element in python from strings in the index. for example

list = { }
list['b']='test1'
list['a']='test2'
list['c']='test3'

and i want to obtain this

list['a'] = 'test2'
list['b'] = 'test1'
list['c'] = 'test3'

Thanks in advance. Be patient but is my first day with python.

Était-ce utile?

La solution

You cannot reference a block in the list with a string. what you can do is make a 2D list.

eg:

list = [['b', 'test1'],['a','test2'],['c','test3']]

this way list[0] = ['b', 'test1'] and list[0][0] = 'b'

not you can sort the list with the unicode values of a,b and c using the ord() function a simple bubble sort... python might even have a sort() function for

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top