[2020 Day 6 (Part 2)] [Python] help me be more pythonic
answers = 0
with open('input06.txt') as f:
for group in f.read().split("\n\n"):
answer_l = group.split("\n")
answer_inter = set(answer_l[0])
for ans in answer_l:
answer_inter = answer_inter & set(ans)
answers += len(inter)
print('> part two: {}'.format(answers))
This answer works, BUT it really does not feel very pythonic. In particular, it feels like the for loop that handles the intersection could be way more elegant. I know I could use an enumerate() to prevent the first set from checking against itself, but that's the least of my worries.
I'd be curious to hear opinions!
Posted Dec 8, 2020, 10:39 PM UTC
22
Ok, I got to thinking, and the above feels more pythonic to me. Leaving this open in case there's alternate opinions.
Fixed formatting.
Hello, mirth23: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
^(You can opt out by replying with backtickopt6 to this comment.)