2016-11-22 3 views
-6

上の位置をリストこれまでのところ、私はあなたが文に入れた場合、私はそれを作りたい、このは、どのように私のコードは、文がテキストファイル

UserSen = input("Enter a sentence of your choice, ") 
position = {} # word -> position 
words = UserSen.split() 
for word in words: 
    if word not in position: # new word 
     position[word] = len(position) + 1 # store its position 

は、のようなものを入れて持って分析するI作るのですか"HIさんはいかがですか" 1,2,3,4,1,3,4,1

+5

ここで勉強しないでください - ウェブを検索し、それが何を意味するのか調べてみましょう。 –

+0

インデントなしでPythonコードを投稿しないでください。 – khelwood

+0

あなたはほとんどそれを持っています。 – user

答えて

0

これまでに見つかった位置にリストを保存する必要があります:

# UserSen = "HI HOW ARE YOU HI ARE YOU HI" 
UserSen = input("Enter a sentence of your choice, ") 

# word -> positionDictionary 
positionDictionary = {} 
positionList  = [] 
words    = UserSen.split() 

for word in words: 

    # print(word) 

    # new word 
    if word not in positionDictionary: 

     # store its positionDictionary 
     positionDictionary[word] = len(positionDictionary) + 1 

    positionList.append(positionDictionary[word]) 

    # print(positionDictionary) 
    # print(positionList) 


print(positionList) 
関連する問題