2017-02-04 8 views
0

私はPythonコースのプロジェクトに取り組んでいます。私はまだ一般的にコーディングするのがかなり新しいです。私は自分のコードのスニペットの1つに問題があります。私はPythonに単語 "the"(または入力された単語は本当に重要ではありません)のすべてのインスタンスを見つけようとしています。私は "the"の後ろに単語を返すことができますが、リスト全体をスキャンするために必要なときに1つのインスタンスの後で停止します。インデックスを作成する方法は?

the_list=['the'] 
animal_list=['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 

for the_list in animal_list: 
    nextword=animal_list[animal_list.index("the")+1] 
    continue 

print(nextword) 

私は戻ってるすべてがcatdog一方とaxolotlが同様にポップアップする必要があります:

は、ここに私のコードです。私はコードをdogaxolotlの同じプロセスに通すためにforループとcontinueを使ってみましたが、うまくいきませんでした。

+0

チェックアウト:http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops – gregory

答えて

0

私はあなたが求めているものは明確ではありませんが、あなたが望むものはリストanimal_listにある動物を得ることであり、「the」という単語が偶数の部分にあると仮定すると、 ;

animals = [animal for animal in animal_list if animal != 'the'] 

あなたが初心者なので、前のコードは、forループせずにループを反復処理するための神託の方法で理解を使用して、forループを使用して、前のものと同等のコードは次のとおりです。

animals = [] 

for animal in animal_list: 
    if animal != 'the': 
     animals.append(animal) 
0

インデックスのみが最初のインスタンスを取得します。 典型的なニシキヘビの方法は、リストの内包表記を使用することです:

[animal_list[i+1] for i,val in enumerate(animal_list) if val=='the'] 
0

list.indexのみ、しかし、あなたが他のインデックスをスキップするstartstop値を指定することができ、最初に出現するでしょう。

は、今、私たちはまた、try/exceptブロックを使用する必要があり、それが一致するものを見つけられない場合はlist.indexますraiseValueErrorので。

animal_list=['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 
match = 'the' 

i = 0 
while True: 
    try: 
     i = animal_list.index(match, i) + 1 # start search at index i 
    except ValueError: 
     break 

    # can remove this check if certain that your list won't end with 'the' 
    # otherwise could raise IndexError 
    if i < len(animal_list): 
     print(animal_list[i]) 

ただし場合には、あなたがlist.index使用する必要はありません、私の代わりに、次のお勧めします。リストは'the'で終了しない場合(再度チェックを削除することができます。

for i, item in enumerate(animal_list): 
    if item == match and i + 1 < len(animal_list): 
     print(animal_list[i + 1]) 

以上のコンパクトは、リストの内包表記を使用することです。どちらの出力は'the'後のすべての項目のリストが表示されます。

animals = [ animal_list[i + 1] for i, v in enumerate(animal_list) 
      if v == match and i + 1 < len(animal_list) ] 
print(animals) 

注意:continueの使用が正しくありません。continueは、ループの現在の繰り返しを終了し、次のループに移動したい場合に使用されます。例えば

for i in range(5): 
    print(i) 
    if i == 2: 
     continue 
    print(i) 

# Output 
0 
0 
1 
1 
2 # Notice only '2' is printed once 
3 
3 
4 
4 
0

はこれを試してみてください:

the_list=['the'] 

animal_list=['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 
i=0 
for i in range(len(animal_list)): 
    if animal_list[i] in the_list: 
     nextword=animal_list[i+1] 
     print nextword 
+0

'ループ内のi = i + 1は何もしません。 – Stuart

+0

true、不要です。ありがとう –

0

一つのアプローチは、それ自体のシフトバージョンにリストを圧縮することです:

keyword = 'the' 
animal_list=['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 
zipped = zip(animal_list, animal_list[1:]) 
# zipped contains [('the', 'cat'), ('cat', 'the'), ('the', 'dog') etc.] 

found_words = [after for before, after in zipped if before == 'the'] 

これは「で終わるリストに対処します( 'the'は単に無視されます)。

0

これは、これを行うのは非常にUN-神託の方法です...しかし、おそらくそれはあなたがインデックスを理解するのに役立ちます:

animal_list = ['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 
index=0 

for x in animal_list: 
    if x == "the": 
    print(animal_list[(index + 1)]) 
    index +=1 
0
the_word = 'the' 
animal_list = ['the', 'cat', 'the', 'dog', 'the', 'axolotl'] 

# Iterate through animal_list by index, so it is easy to get the next element when we find the_word 
for i in range(len(animal_list) - 1): 
    if animal_list[i] == the_word:  # if the current word == the word we want to find 
     print(animal_list[i+1])   # print the next word 

我々はanimal_listの最後の要素をチェックしたいいけません。それで、私はanimal_listの長さから1を引きます。そうすれば、私は0,1,2,3,4の値を持つでしょう。

関連する問題