lists are accounting for nothing as a similarity?
Question:
I’m trying to find matches in 2 parts of a string of numbers, my input, "ref4" is:
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
when I try and find matches from one side of the split line in the middle, i.e. on card 1 the matches would be 48, 83, 86 and 17, but it also counts ” as a match. The exact result i get is:
{”, ’48’, ’83’, ’86’, ’17’}
{”, ’32’, ’61’}
{”, ‘1’, ’21’}
{”, ’84’}
{”}
{”}
this leads the amount of matches to be "5" because it’s counting nothing each time as a match, when in reality it should be "4" (card 1)
I have no idea why it’s counting literally nothing as a match but that’s messing with my ability to calculate the score of this properly. My goal is to find the matches, 1 match is one point and every match after that doubles the score. for instance, card 1 should have 8 points worth of matches with 4 matches in total so that’s just 1x2x2x2, etc. and moving on with the rest of the cards. I think i can figure out how to calculate the score but my main issue is having the match part of the code count nothing as a match. Why is this happening?
code:
with open('ref4', 'r') as fp:
for line in fp:
winners = line.split(':')[1].split('|')[0].split(' ')
choices = line.strip().split('|')[-1].split(' ')
def common_member(win, choice):
win_set = set(win)
choice_set = set(choice)
if win_set & choice_set:
match = win_set & choice_set
print(match)
common_member(winners, choices)
ref4:
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
Answers:
Don’t use a separator to split on whitespace, let Python does the job. I just convert the string as number in a comprehension:
with open('ref4', 'r') as fp:
for line in fp: # remove separator here --v
winners = [int(num) for num in line.split(':')[1].split('|')[0].split()]
choices = [int(num) for num in line.strip().split('|')[-1].split()]
print(set(winners).intersection(choices))
Output:
{48, 17, 83, 86}
{32, 61}
{1, 21}
{84}
set()
set()
Documentation str.split
:
sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
Edit
I prefer this version:
with open('ref4', 'r') as fp:
for line in fp:
winners, choices = line[line.index(':')+1:].split('|')
winners = [int(num) for num in winners.split()]
choices = [int(num) for num in choices.split()]
print(set(winners).intersection(choices))
I’m trying to find matches in 2 parts of a string of numbers, my input, "ref4" is:
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
when I try and find matches from one side of the split line in the middle, i.e. on card 1 the matches would be 48, 83, 86 and 17, but it also counts ” as a match. The exact result i get is:
{”, ’48’, ’83’, ’86’, ’17’}
{”, ’32’, ’61’}
{”, ‘1’, ’21’}
{”, ’84’}
{”}
{”}
this leads the amount of matches to be "5" because it’s counting nothing each time as a match, when in reality it should be "4" (card 1)
I have no idea why it’s counting literally nothing as a match but that’s messing with my ability to calculate the score of this properly. My goal is to find the matches, 1 match is one point and every match after that doubles the score. for instance, card 1 should have 8 points worth of matches with 4 matches in total so that’s just 1x2x2x2, etc. and moving on with the rest of the cards. I think i can figure out how to calculate the score but my main issue is having the match part of the code count nothing as a match. Why is this happening?
code:
with open('ref4', 'r') as fp:
for line in fp:
winners = line.split(':')[1].split('|')[0].split(' ')
choices = line.strip().split('|')[-1].split(' ')
def common_member(win, choice):
win_set = set(win)
choice_set = set(choice)
if win_set & choice_set:
match = win_set & choice_set
print(match)
common_member(winners, choices)
ref4:
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
Don’t use a separator to split on whitespace, let Python does the job. I just convert the string as number in a comprehension:
with open('ref4', 'r') as fp:
for line in fp: # remove separator here --v
winners = [int(num) for num in line.split(':')[1].split('|')[0].split()]
choices = [int(num) for num in line.strip().split('|')[-1].split()]
print(set(winners).intersection(choices))
Output:
{48, 17, 83, 86}
{32, 61}
{1, 21}
{84}
set()
set()
Documentation str.split
:
sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
Edit
I prefer this version:
with open('ref4', 'r') as fp:
for line in fp:
winners, choices = line[line.index(':')+1:].split('|')
winners = [int(num) for num in winners.split()]
choices = [int(num) for num in choices.split()]
print(set(winners).intersection(choices))