2016-06-19 6 views
0

これは私の現在の進歩です。私はPythonに若干の新しさを感じています。私は本当に問題を解決する方法を正確には知らないし、タイトルがやや誤解を招く場合はお詫び申し上げます。私はできる限り最善の方法で問題を説明しようとします。リスト内の単語に番号を割り当てるにはどうすればよいですか?


出力はリスト形式でこれをする必要があります:

"WHAT", "IS", "MINE", "IS", "YOURS", "AND", "WHAT", "IS", "YOURS", "IS", "MINE" 

"1", "2", "3", "2", "4", "5", "1", "2", "4", "2", "3" 

"1""WHAT"で、"2""IS"ある...というように。単語が複数回表示される場合は、同じ番号のままになります。これは動作するはず


コード

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE"; 

sentence = sentence.lower(); 

sentence = sentence.split(); 

uniqueWord = []; 

store = []; 

for i in sentence: 
    if i not in uniqueWord: 
     uniqueWord.append(i); 

lengthOfUniqueWord = len(uniqueWord); 

print(sentence); 

print(uniqueWord); 

for i in range(lengthOfUniqueWord): 
    i = str(i+1); 
    store.append(i); 

print(store); 

for positions in enumerate(uniqueWord, 1): 
    print(positions); 

出力

['what', 'is', 'mine', 'is', 'yours', 'and', 'what', 'is', 'yours', 'is', 'mine'] 
['what', 'is', 'mine', 'yours', 'and'] 
['1', '2', '3', '4', '5'] 
(1, 'what') 
(2, 'is') 
(3, 'mine') 
(4, 'yours') 
(5, 'and') 
+0

は、サイトへようこそ!最も効果的にヘルプを得る方法の詳細については、[ツアー](http://stackoverflow.com/tour)を参照してください。あなたはあなたが望む出力(すでに含まれている)に加えて*現在*取得している出力を含めるには、[あなたの質問を編集してください](http://stackoverflow.com/posts/37910190/edit)をお願いしますか? – cxw

答えて

1

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE";  
sentence = sentence.lower(); 
sentence = sentence.split(); 

uniqueWord = []; 

for i in sentence: 
    if i not in uniqueWord: 
     uniqueWord.append(i); 

for word in sentence: 
    print uniqueWord.index(word) + 1 

ここでは、へのリンクですindex

0

のドキュメント他の人も私が思いついた;-)回答型付けながら - それはまた、Pythonのコーディングに関する更なるヒントが含まれている願っています:Pythonの2.7.11で(これは私のシステムで提供します

#! /usr/bin/env python 
from __future__ import print_function 


def word_indexer(text): 
    """Split the text in words maintaining order and return two 
    aligned lists: 1) the words all in sequence, and 2) the matching 
    unique 1-based case insensitive index (insert based rank).""" 

    words_in_order = text.split() 
    word_index = [] 
    unique_word_rank = {} 
    rank = 1 
    for word in words_in_order: 
     normalized_word = word.lower() 
     if normalized_word not in unique_word_rank: 
      unique_word_rank[normalized_word] = rank 
      rank += 1 
     word_index.append(unique_word_rank[normalized_word]) 

    return words_in_order, word_index 


def main(): 
    """Do the word indexing.""" 
    sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE" 
    words_in_order, word_index = word_indexer(sentence) 
    # print as in question in two lines: 
    print(words_in_order) 
    print(word_index) 
    # to display like a table: 
    for ndx, word in zip(word_index, words_in_order): 
     print(ndx, word) 

if __name__ == '__main__': 
    main() 

が、 Python 3.5.1でも動作します):

['WHAT', 'IS', 'MINE', 'IS', 'YOURS', 'AND', 'WHAT', 'IS', 'YOURS', 'IS', 'MINE'] 
[1, 2, 3, 2, 4, 5, 1, 2, 4, 2, 3] 
1 WHAT 
2 IS 
3 MINE 
2 IS 
4 YOURS 
5 AND 
1 WHAT 
2 IS 
4 YOURS 
2 IS 
3 MINE 

これが役立ちます - そしてハッピーハッキング!

+1

広範なライセンスの下で寄付をライセンス供与してくれてありがとう!私もそのようにしており、サイトのユーザーの生活を楽にしている他の人たちを見ると、いつも励まされています! :) – cxw

+1

偉大なメタファーのフィードバック:-)私は私のco(de | n)の賛辞のどれかを自分自身と考えているのではなく、他の人が恐怖を感じなく答えをコピー&ペーストできるようにすることは、Q&Aコーディングサイト。結局のところ、コードを実行する準備ができたコピーとペーストを提供するよう人々に促す... – Dilettant

0

新しい単語がdict dに挿入された場合にのみインクリメントするcountイテレータを設定します。各単語の一致数から最終的なリストをビルドします。ここでは

from itertools import count 

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE" 
s = sentence.split() 
c = count(1) 
d = {} 

for word in s: 
    if word.lower() not in d: 
     d[word.lower()] = next(c) 

result = [str(d[word.lower()]) for word in s] 
# ['1', '2', '3', '2', '4', '5', '1', '2', '4', '2', '3'] 
0

は、同じ目標を達成するための別の方法である:

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE".lower().split() 
uniqueWord = list(set(sentence)) 

print(sentence); 
print(uniqueWord); 

store = [uniqueWord.index(x) for x in sentence] 

print(store); 
関連する問題