对于没有comp-sci背景的人来说,计算机科学领域的lambda是什么?
language-agnostic theory lambda computer-science terminology
Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.
我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?
例如:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud) 简单的例子.两种方法,一种叫另一种方法:
def method_a(arg):
some_data = method_b(arg)
def method_b(arg):
return some_data
Run Code Online (Sandbox Code Playgroud)
在Python中,我们可以def在另一个内部声明def.因此,如果method_b需要并且仅从中调用method_a,我应该method_b在内部声明method_a吗?像这样 :
def method_a(arg):
def method_b(arg):
return some_data
some_data = method_b
Run Code Online (Sandbox Code Playgroud)
或者我应该避免这样做?
我怎么能写一个相当于的lambda表达式:
def x():
raise Exception()
Run Code Online (Sandbox Code Playgroud)
以下是不允许的:
y = lambda : raise Exception()
Run Code Online (Sandbox Code Playgroud) 当我遇到这种情况时我可以用javascript来做,我总是认为如果有一个foreach功能就会方便.通过foreach我的意思是下面描述的功能:
def foreach(fn,iterable):
for x in iterable:
fn(x)
Run Code Online (Sandbox Code Playgroud)
他们只是在每个元素上做它并且没有产生或返回一些东西,我认为它应该是一个内置函数,并且应该比用纯Python编写它更快,但我没有在列表中找到它,或者它只是叫另一个名字?或者我在这里错过了一些观点?
也许我错了,因为调用Python中的函数成本很高,对于这个例子来说绝对不是一个好习惯.该函数不应该是一个out循环,而应该在它的正文中执行循环,如下所示,在许多python的代码建议中已经提到过:
def fn(*args):
for x in args:
dosomething
Run Code Online (Sandbox Code Playgroud)
但我认为foreach仍然受到以下两个事实的欢迎:
如何将参数绑定到Python方法以存储一个用于以后调用的nullary仿函数?与C++类似boost::bind.
例如:
def add(x, y):
return x + y
add_5 = magic_function(add, 5)
assert add_5(3) == 8
Run Code Online (Sandbox Code Playgroud) 该list.index(x)函数返回值为的第一个项的列表中的索引x.
是否有一个函数,list_func_index()类似于index()具有函数的函数f(),作为参数.该函数在列表的f()每个元素上运行e,直到 f(e)返回True.然后list_func_index()返回索引e.
Codewise:
>>> def list_func_index(lst, func):
for i in range(len(lst)):
if func(lst[i]):
return i
raise ValueError('no element making func True')
>>> l = [8,10,4,5,7]
>>> def is_odd(x): return x % 2 != 0
>>> list_func_index(l,is_odd)
3
Run Code Online (Sandbox Code Playgroud)
有更优雅的解决方案吗?(以及该功能的更好名称)
在Python 3中,可以格式化一个字符串,如:
"{0}, {1}, {2}".format(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
但是如何格式化字节?
b"{0}, {1}, {2}".format(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
加油AttributeError: 'bytes' object has no attribute 'format'.
如果没有format字节方法,如何格式化或"重写"字节?
目前,在Python中,函数的参数和返回类型可以是类型提示,如下所示:
def func(var1: str, var2: str) -> int:
return var1.index(var2)
Run Code Online (Sandbox Code Playgroud)
表示该函数接受两个字符串,并返回一个整数.
但是,这种语法与lambdas非常混淆,它看起来像:
func = lambda var1, var2: var1.index(var2)
Run Code Online (Sandbox Code Playgroud)
我已经尝试在参数和返回类型上添加类型提示,我无法找出一种不会导致语法错误的方法.
是否可以输入提示lambda函数?如果没有,是否有计划暗示lambda,或任何原因(除了明显的语法冲突)为什么不呢?
A有一个真正的问题(并且头疼)有一个任务......
我在一个入门编程课程中,我必须编写一个函数,给定一个列表,它将返回它所达到的"最大"深度...例如:[1,2,3]将返回1,[ 1,[2,3]]将返回2 ...
我写了这段代码(这是我能得到的最好的T_T)
def flat(l):
count=0
for item in l:
if isinstance(item,list):
count+= flat(item)
return count+1
Run Code Online (Sandbox Code Playgroud)
然而,它显然没有像它应该的那样工作,因为如果有列表不计入最大深度,它仍然提出反击......
例如:当我使用[1,2,[3,4],5,[6],7]的函数时,它应返回2,但它返回3 ...
任何想法或帮助将非常感谢^^非常感谢!! 我已经持续数星期了......
python ×9
lambda ×2
list ×2
python-3.x ×2
coding-style ×1
foreach ×1
function ×1
indexing ×1
levels ×1
nested ×1
regex ×1
string ×1
terminology ×1
theory ×1