0
General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD
General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD
Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD
Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD

Say I have these lines. How would I put all the lines in order based on the 5th element in each line? (In the first line, the 18)

1
  • 18 looks like the 4th element. Commented Nov 16, 2014 at 3:57

3 Answers 3

2

It is the fourth element so you would use a lambda as a key splitting on the comma's :

 s="""
General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD
General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD
Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD
Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD
"""
print(sorted(s.splitlines()[1:],key=lambda x:x.split(",")[3]))

['Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD', 'Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD', 'General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD', 'General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD', 'Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD', 'Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD']

Reading from a file:

with open("in.txt") as f:
    print(sorted(f,key=lambda x:x.split(",")[3]))
['Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD\n', 'Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD\n', 'General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD\n', 'General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD\n', 'Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD\n', 'Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD']
Sign up to request clarification or add additional context in comments.

4 Comments

with file open will it work, as you didn't know the next line, and you sorting??
I thought these would be lists. Great answer.
can you explain me how it is working, as while didn't gone to next line, didn't know about what come next
@Hackaholic, calling sorted on the file object iterates over all the lines no different to print(list(f))
0

This sort of thing is generally nicer without resorting to lambdas -- use operator.itemgetter instead:

from operator import itemgetter    

data = """
General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD
General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD
Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD
Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD
"""

data_lines = [x.split(",") for x in data.splitlines()[1:]]

print sorted(data_lines, key=itemgetter(3))

# Optionally join back to strings:
map(",".join, sorted(data_lines, key=itemgetter(3)))

Optionally, to use the file-as-iterable approach:

from string import split
from functools import partial
from operator import itemgetter

getter = itemgetter(3)
splitter = partial(split, sep=",")

def sort_key(s):
    return getter(splitter(s))

with open("data_file.txt") as data:
    print sorted(data, key=sort_key))

This nicely separates the different parts (the splitting separator, the particular item index for sorting, and the actual call to sorted) that would otherwise have been poorly coupled inside a single lambda.

1 Comment

I don't need to defend my approach, it is pythonic, using a lambda with sorted is an idiomatic approach. I don't see where you are coming from with This sort of thing is generally nicer without resorting to lambdas
0

You could do something like:

l=[[1,2,3,4,8,6], [1,2,3,4,7,6]]
l.sort(key=lambda x: x[4]) 

l would be a list of all your lists.

1 Comment

would there be anyway to join these elements back into a string?

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.