Hej, ucząc się z tego tutoriala zauważyłem bardzo dziwny błąd, oto mój kod:
import itertools
def get_state(person):
return person['state']
people = [
{'name': 'John Doe', 'city': 'Gotham', 'state': 'NY'},
{'name': 'Jane Doe', 'city': 'Kings Landing', 'state': 'NY'},
{'name': 'Corey Schafer', 'city': 'Boulder', 'state': 'CO'},
{'name': 'Al Einstein', 'city': 'Denver', 'state': 'CO'},
{'name': 'John Henry', 'city': 'Hinton', 'state': 'WV'},
{'name': 'Randy Moss', 'city': 'Rand', 'state': 'WV'},
{'name': 'Nicole K', 'city': 'Asheville', 'state': 'NC'},
{'name': 'Jim Doe', 'city': 'Charlotte', 'state': 'NC'},
{'name': 'Jane Taylor', 'city': 'Faketown', 'state': 'NC'}
]
person_group = itertools.groupby(people, get_state)
for key, group in person_group:
#print(key, len(list(group)))
for person in group:
print(person)
print()
jak widać to wywołanie jest zakomentowane:
#print(key, len(list(group)))
wszystko działa poprawnie, jest wypisane:
{'name': 'John Doe', 'city': 'Gotham', 'state': 'NY'}
{'name': 'Jane Doe', 'city': 'Kings Landing', 'state': 'NY'}
{'name': 'Corey Schafer', 'city': 'Boulder', 'state': 'CO'}
{'name': 'Al Einstein', 'city': 'Denver', 'state': 'CO'}
{'name': 'John Henry', 'city': 'Hinton', 'state': 'WV'}
{'name': 'Randy Moss', 'city': 'Rand', 'state': 'WV'}
{'name': 'Nicole K', 'city': 'Asheville', 'state': 'NC'}
{'name': 'Jim Doe', 'city': 'Charlotte', 'state': 'NC'}
{'name': 'Jane Taylor', 'city': 'Faketown', 'state': 'NC'}
jednak kiedy chcę pokazać obie informacje:
print(key, len(list(group)))
for person in group:
print(person)
to wypisywane jest tylko:
NY 2
CO 2
WV 2
NC 3
Czyli odkomentowanie jednego print'a sprawia że nie działa drugi... nie mam żadnego pomysłu skąd jest ten błąd, z góry dziękuje za pomoc i pozdrawiam serdecznie :)