繁体   English   中英

如何从python中的MySQL选择列表中删除括号以通过变量值比较列表数据

[英]how to remove parenthesis from list of MySQL select in python for compare list data via variable value

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

这是我的代码:

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)

结果是:

[('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***

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

下面说明了 difflib 发生了什么。

a = [('foo',),('bar',)]
difflib.get_close_matches('foo', a)
[]
a = [a[0] for a in a]
difflib.get_close_matches('foo', a)
['foo']

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

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

输出:

['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']

感谢@Rich Andrews 它的工作:

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 WEB x264-LiGATE”的结果是:

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***

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM