2016-02-11 42 views
0

我想在另一個列表中的列表的片標點的索引處插入一個句子:的Python - 插入可變(多於一個號碼列表)

for x in range(len(final1)): 
    final1.insert(punc_num[x], punct[x])- WHY WONT THIS WORK? 

任何幫助麻煩理解: - )

整個代碼:

所有的
f = open("file.txt","r") 
sentence= f.read() 
print (sentence) 

punctuations = ("'", "!", "(", ")", "-", "[", "]", "{", "}", ";", ":", '"', "<", ">", ".", "/", "?", "@", "#", "$", "%", "^", "&", "*", "_", "~", ",") 

punc_num=[] 

for x in sentence: 
    if x in punctuations: 
     punc_num.append(sentence.index(x)) 

print(punc_num) 


punct=[] 

for x in sentence: 
    if x in punctuations: 
     punct.append(x) 

print(punct) 





no_punct= "" 

for y in sentence: 
    if y not in punctuations: 
     no_punct = no_punct + y 

print (no_punct) 





no_punct=no_punct.split() 

final= (" ".join(sorted(set(no_punct), key=no_punct.index))) 
print(final) 
storing=[] 

for x in no_punct: 

    storing.append (no_punct.index(x)) 

print (storing) 


final1= (" ".join(sorted(set(no_punct), key=no_punct.index))) 
print(final1) 




for x in range(len(final1)): 
    final1.insert(punc_num[x], punct[x]) 
+0

1'str'沒有'insert'方法。 2.如果你正在考慮'list.insert()'方法,它的參數應該是'index,object',而不是'object,object'。 – zondo

+0

好吧,如果我將句子分成列表,我將如何插入索引(它在列表中) – Admin

+0

其實,我的第二點是無效的;你*是*使用一個數字。爲了得到你想要的結果,你應該在'for'循環前加上'no_punct_list = list(no_punct)',然後在調用'insert()'時使用'no_punct_list'而不是'no_punct'。在for循環之後,你可以說'no_punct =「」.join(no_punct_list)'。如果這解決了你的問題,我會把它放在一個答案。 – zondo

回答

0

首先,當你使用final1.insert()final1是一個字符串,而不是一個列表。你可以這樣做更新:

final1_list = [] 
for x in range(len(punc_num)): 
    final1_list.insert(punc_num[x], punct[x]) 
final1 = "".join(final1_list) 

其次,當你定義punc_num,您使用list.index()方法。由於list.index()將始終返回第一個其參數的出現,所以任何給定的標點符號將始終具有相同的索引,而不管它出現在字符串中的位數有多少。您可以在迴路改成這樣:

punc_num=[] 
for x in sentence: 
    if x in punctuations: 
     index = 0 
     while index + sentence[index:].index(x) in punc_num: 
      index += sentence[index:].index(x) + 1 
     punc_num.append(index + sentence[index:].index(x)) 

你的整個程序應該是這樣的:

f = open("file.txt","r") 
sentence = f.read() 
print (sentence) 

punctuations = ("'", "!", "(", ")", "-", "[", "]", "{", "}", ";", ":", '"', "<", ">", ".", "/", "?", "@", "#", "$", "%", "^", "&", "*", "_", "~", ",") 

punc_num=[] 

for x in sentence: 
    if x in punctuations: 
     index = 0 
     while index + sentence[index:].index(x) in punc_num: 
      index += sentence[index:].index(x) + 1 
     punc_num.append(index + sentence[index:].index(x)) 

print(punc_num) 


punct=[] 

for x in sentence: 
    if x in punctuations: 
     punct.append(x) 

print(punct) 





no_punct= "" 

for y in sentence: 
    if y not in punctuations: 
     no_punct = no_punct + y 

print (no_punct) 



no_punct=no_punct.split() 

final= (" ".join(sorted(set(no_punct), key=no_punct.index))) 
print(final) 
storing=[] 

for x in no_punct: 

    storing.append (no_punct.index(x)) 

print (storing) 


final1= (" ".join(sorted(set(no_punct), key=no_punct.index))) 
print(final1) 




final1_list = list(final1) 
for x in range(len(punc_num)): 
    final1_list.insert(punc_num[x], punct[x]) 
final1 = "".join(final1_list) 
+0

非常感謝。對不起,浪費你的時間,但我試圖從沒有標點符號(no_punct)的句子和標點符號(punc_num)和標點符號(點)非常感謝你的句子列表中添加標點符號。當我運行它時說: – Admin

+0

>>> ================================ RESTART ====== ========================== >>> 你好我的名字是丹。 [8,21] [ ' ''。'] 我的名字是擔 我的名字是擔 [0,1,2,3,4] 我的名字是擔 [ ] – Admin

+0

代碼: no_punct = no_punct.split() 最終=(」」。加入(排序(集(no_punct),鍵= no_punct.index))) 打印(最終) 存儲= [] 對於x在no_punct: storage.append(no_punct.index(x)) print(stored) final1 =(「」.join(sorted(set(no_punct),key = no_punct.index))) 打印(final1) punc_num = [] 用於句子X: 如果x在標點: 索引= 0 而指數+句[指數:]在punc_num索引(X): 指數+ =句子[指數:]。index(x)+ 1 punc_num.append(index + sentence [index:]。index(x)) – Admin