News for hackers. Stuff that matters.
Re:Data munging in Perl 6 vs. Perl 5 by viraptor on Tuesday December 09, 2014 @10:47PM (#8727580)
A working version with some stuff improved - groupby implemented, len() instead of sum(), a less squished version of printing, "student(s)" added:

    #!/usr/bin/env python3

    from collections import defaultdict

    def invert_dict(data):
        res = defaultdict(list)
        for k, v in data.items():
            res[v[0]].append(k)
        return res

    with open('data.txt', 'r') as f:
        data = dict(x.split() for x in f if x.strip())

    print("Zsófia's grade:", data["Zsófia"])
    print("List of students with a failing grade:")
    print("  " ,end="")
    print(*[x for x in data.keys() if data[x] == 'F'], sep=", ")
    print("Distribution of grades by letter:")
    grouped = invert_dict(data)
    for k, g in sorted(grouped.items()):
        print("  {}: {} student{}".format(k, len(g), 's' if len(g) > 1 else ''))
Re:Data munging in Perl 6 vs. Perl 5 by Veedrac on Wednesday December 10, 2014 @11:50AM (#8730103)
No need for `invert_dict`; you're just counting so `collections.Counter` would work fine:

    from collections import Counter

    with open('data.txt', 'r') as datafile:
        students = dict(line.rsplit(maxsplit=1) for line in datafile)

    failing = [name for name, grade in students.items() if grade >= 'E']
    grade_counts = Counter(grade[0] for grade in students.values())

    print("Zsófia's grade:", students["Zsófia"])

    print("List of students with a failing grade:")
    print("  " + ", ".join(failing))

    print("Distribution of grades by letter:")
    for grade, count in sorted(grade_counts.items()):
        print("  {}: {} student{}".format(grade, count, 's' * (count != 1)))