-1
入力からの単語をPythonの値に変換する方法を知りたいと思います。私はそれを返すたい入力から値をPythonに変換する
"The dog chased the cat the lion chased the dog"
:
["1,2,3,1,4,1,5,6,1,2"]
Iが重複する単語が単語が最初に発生したときと同じ値を保持したいユーザは、例えば、入力を入力します。 、 アイザック
入力からの単語をPythonの値に変換する方法を知りたいと思います。私はそれを返すたい入力から値をPythonに変換する
"The dog chased the cat the lion chased the dog"
:
["1,2,3,1,4,1,5,6,1,2"]
Iが重複する単語が単語が最初に発生したときと同じ値を保持したいユーザは、例えば、入力を入力します。 、 アイザック
def text_process(text):
'''split text to list and all the item are lower case'''
text_list = text.lower().split()
return text_list
def lookup_table(text_list):
'''build dict contain the text and number pair'''
lookup = {}
start = 0
for item in text_list:
if item not in lookup:
lookup[item] = start + 1
start += 1
return lookup
if __name__ == '__main__':
text = "The dog chased the cat the lion chased the dog"
text_list = text_process(text)
lookup_table = lookup_table(text_list)
out_put = [lookup_table[text]for text in text_list]
print(out_put)
アウトの助けを
l = input("Enter a sentence").lower lists= list(l) valueindex = range(len(lists)) print(valueindex)
ありがとう:私は、私は現時点では、次のコードを使用しています。この複数の方法を行うことを試みたが、ランダムなインデックスを返すようです:
[1, 2, 3, 1, 4, 1, 5, 3, 1, 2]
私はそれを閉じた25分後にこの質問にどのように回答したかについて、私は好奇心が強いです。私はいつもすぐに通知を見る。 – TigerhawkT3
はい、重複しています。私は非常に慎重にチェックした。私は同じことをすることをお勧めします。 – TigerhawkT3
@ TigerhawkT3私の間違い、あなたの時間のおかげで。 –