gpt4 book ai didi

python - 比较两组数字

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:37 27 4
gpt4 key购买 nike

我正在尝试比较两组匹配的数字。一旦找到号码,就会将与该号码关联的用户名以及它们匹配的号码数量附加到字典中。我的解释不是最好的,但也许我的代码会显示我正在尝试做的事情。

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'))

最佳答案

您可以使用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)))

关于python - 比较两组数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com