2016-04-13 3 views
0

今はすべて私のコードはすべてaのすべての私の言葉を見つけることです。私がしたいのは、その文字を使ってタイプする文字で始まる単語を見つけることだけです。例えば。 Aは、activity,againおよびagoという単語を見つけるでしょう。私は必要な答えをどこから探しても見つけられませんでした。私は文字で始まる単語を見つけるプログラムの援助が必要です

dictionary=["activity","again","ago","begin","behaviour","beyond","camp","cannon","cell","discussion","doctor","display","else","estimate","establish","fudge","flight","fight","gear","great","grunt","how","hoe","house","impact","image","implication","just","job","judge","keep","key","kai"] 
newlist=[] 
choice = '' 
while choice != 'q': 

    choice = input("?") 
    if choice == 'a': 
    for a in dictionary: 
     newlist.append(choice.lower()) 
     print(newlist) 

答えて

1
dictionary=["activity","again","ago","begin","behaviour","beyond","camp","cannon","cell","discussion","doctor","display","else","estimate","establish","fudge","flight","fight","gear","great","grunt","how","hoe","house","impact","image","implication","just","job","judge","keep","key","kai"] 
newlist=[] 
choice = '' 
while choice != 'q': 

choice = input("?") 
if choice == 'a': 
    for item in dictionary: 
     if(item[0] == choice): 
      newlist.append(item) 
    print(newlist) 

あなたの上記のコードは、辞書内の各項目の最初の文字と照合されていません。辞書内のすべての項目を実行していて、「選択」(この場合は「a」)を「newList」に追加してから「newList」に現在の項目を印刷することです。

辞書の各項目の最初の文字を選択してから「newList」に追加してください。

+0

ありがとうございます!私は現在、配列の印刷後にループの問題を解決しようとしていますが、それを並べ替えることができます。 –

+0

あなたがそれ以上の問題に遭遇したら私に教えてください:) –

0

コードを必要とする人は、しばらくコードを印刷してください。これは、それに役立つはず:それはあなたのケースでstring[0]と同等です:)

count = 0 
while count < len(newlist): 
    count = count + 1 
    print(newlist) 
    length = len(newlist) 
1

Pythonの文字列は、実際にstartswithメソッドを持っています。

dictionary=["activity","again","ago","begin","behaviour","beyond","camp","cannon","cell","discussion","doctor","display","else","estimate","establish","fudge","flight","fight","gear","great","grunt","how","hoe","house","impact","image","implication","just","job","judge","keep","key","kai"] 
newlist=[] 
choice = '' 
while choice != 'q': 
    choice = input("?") 
    for a in dictionary: 
     if a.startswith(choice.lower()): 
      newlist.append(a) 
    print(newlist) 
関連する問題