- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
执行以下操作最惯用的方法是什么?
def xstr(s):
if s is None:
return ''
else:
return s
s = xstr(a) + xstr(b)
更新:我正在采纳 Trypich 的使用 str(s) 的建议,这使得该例程适用于字符串以外的其他类型。我对 Vinay Sajip 的 lambda 建议印象深刻,但我想让我的代码相对简单。
def xstr(s):
if s is None:
return ''
else:
return str(s)
最佳答案
可能最短的是str(s or '')
因为 None 为 False,如果 x 为 false,“x or y”返回 y。见 Boolean Operators详细说明。它很短,但不是很明确。
关于string - Python:将 None 转换为空字符串的最惯用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1034573/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!