I have an input:
4 m.hosSein.python f.miNa.C m.aHMad.C++ f.Sara.java
and I must sort it in the way that first comes all f(females) ordered by alphabet then comes all m(males) ordered by alphabet. This is the desirable output:
f Mina C f Sara java m Ahmad C++ m Hossein python
.here is my code:
`n=int(input()) lst=[] for i in range (0,n): x=input().split('.') lst.append(x) list=sorted(lst,key=lambda x:(x[0],x[1])) for item in lst: if item[0]=='f': print(item[0],item[1].capitalize(),item[2]) else: print(item[0],item[1].capitalize(),item[2])`
my code dose not sort f.(females) correctly and Sara comes first. Could anybody help me?
Advertisement
Answer
Try this, it should be fine
n = int(input()) lst = [] for i in range(0, n): x = input() # no split as above comments lst.append(x) # remove end part (no need for comparison lst = sorted(lst, key=lambda x: x.lower().rsplit(".",1)[0]) print(lst) # if you really need to split you can do for item in lst: item = item.split(".") # what is the point of this IF stateement ? (OP, please check the logic here ) if item[0] == 'f': print(item[0], item[1].capitalize(), item[2]) else: print(item[0], item[1].capitalize(), item[2])
Output :
f Mina C f Sara java m Ahmad C++ m Hossein python