Program to sort the list as shown in output.
list1=['gt','abc','eabc','exxx','def','edef','ebcde']
output should be:
['eabc','ebcde','edef','exxx','abc','def','gt'] ....i.e items starting with 'e' and then remaining items sorted.
Solution:
def custom_sort(words):
# Put each word into the x_list or the other_list.
e_list = []
other_list = []
for word in words:
# below if condition can be changed as "if word[0] == 'e':", Output will be same.
if word.startswith('e'):
e_list.append(word)
else:
other_list.append(word)
return sorted(e_list) + sorted(other_list)
if __name__ == '__main__':
list1 = ['gt','abc','eabc','exxx','def','edef','ebcde']
# Calling function defined
list2 = custom_sort(list1)
print list2
No comments:
Post a Comment