Hi there! Have the following task:
***
data.json - file with data about ancient mathematicians.
1. Write function, which reads data from the file. Function parameter is the name of the file.
2. Write function which sorts data by surname in the field 'name' (for those who have it).
For example, in case of Rene Descartes surname is Descartes, for Pierre de Fermat it is Fermat etc.
If there is no surname, use name, for example Euclid.
***
It seems I've coped with the first part, but a bit confused by the second task. As the field name can contain only name, a name and surname, or a name, such word as "de" and surname. But we need to sort data by surname only. Not sure how to code it. Here's what I've done so far:
Thanks in advance!
***
data.json - file with data about ancient mathematicians.
1. Write function, which reads data from the file. Function parameter is the name of the file.
2. Write function which sorts data by surname in the field 'name' (for those who have it).
For example, in case of Rene Descartes surname is Descartes, for Pierre de Fermat it is Fermat etc.
If there is no surname, use name, for example Euclid.
***
It seems I've coped with the first part, but a bit confused by the second task. As the field name can contain only name, a name and surname, or a name, such word as "de" and surname. But we need to sort data by surname only. Not sure how to code it. Here's what I've done so far:
import json def read_json(data): with open(data, "r", encoding='utf-8') as file: result = json.load(file) return result def sort_by_surname(data): surname = # suppose here I should convert "name" field to a string, then use split to separate words, and make it use the latter word in case there are 2 or 3 words - sounds a bit complicated for me. Is there another way? Or is my version correct. Would be great if you could help me **huh** sort_surname = sorted(data, key=abs(surname)) print(sort_surname)Pls help

buran write Jan-04-2021, 08:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.