- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这样一个功能:
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
if (endWord not in wordList) or (beginWord not in wordList):
return 0
多个bool操作很麻烦。
if (endWord not in wordList) or (beginWord not in wordList):
return 0
如何让它变得简洁明了?
最佳答案
如果你所有的 if
-block 都是这样的:
if (endWord not in wordList) or (beginWord not in wordList):
return 0
else: # <- I am assuming this, see Note 1
return 1
然后你可以将整个东西替换为:
return int(all(x in wordList for x in (endWord, beginWord)))
Note 1
Not having an
else
clause is generally perfectly fine but in your case you would have a function that might return0
orNone
and that is not optimal\recommended. If you can, redesign it as per above.If not, I wouldn't bother changing the condition. The one you have is very readable and there are no alternatives that are much better. Sure you could do:
if not all(x in wordList for x in (endWord, beginWord)):
return 0but that is pretty much it.
关于python - wordList 中的两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490414/
我有这样一个功能: def ladderLength(self, beginWord, endWord, wordList): """ :type beginWord: str
我在 Eclipse-android 中构建一个 c 项目并得到以下错误: 这是一个 ndk 构建错误(与我的代码无关) /Users/eladb/MyWorkspace/android-ndk-r8
有没有办法在 WORDLIST 中使用正则表达式? 我需要实现与 https://issues.apache.org/jira/browse/UIMA-3382 中提到的相同的内容. 或者有什么替代方
我有一个列表 stopWord(每个字符串只有一个单词),我有一个字符串短语(至少 2 个单词)。我想检查我的短语是否包含 Java 中的 stopWord 元素之一。我怎样才能做到这一点? if(!
这样做的最佳方法是什么?这是一个 250GB 的文本文件,每行 1 个单词 输入: 123 123 123 456 456 874 875 875 8923 8932 8923 想要的输出: 123
我正在为 dot file management utility 编写 bash 完成脚本.该工具有一个命令 dots diff [filename],它将显示已安装的点文件和源点文件之间的差异。它还
[root@unknowna08cfd77f899 crunch-3.6]# ls charset.lst COPYING crunch crunch.1 crunch.c Makefile
我是一名优秀的程序员,十分优秀!