簡體   English   中英

我有一個列表,其中是字符串,我想刪除 [ ]

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM