简体   繁体   中英

Comparing two sets of numbers

I am trying to compare two sets for matching numbers. The once a number has been found append the username associated with the numbers to a dictionary along with the amount of numbers they have matched. My explanation is not the best but perhaps my code will show what I am trying to do.

c.execute('SELECT * FROM {} WHERE drawnumber = (%s)'.format(numbertable),
          drawnumber)
numbers = c.fetchall()[0]
print(numbers)

numbers = set(imap(str.rstrip, numbers))

print(numbers)

c.execute('SELECT * FROM {} WHERE paid = (%s)'.format(table), paid)
entries = c.fetchall()
print(entries)

results = defaultdict(list)

for row in entries:
    user = row[1]
    number1 = row[2]
    number2 = row[3]
    number3 = row[4]
    number4 = row[5]
    number5 = row[6]
    pub_key = row[7]

    results[sum(n in numbers for n in row)].append(user)

FYI: the set numbers looks like so: set(['11', '12', '15', '1', '3', '8'])

The entries set looks something like this:

((9L, 'test8', 12L, 4L, 3L, 15L, 8L, '1Bu1hXvk7NzoCgRFyXcGwFULtdSRsDNoCy', 'T'),
 (10L, 'test9', 12L, 4L, 1L, 15L, 8L, '1Bu1hXvk7NzoCgRFyXcGwFULtdSRsDNoCy', 'T'))

You could use set.intersection

for _, user, number1, number2, number3, number4, \
        number5, pub_key, _ in row:
    row_nums = set([number1, number2, number3, number4, number5])
    matching = row_nums.intersection(numbers)
    if matching:
        results[len(matching)].append(user)

However numbers is a set of strings, not a set of ints. You should fix that.

numbers = set(imap(int, imap(str.rstrip, numbers)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM