Skip to content

Instantly share code, notes, and snippets.

@Ojha-Shashikant
Last active November 29, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ojha-Shashikant/50b2c1c70e75f6a1dd33421e34ad2489 to your computer and use it in GitHub Desktop.
Save Ojha-Shashikant/50b2c1c70e75f6a1dd33421e34ad2489 to your computer and use it in GitHub Desktop.
Read the file Age.txt and print the names in descending order of Age.
''' Read the file Age.txt and print the names in descending order of Age '''
def reading_file():
FH = open("C:\\Users\\Ojha\\Documents\\Python exercises\\Python Scripts\\Age.txt", 'r+')
return FH
def file_operation(FH):
text = FH.read()
FH.close()
new_text = text.split()
final_text = [txt for txt in new_text if txt != '-']
details = dict()
#name_list = [final_text[idx] for idx in range(len(final_text)) if idx % 2 == 0]
#age_list = [final_text[idx] for idx in range(len(final_text)) if idx % 2 != 0]
#print(name_list)
#print(age_list)
for idx in range(len(final_text)):
if idx % 2 == 0:
details[final_text[idx]] = final_text[idx + 1]
return details
def printing(details):
sorted_details = sorted(details.items(), key = lambda x:x[1], reverse = True)
print(sorted_details)
for k,v in sorted_details:
print(k + '-' + v)
#main starts here
if __name__ == '__main__':
FH = reading_file()
details = file_operation(FH)
printing(details)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment