检查字符串x
是否为子串的Pythonic方法y
是:
if x in y:
Run Code Online (Sandbox Code Playgroud)
如果找到x
就相当于a
,b
,c
,d
,e
,f
或g
也是Python的:
if x in [a,b,c,d,e,f,g]:
Run Code Online (Sandbox Code Playgroud)
但是,如果检查一些字符串x
包含两种a
,b
,c
,d
,e
,f
或g
看起来笨重:
if a in x or b in x or c in x or d in x or e in x or f in x or g in x
Run Code Online (Sandbox Code Playgroud)
是否有更多Pythonic方法检查字符串是否x
包含列表元素? …
我知道我可以通过指定--depth
标志来做一个浅层克隆.但是,这会以整数作为其值.有没有办法让一个相同的行为datetime
?我不希望克隆整个存储库并检查以前的状态.
我的印象是深度复制将所有内容递归地复制到树上,但我遇到的情况似乎与我之前的想法背道而驰。
>>> item = "hello"
>>> a["hello"] = item
>>> b = copy.deepcopy(a)
>>> id(a)
31995776
>>> id(b)
32733616 # I expected this
>>> id(a["hello"])
140651836041376
>>> id(b["hello"])
140651836041376 # I did not expect this
Run Code Online (Sandbox Code Playgroud)
a 和 b 的 id 不同,这是我所期望的,但内部项仍然是同一个对象。deepcopy 只能复制到一定深度吗?或者这是 Python 存储字符串的特定方式?(我也得到了与整数类似的结果)
Python允许您声明一个类似的函数
def print_all(*arguments):
for a in arguments:
print(a)
print_all(1,2,3)
Run Code Online (Sandbox Code Playgroud)
这允许人们传递可变数量的数据.对于我而言,这似乎比构建列表或字典更具可读性,并将其作为参数传递给我.
def print_all2(things_to_print):
for thing in things_to_print:
print(thing)
things_to_print = [1,2,3]
print_all2(things_to_print)
Run Code Online (Sandbox Code Playgroud)
第二个选项允许您为参数指定正确的名称.什么时候最好使用*arguments技术?是否有时候使用*参数更多Pythonic?
python ×3
coding-style ×1
copy ×1
date ×1
deep-copy ×1
git ×1
git-clone ×1
if-statement ×1
python-2.7 ×1
python-3.x ×1
substring ×1