Hi,
Could u please help me to sort this 3 dimension data in python? How can sort this 5 by 3 array with 1st column or last column? Thanks a lot.

marek brutalski 20
zenia markownikowa 10
teresa parufkowa 90
bogumila pierdawa 40
genowefa tempawa 50

John

## All 8 Replies

What have you got so far?
How do you have the data represented?

What have you got so far?
How do you have the data represented?

That's 5 by 3 array separated by space. How can I sort it by any columns? Thanks.

It looks more like an array of 5 strings?

``````first_list = [" marek brutalski 20",
"zenia markownikowa 10",
"teresa parufkowa 90",
"bogumila pierdawa 40",
"genowefa tempawa 50"]``````

What you will need to do is to split it into a real 5x3 array:

``````first_list = [" marek brutalski 20",
"zenia markownikowa 10",
"teresa parufkowa 90",
"bogumila pierdawa 40",
"genowefa tempawa 50"]

second_list = [x.split() for x in first_list]``````

Then you need to sort based on a key. list.sort() allows the user to supply a key, which can be any function that returns a comparable value. Usually, the function is in the form

``my_list.sort(key = lambda x: x.upper())``

If you aren't familiar with lambda functions, here's what this means:

* create a function in place that takes an x and returns x.upper()
* sort my_list by passing each element to the function above and using the result for comparison.

In other words, my_list will be sorted according to upper case alphabetical order.

Now for you, the key should be

``second_list.sort(key = lambda x:x[column_number])``

And that'll do it!

Ex.:

``````first_list = [" marek brutalski 20",
"zenia markownikowa 10",
"teresa parufkowa 90",
"bogumila pierdawa 40",
"genowefa tempawa 50"]

second_list = [x.split() for x in first_list]
second_list.sort(key = lambda x:x[0])  # sorts on column 0``````

Jeff

If you are uncomfortable with lambda, you can also use itemgetter() from the module operator ...

``````from operator import itemgetter

first_list = [
" marek brutalski 20",
"zenia markownikowa 10",
"teresa parufkowa 90",
"bogumila pierdawa 40",
"genowefa tempawa 50"]

second_list = [x.split() for x in first_list]

# first column is 0, sort on column 2, print result
for name in sorted(second_list, key=itemgetter(2)):
print " ".join(name)

"""
my result -->
zenia markownikowa 10
marek brutalski 20
bogumila pierdawa 40
genowefa tempawa 50
teresa parufkowa 90
"""``````

How can I sort it by any columns?

There is a way to do this using a function, but you will have to at least __try__ to do it yourself first and then post the code here. I hope you understand that it is considered rude to "place your code order" and then sit back and wait for someone else to do it for you.

There is a way to do this using a function, but you will have to at least __try__ to do it yourself first and then post the code here. I hope you understand that it is considered rude to "place your code order" and then sit back and wait for someone else to do it for you.

You are of course correct woooee! Got to give this more attention, hope it wasn't homework.

It's impossible to tell if someone is serious and doesn't have a clue on where to start, in which case I would help, or if they are too lazy or want to scam someone else into doing it for them. I think I will start asking if this is homework, and/or for some pseudo-code at least so their little minds will have to start cranking on their own. This is a nice site and I want to do my part to keep it that way so I'll error on the side of "no clue where to start".

I am primarily using C# (that's what my school uses), but enjoy looking into Python code. I am looking at the many code samples shown here, and find them so much easier to understand and read than C#.

I have installed the Python system on my computer and, using the great gem called PyScripter editor, start to get used to this initially strange but elegant language. Lots to learn!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.