gpt4 book ai didi

python - 如何使用python比较表的行

转载 作者:行者123 更新时间:2023-12-01 05:57:56 24 4
gpt4 key购买 nike

很抱歉,如果标题有误导性或描述性不够,但这就是我想要的我有一个包含 2 列的表格:

name|value
1.aaaa|132412
2.aaaa|234124
3.aaaa|542253
bbbb|234324
bbbb|342342

所以我想基本上比较 name="aaaa"的行我陷入困境的部分是如何比较 name="aaaa"的所有行

sql="select value from table where name='aaaa'"
cursor.execute(sql)
result=cursor.fetchall()
for row in result:
value=row[0]

从这里我该怎么走?

编辑:我想比较 1 与 2,1 与 3 的值,类似地比较 2 与 3 的值,依此类推。

最佳答案

您已经弄清楚了获取所有结果的代码,然后听起来您想要比较所有两项值的组合。

sql="select value from table where name='aaaa'"
cursor.execute(sql)
results=cursor.fetchall() # changed to results to better reflect the list structure
count = len(results)
for i in range(0, count):
for j in range (i+1, count):
print results[i][0], results[j][0]

这将打印所有的对。 。 。显然您需要解析它们,并在 print 语句所在的位置进行比较。

关于python - 如何使用python比较表的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11659513/

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