I have an input:
JavaScript
x
6
1
4
2
m.hosSein.python
3
f.miNa.C
4
m.aHMad.C++
5
f.Sara.java
6
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:
JavaScript
1
5
1
f Mina C
2
f Sara java
3
m Ahmad C++
4
m Hossein python
5
.here is my code:
JavaScript
1
13
13
1
`n=int(input())
2
lst=[]
3
for i in range (0,n):
4
x=input().split('.')
5
lst.append(x)
6
list=sorted(lst,key=lambda x:(x[0],x[1]))
7
8
for item in lst:
9
if item[0]=='f':
10
print(item[0],item[1].capitalize(),item[2])
11
else:
12
print(item[0],item[1].capitalize(),item[2])`
13
my code dose not sort f.(females) correctly and Sara comes first. Could anybody help me?
Advertisement
Answer
Try this, it should be fine
JavaScript
1
19
19
1
n = int(input())
2
lst = []
3
for i in range(0, n):
4
x = input() # no split as above comments
5
lst.append(x)
6
7
# remove end part (no need for comparison
8
lst = sorted(lst, key=lambda x: x.lower().rsplit(".",1)[0])
9
print(lst)
10
11
# if you really need to split you can do
12
for item in lst:
13
item = item.split(".")
14
# what is the point of this IF stateement ? (OP, please check the logic here )
15
if item[0] == 'f':
16
print(item[0], item[1].capitalize(), item[2])
17
else:
18
print(item[0], item[1].capitalize(), item[2])
19
Output :
JavaScript
1
5
1
f Mina C
2
f Sara java
3
m Ahmad C++
4
m Hossein python
5