我正在尝试比较两组匹配数字。一旦找到一个数字,就会将与数字相关联的用户名附加到字典以及它们匹配的数字量。我的解释不是最好的,但也许我的代码会显示我想要做的事情。
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)
仅供参考:设定数字如下:set(['11', '12', '15', '1', '3', '8'])
条目集看起来像这样:
((9L, 'test8', 12L, 4L, 3L, 15L, 8L, '1Bu1hXvk7NzoCgRFyXcGwFULtdSRsDNoCy', 'T'),
(10L, 'test9', 12L, 4L, 1L, 15L, 8L, '1Bu1hXvk7NzoCgRFyXcGwFULtdSRsDNoCy', 'T'))
答案 0 :(得分:1)
您可以使用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)
但是numbers
是一组字符串,而不是一组整数。你应该解决这个问题。
numbers = set(imap(int, imap(str.rstrip, numbers)))