私がやろうとしていること:単語の逆が小文字の単語のセットにある単語のリストを作成して印刷します。たとえば、「timer」とその逆の「remit」は、両方とも小文字の単語リストに含まれています。
私がこれまでに持っているもの:
s = set(lowers)
[word for word in s if list(word).reverse() in s]
空のリストを取得するだけです。
私がやろうとしていること:単語の逆が小文字の単語のセットにある単語のリストを作成して印刷します。たとえば、「timer」とその逆の「remit」は、両方とも小文字の単語リストに含まれています。
私がこれまでに持っているもの:
s = set(lowers)
[word for word in s if list(word).reverse() in s]
空のリストを取得するだけです。
[::-1]
ここでは list() を使用する必要はなく、リストを in-place に変更するとlist(word).reverse()
戻ります。None
を使用することもできますが、十分に単純"".join(reversed(word))
だと思います。word[::-1]
[word for word in s if word[::-1] in s]
In [193]: word="timer"
In [194]: print list(word).reverse()
None
In [195]: word[::-1]
Out[195]: 'remit'
In [196]: "".join(reversed(word))
Out[196]: 'remit'
このlist.reverse()
メソッドは、その場でリストを反転します。その戻り値はNone
です。文字列を逆にしたい場合は、 を使用してword[::-1]
ください。
list.reverse()
リストを反転し、何も返しません。が必要"".join(reversed(list))
です。
これを試して:
s = set(lowers)
[word for word in s if word.lower()[::-1] in s]
[::-1]
文字列を逆にするため に使用する必要があります-
[word for word in s if word[::-1] in s]