2015-10-06 24 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. 
相关问题