首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在MySQL select列表中去掉括号通过变量值比较列表数据

如何在MySQL select列表中去掉括号通过变量值比较列表数据
EN

Stack Overflow用户
提问于 2019-03-02 05:35:37
回答 3查看 143关注 0票数 1

我想从MySQL数据库中选择一些行并保存在一个列表中,然后与变量值进行比较,但结果为空

这是我的代码:

代码语言:javascript
复制
mycursor = lucas_db.cursor()

mycursor.execute("SELECT Release_Name FROM Lucas_Table WHERE Published_Time > SUBDATE( CURRENT_TIME, INTERVAL 30 MINUTE)")
myresult = mycursor.fetchall()

print(myresult)

last=input('give me:')

me = difflib.get_close_matches(last, myresult)
print(me)

结果是:

代码语言:javascript
复制
[('Food-Fact or Fiction S04E16 Tea Time WEBRip x264-CAFFEiNE',), ('A Million Little Things S01E17 720p HEVC x265-MeGusta',), ('The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD',), ('The Pioneer Woman S21E09 16-Minute Chicken AAC MP4-Mobile',), ('Northern Rescue S01E09 720p HEVC x265-MeGusta',), ('Food-Fact or Fiction S04E16 Tea Time XviD-AFG',), ('Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD',), ('Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile',), ('How to Get Away with Murder S05E15 720p HEVC x265-MeGusta',), ('The Titan Games S01E09 720p HEVC x265-MeGusta',)]
give me:The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD

[]

***Repl Closed***
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-02 05:44:36

myresult将是一个元组列表,其中包含一个元素,即数据库中的单个选择列。

下面说明了difflib是怎么回事。

代码语言:javascript
复制
a = [('foo',),('bar',)]
difflib.get_close_matches('foo', a)
[]
a = [a[0] for a in a]
difflib.get_close_matches('foo', a)
['foo']
票数 1
EN

Stack Overflow用户

发布于 2019-03-02 06:11:14

您可以使用chain.from_iterable()链接myresult中的所有字符串。您还可以尝试更改difflib.get_close_matches()中的cutoff参数

代码语言:javascript
复制
from itertools import chain
import difflib

myresult = [('Food-Fact or Fiction S04E16 Tea Time WEBRip x264-CAFFEiNE',), ('A Million Little Things S01E17 720p HEVC x265-MeGusta',), ('The Pioneer Woman S21E09 16-Minute Chicken 480p x264-mSD',), ('The Pioneer Woman S21E09 16-Minute Chicken AAC MP4-Mobile',), ('Northern Rescue S01E09 720p HEVC x265-MeGusta',), ('Food-Fact or Fiction S04E16 Tea Time XviD-AFG',), ('Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD',), ('Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile',), ('How to Get Away with Murder S05E15 720p HEVC x265-MeGusta',), ('The Titan Games S01E09 720p HEVC x265-MeGusta',)]

c = chain.from_iterable(myresult)

print(difflib.get_close_matches('Food', c, cutoff=0.1))

输出:

代码语言:javascript
复制
['Food-Fact or Fiction S04E16 Tea Time XviD-AFG', 'Food-Fact or Fiction S04E16 Tea Time 480p x264-mSD', 'Food-Fact or Fiction S04E16 Tea Time AAC MP4-Mobile']
票数 0
EN

Stack Overflow用户

发布于 2019-03-02 06:11:40

感谢@Rich Andrews,它成功了:

代码语言:javascript
复制
mycursor.execute("SELECT Release_Name FROM Lucas_Table WHERE Published_Time > SUBDATE( CURRENT_TIME, INTERVAL 30 MINUTE)")
myresult = mycursor.fetchall()

myresult = [a[0] for a in myresult]

last=input('give me:')

me = difflib.get_close_matches(last, myresult)
print(me)

"James Martins Great British Adventure S01E03 WebX264-LiGATE“的结果是:

代码语言:javascript
复制
give me:James Martins Great British Adventure S01E03 WEB x264-LiGATE
['James Martins Great British Adventure S01E09 WEB x264-LiGATE', 'James Martins Great British Adventure S01E11 WEB x264-LiGATE']

***Repl Closed***
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54952635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档