[英]I have a list where is are strings, I want to remove the [ ]
Python 程序,其中我有来自 pandas 的单词列表:
a = [['how to get my bill'], ['Where can I locate my student records'], ['Where can I locate my GPA'], ['how do I find my bill'], ['How do I find my student records']]
b =[["'GPA', 'G P A', 'G.P.A'"], ["'academic standing'"], ["'my bill', 'bill'"], ["'student records'"], ["'time sheet', 'timesheet'"]]
output 我期待的是['how to get my bill','how to get bill','Where can I locate my student records','Where can I locate my GPA', 'Where can I locate my GPA','Where can I locate my GP A','how do I find my bill','How do I find my student records']
a = [['how to get my bill'], ['Where can I locate my student records'], ['Where can I locate my GPA'], ['how do I find my bill'], ['How do I find my student records'], ['where can I find my bill'], ['where can I locate my bill'], ['where do I find GPA'], ['I want my student records'], ['Where do I find my academic standing'], ['Where do I find my student records'], ['where do I I find my GPA']]
b = [['GPA', 'G P A', 'G.P.A'], ['academic standing'], ['my bill', 'bill'], ['student records'], ['time sheet', 'timesheet']]
output = []
for i in a:
for word in i.split():
if word in b:
output += [i.replace(word, other) for other in b if other != word]
q = output
print(output)
您的代码中的问题是 b 是列表列表,您需要检查 b 中每个子列表中的单词,因此您需要在 b 上进行第二次循环。 如果b
的内容确实如您所示,则列表列表中的每个字符串都需要删除内引号并拆分为单独的字符串,因此我添加了b = [x[0].replace("'", "").split(',') for x in b]
来做到这一点。
它与您所需的 output 与if other != word
不完全匹配,因此它不会重现与原始单词匹配的任何内容。 如果这是你想要的,你可以删除if
。
由于使用了.split()
,您编写的代码也不会处理多词短语。 例如,它会找到“bill”并将其替换为“my bill”,但您需要完全不同的逻辑来查找单词组合。
a = [['how to get my bill'], ['Where can I locate my student records'], ['Where can I locate my GPA'], ['how do I find my bill'], ['How do I find my student records'], ['where can I find my bill'], ['where can I locate my bill'], ['where do I find GPA'], ['I want my student records'], ['Where do I find my academic standing'], ['Where do I find my student records'], ['where do I I find my GPA']]
b =[["'GPA', 'G P A', 'G.P.A'"], ["'academic standing'"], ["'my bill', 'bill'"], ["'student records'"], ["'time sheet', 'timesheet'"]]
b = [x[0].replace("'", "").split(',') for x in b]
output = []
for i in a:
for word in i[0].split():
for replacements in b:
if word in replacements:
output += [i[0].replace(word, other) for other in replacements if other != word]
q = output
print(output)
Output 是:
['how to get my my bill', 'Where can I locate my G P A', 'Where can I locate my G.P.A', 'how do I find my my bill', 'where can I find my my bill', 'where can I locate my my bill', 'where do I find G P A', 'where do I find G.P.A', 'where do I I find my G P A', 'where do I I find my G.P.A']
如果你翻转逻辑循环(循环遍历 b 中每个子列表中的条目以获取 a 中的每个子列表),你会更接近你想要的(至少一开始是这样)。
a = [['how to get my bill'], ['Where can I locate my student records'], ['Where can I locate my GPA'], ['how do I find my bill'], ['How do I find my student records'], ['where can I find my bill'], ['where can I locate my bill'], ['where do I find GPA'], ['I want my student records'], ['Where do I find my academic standing'], ['Where do I find my student records'], ['where do I I find my GPA']]
b =[["'GPA', 'G P A', 'G.P.A'"], ["'academic standing'"], ["'my bill', 'bill'"], ["'student records'"], ["'time sheet', 'timesheet'"]]
b = [x[0].replace("'", "").split(',') for x in b]
output = []
for i in a:
for replacements in b:
for j in replacements:
if j in i[0]:
output += [i[0].replace(j, other) for other in replacements]
break
q = output
print(output)
Output 是:
['how to get my bill', 'how to get bill', 'Where can I locate my student records', 'Where can I locate my GPA', 'Where can I locate my G P A', 'Where can I locate my G.P.A', 'how do I find my bill', 'how do I find bill', 'How do I find my student records', 'where can I find my bill', 'where can I find bill', 'where can I locate my bill', 'where can I locate bill', 'where do I find GPA', 'where do I find G P A', 'where do I find G.P.A', 'I want my student records', 'Where do I find my academic standing', 'Where do I find my student records', 'where do I I find my GPA', 'where do I I find my G P A', 'where do I I find my G.P.A']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.