0

たとえば、リストが与えられた場合、

F=['MIKE', 'BALL', 'FISH', 'BAT', 'BEAR', 'CAT', 'AT', 'ALLY']

ユーザーが入力した特定の長さのすべての単語を見つけて、上記のリストを反復処理するにはどうすればよいですか? だろうと思った..

number=input("How long would you like your word to be?")
possible_words=[]
for word in F:
     if word(len)=number:
          possible_words+=word 
4

4 に答える 4

6

これlistにより、長さがN

possible_words = [x for x in F if len(x) == N]

list辞書ではなくがあることに注意してください

于 2013-04-10T19:34:35.470 に答える
2

ここでも使用できfilterます:

 filter(lambda x:len(x)==number, F)

ヘルプ (フィルター) :

In [191]: filter?
Type:       builtin_function_or_method
String Form:<built-in function filter>
Namespace:  Python builtin
Docstring:
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If
function is None, return the items that are true.  If sequence is a tuple
or string, return the same type, else return a list.
于 2013-04-10T19:37:11.313 に答える
0

組み込みのフィルター機能を使用できます。

print filter(lambda word: len(word) == N, F)
于 2013-04-11T14:14:51.963 に答える