How do operator.itemgetter() and sort() work?

前端 未结 5 2037
孤城傲影
孤城傲影 2020-12-12 09:29

I have the following code:

# initialize
a = []

# create the table (name, age, job)
a.append(["Nick", 30, "Doctor"])
a.append(["John&         


        
5条回答
  •  情歌与酒
    2020-12-12 10:02

    #sorting first by age then profession,you can change it in function "fun".
    a = []
    
    def fun(v):
        return (v[1],v[2])
    
    # create the table (name, age, job)
    a.append(["Nick", 30, "Doctor"])
    a.append(["John",  8, "Student"])
    a.append(["Paul",  8,"Car Dealer"])
    a.append(["Mark", 66, "Retired"])
    
    a.sort(key=fun)
    
    
    print a
    

提交回复
热议问题