2015-10-06 65 views
-2

我期待開發一個程序,可以識別句子中的單個單詞,並用每個單詞在列表中的位置替換每個單詞。從其中的每個單詞的位置重新創建一個句子

例如,有這樣一句話:

HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON 

這包含11個不同的話,我想我的程序重新創建這些11個字在列表中的位置了一句:

1,2,3,4,5,6,7,8,9,10,5,11,6,7 

然後我想將這個新列表保存在一個單獨的文件中。到目前爲止,我只得到了這一點:

#splitting my string to individual words 
my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"  
splitted = my_string.split() 

回答

1
>>> my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"  
>>> splitted = my_string.split() 
>>> order = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 11, 6, 7 
>>> new_str = ' '.join(splitted[el] for el in order) 
'I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP IN ME PYTHON PLEASE' 

更新根據您的評論:

您正在尋找index()方法。

my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"  
splitted = my_string.split() 
test = "I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP IN ME PYTHON PLEASE".split() 
print ', '.join(str(splitted.index(el)) for el in test) 

>>> 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 11, 6, 7 

**我們假設有沒有重複的話

+0

我認爲數應該是'[1,2,3,4,5,6,7,8,9,10,11,5,7]' – SirParselot

+0

@SirParselot元組或列表中沒有區別(這只是常量) –

+0

不,我是指數字的順序。 – SirParselot

0
my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"  
splitted = my_string.split() 
d = {} 
l=[] 
for i,j in enumerate(splitted): 
    if j in d: 
     l.append(d[j]) 
    else: 
     d[j]=i 
     l.append(i) 
print l 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 11, 5, 6] 
+0

非常感謝!這真的幫了我:) :) –

+0

@ new.teacher不要忘記upvote然後標記幫助最多的答案 – SirParselot

0

試試這個:

sentence= "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON" 
lst = sentence.split() 
lst2= [] 

for i in lst: 
    if i not in lst2: 
     lst2.append(i) 

inp = inputSentence.split() 
output=[] 
for i in inp: 
    print lst2.index(i)+1, 
    output.append(lst2.index(i)+1) 

該指數是評估並存儲在lst2。您只需將您的輸入字符串傳遞給inputSentence,以便您可以測試此代碼。

0

嘗試:

>>> from collections import OrderedDict 
>>> my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON" 
>>> splitted = my_string.split() 
>>> key_val = {elem : index + 1 for index, elem in enumerate(list(OrderedDict.fromkeys(splitted)))} 
>>> [key_val[elem] for elem in splitted] 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 11, 6, 7] 

list(OrderedDict.fromkeys(splitted))創建具有splitted只有唯一元素的列表。
key_val是這些獨特元素的字典,作爲鍵和它們的索引作爲值。

0
benstring = input ('enter a sentence ') 

print('the sentence is', benstring) 

ben2string = str.split(benstring) 

print('the sentence now looks like this',ben2string) 

x=len(ben2string) 
for i in range(0,x): 
    if x is 
+4

請解釋! –

0
Sentence = input("Enter a sentence!") 
s = Sentence.split() #Splits the numbers/words up so you can see them individually. 
another = [0] 

for count, i in enumerate(s): 
    if s.count(i) < 2: 
     another.append(max(another) + 1) #adds +1 each time a word is used, showing the position of each word. 
    else: 
     another.append(s.index(i) +1) 

another.remove(0) 

print(another) #prints the number that word is in. 
相關問題