我有一个只能返回的函数a
,b
或者c
它们都属于类型,T
但是我想将它的一部分签名这个事实,因为它们在函数的上下文中具有特殊含义,我是如何做到的?
目前我用这个
def fun(...) -> "a or b or c":
#briefly explain the meaning of a, b and c in its docstring
Run Code Online (Sandbox Code Playgroud)
那是正确的吗?
我知道我可以做到这一点
def fun(...) -> T:
#briefly explain the meaning of a, b and c in its docstring
Run Code Online (Sandbox Code Playgroud)
但正如我所说,我想在签名中表达该函数只返回那些特定的值
最近我开始研究 Cython,并且 Anaconda 发行版有这个方便的脚本cythonize
,它允许做
>cythonize -i foo.pyx
Run Code Online (Sandbox Code Playgroud)
就地编译文件。
我的问题是如何使用该脚本包含 numpy 以便我cimport numpy
正常工作?
我试过
>cythonize -X include_path=C:\Anaconda3\lib\site-packages\numpy\core\include -i foo.pyx
>cythonize -s include_path=C:\Anaconda3\lib\site-packages\numpy\core\include -i foo.pyx
Run Code Online (Sandbox Code Playgroud)
也有include_dir=...
,include_dirs=...
都带有 -X 和 -s
如果我使用 -XI 得到
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\cythonize-script.py", line 5, in <module>
sys.exit(Cython.Build.Cythonize.main())
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 185, in main
options, paths = parse_args(args)
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 172, in parse_args
options, args = parser.parse_args(args)
File "C:\Anaconda3\lib\optparse.py", line 1387, in parse_args
stop = self._process_args(largs, rargs, …
Run Code Online (Sandbox Code Playgroud) 在python中是否有内置函数,numpy或其中一个库可以得到像这样的系列之和:
list1 = [2,3,4]
list2 = [3,3,3]
Run Code Online (Sandbox Code Playgroud)
其中x和y是列表,L是x或y的长度.
最后,如果没有内置函数那样做,我尝试了另外一个代码,如:
Total = sum ((i for i in list1) * (j for j in list2))
Run Code Online (Sandbox Code Playgroud)
当然,它不起作用但我需要靠近这个或附近的东西:
Total = sum (i * j for i in list1 and j in list2 )
Run Code Online (Sandbox Code Playgroud)
注意:我可以构建自己的功能,但我正在寻找一个简单,快速或内置的功能,所以请不要给我自己的功能.
编辑:我想要一般表格这样做,所以我可以使用这个表格,作为一个例子Log(n)
或系列中的另一种数学.
我使用Python3并将结果写入这样的文件中:
with open(output,'w') as f:
f.write('Line count of the log files is: ' + str(line_count) + '. \n')
Run Code Online (Sandbox Code Playgroud)
f.write()
自动返回#个写入的字符,有没有办法不输出呢?我问这个是因为我不希望它输出。
谢谢。
例如,如果字典正在{0:0, 1:0, 2:0}
制作一个列表:[0, 0, 0]
.
如果这是不可能的,你如何取字典的最小值,意思是字典:{0:3, 1:2, 2:1}
返回1
?
为什么它没有给出正确的第一个甚至斐波纳契数最多达到400万的总数?
x = 1
y = 2
list = [1,2]
while y< 4000000:
z= x+y
x=y
y=z
list.append (y)
list_even = []
for a in list:
if a%2 == 0:
list_even.append (a)
else:
pass
total = sum(list_even)
print (total)
Run Code Online (Sandbox Code Playgroud) python ×5
numpy ×2
python-3.x ×2
anaconda ×1
cython ×1
dictionary ×1
list ×1
literals ×1
python-2.7 ×1
type-hinting ×1
windows-10 ×1