如何从python中MySQL select的列表中去掉括号,通过变量valu比较列表数据

2024-05-15 09:48:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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***

Tags: orthetimefoodmefactx264tea
3条回答

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

“詹姆斯·马丁斯伟大的英国冒险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***

myresult将是一个元组列表,其中一个元素是数据库中的单个select列。你知道吗

下面说明了difflib的情况。你知道吗

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

相关问题 更多 >