python中的单元格搜索



我有一个代码,通过在不同单元格中发现的+搜索单词(bio, username, name)但它并不完美。我怎样才能使它在搜索单词时,显示如下

为例Web3 + Founder

如果这两个词都出现在两个生物中或者一个在bio一个在username

一般来说,这两个词应该出现在这些地方

我目前正在使用循环。是否有可能通过RegEx

使它们工作?[(1161769, 1108822894, -1001568851464, 'Founder', 'None', 'Web3'),]-True

[(1161769, 1108822894, -1001568851464, 'Founder', 'None', 'None'),]-False

[(1161769, 1108822894, -1001568851464, 'Founder Web3', 'None', 'None'),]-True

[(1161769, 1108822894, -1001568851464, 'None', 'None', 'Web3 Founder'),]-True

[(1161769, 1108822894, -1001568851464, 'Founder', 'Founder', 'Web3'),]-True

data = []
for i in rows:
data.append(i)
clear_data = []
dict = {}
inp = input("Введите слова через '+': ").replace(' ', '').split('+')
lens = len(inp)
n = 0
for i in data:
for j in i:
for o in inp:
if o.lower() in str(j).lower():
n += 1
if n >= lens:
clear_data.append(i)
n = 0
else:
n = 0
cnt = 0
for i in clear_data:
cnt += 1
print(cnt,i)

我的算法并不总是给我想要的

我想我可以向你解释我的问题

我需要它找到两个或更多的单词,但同时所有这些n单词都在这三个单元格中至少在不同的地方

给定您的输入,您可以在单个列表推导中完成所有工作,该列表推导检查是否为"创始人";或";web3"在元组的最后三个元素的字符串中找到,不需要regex(尽管等效的regex将是r"(?i)founder|web3")。

lines = [
[(1161769, 1108822894, -1001568851464, "Founder", "None", "Web3")],
[(1161769, 1108822894, -1001568851464, "Founder", "None", "None")],
[(1161769, 1108822894, -1001568851464, "Founder Web3", "None", "None")],
[(1161769, 1108822894, -1001568851464, "None", "None", "Web3 Founder")],
[(1161769, 1108822894, -1001568851464, "Founder", "Founder", "Web3")],
]
web3_founder_lines = [
line
for line in lines
if any("founder" in v.lower() for v in line[0][-3:])
and any("web3" in v.lower() for v in line[0][-3:])
]
# contains
# [[(1161769, 1108822894, -1001568851464, 'Founder', 'None', 'Web3')],
#  [(1161769, 1108822894, -1001568851464, 'Founder Web3', 'None', 'None')],
#  [(1161769, 1108822894, -1001568851464, 'None', 'None', 'Web3 Founder')],
#  [(1161769, 1108822894, -1001568851464, 'Founder', 'Founder', 'Web3')]]

最新更新