1

我的 python 代码运行良好,但代码看起来有点乏味和混乱。我想知道是否有更简单的方法来编写它。我有一个文本文件,我需要查找是否可以在该行内找到字母“aardvark”。

if i.casefold().count('a') >= 3 and i.casefold().count('r') >= 2 and i.casefold().count('d') >= 1 and i.casefold().count('v') >= 1 and i.casefold().count('k') >=1:
4

2 回答 2

0

这是解决方案的交互式演示:

>>> i = 'this is a test'
>>> all(i.casefold().count(x) >= y for x,y in [('a',3), ('r',2), ('d', 1), ('v',1)])
False

>>> i = 'ardv'*4
>>> i
'ardvardvardvardv'

>>> all(i.casefold().count(x) >= y for x,y in [('a',3), ('r',2), ('d', 1), ('v',1)])
True
于 2017-09-14T01:34:15.267 回答
0
if all(
 i.casefold().count(letter) >= 'aardvark'.count(letter)
 for letter in 'aardvark')

有点愚蠢的解决方案,但它有效

于 2017-09-14T01:25:52.770 回答