我使用 4 个常见的 Python 类型检查器测试了以下代码片段,令我惊讶的是,它们都没有抱怨:
\nfrom typing import Any\n\ndef length(s: str) -> int:\n return len(s)\n\ndef any_length(o: Any) -> int:\n return length(o)\n\nif __name__ == "__main__":\n print(any_length(1234))\nRun Code Online (Sandbox Code Playgroud)\n很容易预测运行此代码将导致异常:
\nTypeError: object of type \'int\' has no len()\nRun Code Online (Sandbox Code Playgroud)\n我的:
\nSuccess: no issues found in 1 source file\nRun Code Online (Sandbox Code Playgroud)\npy类型:
\nSuccess: no errors found\nRun Code Online (Sandbox Code Playgroud)\n皮赖特:
\n0 errors, 0 warnings, 0 informations\nCompleted in 0.435sec\nRun Code Online (Sandbox Code Playgroud)\n柴堆:
\n\xc6\x9b No type errors found\nRun Code Online (Sandbox Code Playgroud)\n我期望至少有一个警告说它 …
我在做一些关于 codewars 的练习时发现了这个问题:
完成该函数
scramble(str1, str2),如果部分str1字符可以重新排列以匹配str2,则返回 true,否则返回 false。
完成并查看其他答案后我发现:
scramble=lambda a,b,c=__import__('collections').Counter:not c(b)-c(a)
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这里发生了什么吗?=为什么lambda 声明后面有 a以及参数是如何分配的???
我没有找到对此的太多解释。
Traceback (most recent call last):
File "C:\Users\nenuk\OneDrive\Desktop\Creator\32.py", line 556, in <module>
driver = uc.Chrome(options=opts)
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\site-packages\undetected_chromedriver\__init__.py", line 258, in __init__
self.patcher.auto()
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\site-packages\undetected_chromedriver\patcher.py", line 178, in auto
self.unzip_package(self.fetch_package())
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\site-packages\undetected_chromedriver\patcher.py", line 287, in fetch_package
return urlretrieve(download_url)[0]
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 240, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 215, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 521, in open
response = meth(req, response)
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 630, in http_response
response = self.parent.error(
File "C:\Users\nenuk\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 559, …Run Code Online (Sandbox Code Playgroud) def tracer(fn):
def traced(x):
print('Calling', fn, '(', x, ')')
result = fn(x)
print('Got', result, 'from', fn, '(', x, ')')
return result
return traced
def fact(n):
if n == 0:
return 1
return n * fact(n-1)
new_fact = tracer(fact)
new_fact(2)
Run Code Online (Sandbox Code Playgroud)
我在 pythontutor.com 上使用了这段代码来更好地理解高阶函数,但我很难理解为什么new_fact(2)在步骤 8 中映射到traced? 换句话说,traced函数如何知道参数是2?
该脚本在 内部定义了一个变量main(),但该变量不可用于func()在 内部运行的main()。这是为什么?
#!/usr/bin/env python3
# vars_in_func.py
# Test script for variables within a function.
def func():
print(greeting)
def main():
greeting = "Hello world"
func()
main()
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "./vars_in_func.py", line 11, in <module>
main()
File "./vars_in_func.py", line 9, in main
func()
File "./vars_in_func.py", line 5, in func
print(greeting)
NameError: name 'greeting' is not defined
Run Code Online (Sandbox Code Playgroud)
如果我将脚本转换为 Python2,错误是相同的,只是它显示的global name是而不是name.
我想我只是错过了一个关键概念。我是在学习 Bash 之后才开始学习 Python 的。
编辑:阅读完答案后,我意识到我的错误:我仍然在考虑 Bash,其中函数要么在与调用者相同的 …
mylist = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
Run Code Online (Sandbox Code Playgroud)
我想要它作为
myDict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
Run Code Online (Sandbox Code Playgroud) 我试图遍历一个函数返回的整数列表。
问题:我想检查列表中的任何元素是否大于特定值(95),然后一次执行一次语句,而不是每次迭代,即遍历列表,即使有更多元素,也要检测大于95的数字,然后执行该语句。
我已经尝试过,但该语句在满足条件的次数不停地执行
这是我正在尝试的代码
for path in Path(spath).iterdir():
for n in cosine_sim(file, path):
x = all(n)
if x > 95:
print("suceess...")
Run Code Online (Sandbox Code Playgroud)
在success...打印多次
我想导出一个带空格的变量名。在shell export命令中可以吗?
{'Truststore Filename': '/hom/truststore.jks', 'Truststore Password': 'pass'}
Run Code Online (Sandbox Code Playgroud)
$ export 'Truststore Password'=clientpass
-bash: export: `Truststore Password=clientpass': not a valid identifier
Run Code Online (Sandbox Code Playgroud)
但是我想使用export命令导出变量,因为我们要导出的变量不止一个,其名称中有空格。
但是,以下方式有效。
$ env 'Truststore Password'=clientpass 'Truststore Filename'=/hom/truststore.jks python3 script.py
Run Code Online (Sandbox Code Playgroud) 因此,如果我有这样的事情,我只想在“帐户已禁用:错误”时捕获或打印出帐户信息。
$ ipa user-find
---------------
2 users matched
---------------
User login: jpeterson
First name: john
Last name: peterson
Home directory: /home/jpeterson
Login shell: /bin/sh
UID: 855800007
GID: 855800007
Account disabled: False
User login: jsmith
First name: john
Last name: smith
Home directory: /home/jsmith
Login shell: /bin/sh
UID: 855800004
GID: 855800004
Account disabled: True
----------------------------
Number of entries returned 2
----------------------------
Run Code Online (Sandbox Code Playgroud)
预期输出为:
User login: jpeterson
First name: john
Last name: peterson
Home directory: /home/jpeterson
Login shell: /bin/sh
UID: 855800007
GID: 855800007 …Run Code Online (Sandbox Code Playgroud) 我已经编写了这段代码,它打印给定字符串的所有子字符串,但我希望它打印所有可能的子序列。
from itertools import combinations_with_replacement
s = 'MISSISSIPPI'
lst = []
for i,j in combinations_with_replacement(range(len(s)), 2):
print(s[i:(j+1)])
Run Code Online (Sandbox Code Playgroud) python ×8
bash ×2
variables ×2
combinations ×1
dictionary ×1
function ×1
lambda ×1
linux ×1
list ×1
python-3.x ×1
subsequence ×1
types ×1