将元组的元组转换为列表并删除额外的括号
Convert a tuple of tuples to list and remove extra bracket
我是 Python 的新手,我正在尝试使用 Python 查询 Microsoft SQL 服务器数据库。数据以这种格式返回:
(('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
我想做的是检查以找到匹配项,以查看来自 SQL 服务器的数据中是否存在来自另一个数据 table 的某些数据。
当我尝试使用“in”进行检查时,它对我不起作用。我想如果我可以将数据(元组的元组)转换为列表,那么我可以更轻松地搜索和匹配。但这不起作用,因为每个列表元素周围都有一些括号。至少,我是这么想的,因为如果我在没有额外括号的情况下手动重新创建列表,那么我就可以成功搜索。
我想知道是否有办法删除那个多余的支架。或者,也许有更好的方法。我已经阅读了这里的几篇文章和其他文章,但到目前为止,我还没有找到方法。
这是我试过的。如您所见,最后一个有效,但这是我手动创建的。
# here is the data that gets returned from Msft Sql Server
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
print(sql_data)
print('type of rds_qubole_data is {}'.format(type(sql_data)))
# the type shows as <class 'tuple'>
# set some variables for checking in the data
Orange1 = 'orange,apple,coconut' in sql_data
print('orange1 is {}'.format(Orange1))
# shows orange1 is False
Vegan1 = 'lettuce,carrot,celery' in sql_data
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# then, i try to convert to a list using a method i saw in another post
conv_to_list = [list(tup) for tup in sql_data]
print(conv_to_list)
# data looks like:
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('type of conv_to_list is {} '.format(type(conv_to_list)))
# prints type of conv_to_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in conv_to_list
print('orange1 is {}'.format(Orange1))
# orange1 is False
Vegan1 = 'lettuce,carrot,celery' in conv_to_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# finally, i just manually type the list and remove the brackets around each element
manual_list = ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print(manual_list)
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('Type of manual_list is {}'.format(type(manual_list)))
# Type of manual_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in manual_list
print('orange1 is {}'.format(Orange1))
# orange1 is True
Vegan1 = 'lettuce,carrot,celery' in manual_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is True
使用for循环匹配数据:
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
found = False
for item in sql_data:
if target in item:
found = True
print(found)
输出:
True
使用列表压缩查找是否找到天气目标:
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
out = [True if target in item else False for item in sql_data]
print(True in out)
输出:
True
使用 lambda 查找天气目标值是否存在:
from functools import reduce
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
found = reduce(lambda bool_value1, bool_value2:bool_value1 or bool_value2,[True if target in item else False for item in sql_data])
print(found)
输出:
True
检查下面的代码是否有帮助:
t1 = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',)) # DataBase
q1 = 'lettuce,apple,celery' # query
for i in t1:
for j in i:
if q2 in j:
print("Items present")
如果您有要查询的项目列表:
q1 = 'lettuce,apple,celery' # no
q2 = 'orange,apple,coconut' # yes
q3 = 'orange,lemon,strawberry' #yes
q4 = 'lettuce,apple,lemon' # no
q = [q1,q2,q3,q4]
for i,j in enumerate(t1):
for x,y in enumerate(q):
if y in j:
print(f'Items in q{x+1} are present in t1 at index: {i}')
答案是:
q2 中的项目出现在 t1 的索引处:0
q3 中的项目出现在 t1 中的索引:2
enter code here
错误
conv_to_list = [list(tup) for tup in sql_data]
表示字符串列表而不是字符串列表。
那就是 conv_to_list
是 list[list[str]] 而不是 list[str]
修复
conv_to_list = [list(tup).pop() for tup in sql_data]
完整代码
# Data from SQL Server
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
# Data to find
Orange1 = 'orange,apple,coconut'
# Converting to list and removing brackets
conv_to_list = [list(tup).pop() for tup in sql_data]
# Check for a match
check_match = Orange1 in conv_to_list
print(f'Orange1 found: {check_match}') # [1] Output: True
我是 Python 的新手,我正在尝试使用 Python 查询 Microsoft SQL 服务器数据库。数据以这种格式返回:
(('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
我想做的是检查以找到匹配项,以查看来自 SQL 服务器的数据中是否存在来自另一个数据 table 的某些数据。
当我尝试使用“in”进行检查时,它对我不起作用。我想如果我可以将数据(元组的元组)转换为列表,那么我可以更轻松地搜索和匹配。但这不起作用,因为每个列表元素周围都有一些括号。至少,我是这么想的,因为如果我在没有额外括号的情况下手动重新创建列表,那么我就可以成功搜索。
我想知道是否有办法删除那个多余的支架。或者,也许有更好的方法。我已经阅读了这里的几篇文章和其他文章,但到目前为止,我还没有找到方法。
这是我试过的。如您所见,最后一个有效,但这是我手动创建的。
# here is the data that gets returned from Msft Sql Server
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
print(sql_data)
print('type of rds_qubole_data is {}'.format(type(sql_data)))
# the type shows as <class 'tuple'>
# set some variables for checking in the data
Orange1 = 'orange,apple,coconut' in sql_data
print('orange1 is {}'.format(Orange1))
# shows orange1 is False
Vegan1 = 'lettuce,carrot,celery' in sql_data
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# then, i try to convert to a list using a method i saw in another post
conv_to_list = [list(tup) for tup in sql_data]
print(conv_to_list)
# data looks like:
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('type of conv_to_list is {} '.format(type(conv_to_list)))
# prints type of conv_to_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in conv_to_list
print('orange1 is {}'.format(Orange1))
# orange1 is False
Vegan1 = 'lettuce,carrot,celery' in conv_to_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# finally, i just manually type the list and remove the brackets around each element
manual_list = ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print(manual_list)
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('Type of manual_list is {}'.format(type(manual_list)))
# Type of manual_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in manual_list
print('orange1 is {}'.format(Orange1))
# orange1 is True
Vegan1 = 'lettuce,carrot,celery' in manual_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is True
使用for循环匹配数据:
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
found = False
for item in sql_data:
if target in item:
found = True
print(found)
输出:
True
使用列表压缩查找是否找到天气目标:
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
out = [True if target in item else False for item in sql_data]
print(True in out)
输出:
True
使用 lambda 查找天气目标值是否存在:
from functools import reduce
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
target = 'orange,apple,coconut'
found = reduce(lambda bool_value1, bool_value2:bool_value1 or bool_value2,[True if target in item else False for item in sql_data])
print(found)
输出:
True
检查下面的代码是否有帮助:
t1 = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',)) # DataBase
q1 = 'lettuce,apple,celery' # query
for i in t1:
for j in i:
if q2 in j:
print("Items present")
如果您有要查询的项目列表:
q1 = 'lettuce,apple,celery' # no
q2 = 'orange,apple,coconut' # yes
q3 = 'orange,lemon,strawberry' #yes
q4 = 'lettuce,apple,lemon' # no
q = [q1,q2,q3,q4]
for i,j in enumerate(t1):
for x,y in enumerate(q):
if y in j:
print(f'Items in q{x+1} are present in t1 at index: {i}')
答案是:
q2 中的项目出现在 t1 的索引处:0
q3 中的项目出现在 t1 中的索引:2
enter code here
错误
conv_to_list = [list(tup) for tup in sql_data]
表示字符串列表而不是字符串列表。
那就是 conv_to_list
是 list[list[str]] 而不是 list[str]
修复
conv_to_list = [list(tup).pop() for tup in sql_data]
完整代码
# Data from SQL Server
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
# Data to find
Orange1 = 'orange,apple,coconut'
# Converting to list and removing brackets
conv_to_list = [list(tup).pop() for tup in sql_data]
# Check for a match
check_match = Orange1 in conv_to_list
print(f'Orange1 found: {check_match}') # [1] Output: True