python - 如何在mysql中搜索多少重复并进行比较
问题描述
所以我想根据它们在 mysql 中有多少重复来比较变量a
和变量。b
这是我的代码:
query = """ SELECT `do` FROM `foo` """
cursor.execute(query)
result=cursor.fetchall()
a = '0001'
b = '1100'
y = collections.Counter(result)
print(y)
这是我的输出:
Counter({('0001',): 2, ('1100',): 1}, ('0000',): 4})
输出正在计算整行的重复项。我希望它只计算mysql的数量a
和数量。b
而且我实际上不知道在此之后该怎么办。
如果a > b
print ,我希望代码运行a
。
如果a < b
打印b
我想要的输出:
a = 2 # number of duplicates
b = 1
a # print a because a > b
任何答案将不胜感激。
解决方案
a = ("0001",) # Set to tuples so you can directly use them as keys
b = ("1100",)
y = Counter({('0001',): 2, ('1100',): 1, ('0000',): 4})
a_count = y.get(a) # The number of times `("0001",)` occurs
b_count = y.get(b) # The number of times `("1100",)` occurs
if a_count > b_count:
print(a)
elif a_count < b_count:
print(b)
推荐阅读
- java - 错误消息后将 inputText 值恢复为以前的值
- c++ - 将消息中的泛型传递给事件侦听器的正确设计
- c# - 实体框架只包含返回部分数据库数据
- c# - 防止 Simple.OData.Client 获取整个结构
- python - Python-GNUPG 加密文件无法用私钥解密
- python - 我的一个轴的所有值都没有显示(只有其中一些显示在图表上)并且我在 python 中得到了一个错误的图表
- python - Python:获取所有可能的组合,用于将 x 个苹果分配给 y 个受约束的篮子
- javascript - 循环不会搜索整个对象数组?
- python - 类实例化在 python 中给出位置错误
- mongodb - 从字段中查找最大值