我想看看str.split()在 Python 中是如何实现的这是我尝试过的:
> inspect.getsource(str.split)
TypeError: <method 'split' of 'str' objects> is not a module,
class, method, function, traceback, frame, or code object
Run Code Online (Sandbox Code Playgroud)
复制 StackOverflow 上的另一个示例不起作用:Python 中最大公约数的代码
我想为数字列表计算gcd。但是我不知道我的代码有什么问题。
A = [12, 24, 27, 30, 36]
def Greatest_Common_Divisor(A):
for c in A:
while int(c) > 0:
if int(c) > 12:
c = int(c) % 12
else:
return 12 % int(c)
print Greatest_Common_Divisor(A)
Run Code Online (Sandbox Code Playgroud) 问题如下:
创建一个接受 2 个输入、一个数字
n和一个列表的函数lst。该函数应返回一个列表,其中lst包含与n(1 除外)没有共同因子的所有数字。n 和 中的所有数字lst均为大于或等于 0 的正整数。
我的尝试
def no_common_factors (n, lst):
def uf(n): #get a set of factors
factors = []
i = 2
while n > 1:
if n % i == 0:
factors += [i]
n = n / i
else:
i += 1
return set(factors)
factors_n = uf(n)
no_common = []
for i in range(0, len(lst)):
factors_i = uf(i)
if factors_n.isdisjoint(factors_i):
no_common += [lst[i]] …Run Code Online (Sandbox Code Playgroud)