Jul-12-2019, 07:07 AM
In order to group first and last name are needed. Therefore this task boils down to how extract names from row.
Rows has different structures or letter types:
ID4,CLARA^OSWALD,F,19890224
ID8,CLARA^oswald,F,19890224
ID14,CLARA^OSWALD^COLEMAN,F,19890224
What these rows have in common? Last name is before and first name after ^. Note, that in one example there is two ^. What will happen if we split on this symbol:
As this is homework you should figure out yourself what to do if you are able to get names out of rows.
Rows has different structures or letter types:
ID4,CLARA^OSWALD,F,19890224
ID8,CLARA^oswald,F,19890224
ID14,CLARA^OSWALD^COLEMAN,F,19890224
What these rows have in common? Last name is before and first name after ^. Note, that in one example there is two ^. What will happen if we split on this symbol:
>>> lst = ['ID4,CLARA^OSWALD,F,19890224', 'ID8,CLARA^oswald,F,1989022', 'ID14,CLARA^OSWALD^COLEMAN,F,19890224'] >>> for row in lst: ... print(row.split('^')) ... ['ID4,CLARA', 'OSWALD,F,19890224'] ['ID8,CLARA', 'oswald,F,19890224'] ['ID14,CLARA', 'OSWALD', 'COLEMAN,F,19890224']We can observe, that last name is in first element after comma; first name is in second element, either before first comma or as whole name. Lets modify code so that we will get first and last name:
>>> for row in lst: ... splitted = row.split('^') ... last_name, first_name = splitted[0].split(',')[1], splitted[1].split(',')[0] ... print(last_name, first_name) ... CLARA OSWALD CLARA oswald CLARA OSWALDWe can observe that names have different types of letters, we should unify them either using str.upper() or str.lower() method.
As this is homework you should figure out yourself what to do if you are able to get names out of rows.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.