假设我想计算以下内容f(f(...f(x)..)
.
基本上很多次都是自身的功能.
目前我正在做以下事情来实现这个结果(并返回所有中间步骤):
def iterator(function, x, n=4):
x = float(x)
arr = []
for i in range(n + 1):
arr.append(x)
x = function(x)
return arr
def funcM(x):
return x / 4 + 12
Run Code Online (Sandbox Code Playgroud)
然后我将我的函数funcM
作为参数传递:
print iterator(funcM, 100, 5)
.
这种方法没有错,计算是正确的.
但有没有办法在没有定义功能的情况下做同样的事情funcM
?
可能是将lambda函数作为参数传递给迭代器函数(对不起,如果它没有意义,我真的不知道lambda函数是什么).
我已经读过使用以下格式打开文件时
with open(filename) as f:
#My Code
f.close()
Run Code Online (Sandbox Code Playgroud)
不需要显式关闭文件.有人可以解释为什么会这样吗?此外,如果有人明确关闭文件,它会有任何不良影响吗?
只是考虑Python的dict
"功能",并开始意识到这dict
根本不是一个功能.例如,如果我们这样做dir(dict)
,我们会得到各种方法,这些方法不包含在用户定义函数的通常命名空间中.扩展这个想法,它dir(list)
和dir(len)
.他们不是功能,但真的type
是.但后来我对文档页面http://docs.python.org/2/library/functions.html感到困惑,它清楚地说明了函数.(我想它应该真的只是说内置的callables)
什么给出了什么?(开始看起来对类和函数的区分是微不足道的)
我想查找列表元素中是否包含特定字符串.如果找到该字符串,我想打印出"String found",否则"找不到字符串".但是,我提出的代码,使得"找不到字符串"的多个打印.我知道原因,但我不知道如何修复它并只打印一次消息.
animals=["dog.mouse.cow","horse.tiger.monkey",
"badger.lion.chimp","trok.cat. bee"]
for i in animals :
if "cat" in i:
print("String found")
else:
print("String not found")
Run Code Online (Sandbox Code Playgroud)
〜
我需要找到其值在有序字典中最低的键,但只有当my_list中的位置为True时才需要.
from collections import OrderedDict
my_list = [True,False,False,True,True,False,False,False,]
my_lookup = OrderedDict([('a', 2), ('b', 9), ('c', 4), ('d', 7),
('e', 3), ('f', 0), ('g', -5), ('h', 9)])
Run Code Online (Sandbox Code Playgroud)
我知道怎么用for循环这样做
mins=[]
i=0
for item in my_lookup.items():
if my_list[i]:
mins.append(item)
i+=1
print min(mins,key=lambda x:x[1])[0]
Run Code Online (Sandbox Code Playgroud)
版画
a
Run Code Online (Sandbox Code Playgroud)
因为a在my_list中最低也是True.
这有效,但很长,我想知道如何用理解或一行做到这一点?
在Python中进行用户输入验证的最"正确"Pythonic方法是什么?
我一直在使用以下内容:
while True:
stuff = input("Please enter foo: ")
try:
some_test(stuff)
print("Thanks.")
break
except SomeException:
print("Invalid input.")
Run Code Online (Sandbox Code Playgroud)
我想,这是好的和可读的,但我不禁想知道是否有一些内置函数或我应该使用的东西.
为了解决这个问题,我试图遍历 python 中的坐标对列表,并删除其中一个坐标为负的所有情况。例如:
在数组中:
map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
Run Code Online (Sandbox Code Playgroud)
我想删除其中任一坐标 < 0 的所有对,留下:
map = [[2, 3], [7, 1]]
Run Code Online (Sandbox Code Playgroud)
我的问题是 python 列表不能有任何间隙,所以如果我这样循环:
i = 0
for pair in map:
for coord in pair:
if coord < 0:
del map[i]
i += 1
Run Code Online (Sandbox Code Playgroud)
当元素被删除时,所有的索引都会发生变化,扰乱迭代并导致各种问题。我尝试将坏元素的索引存储在另一个列表中,然后循环遍历并删除这些元素,但我遇到了同样的问题:一旦一个元素消失,整个列表就会发生变化,索引不再准确。
有什么我想念的吗?
谢谢。
什么<function at 'somewhere'>
意思?例:
>>> def main():
... pass
...
>>> main
<function main at 0x7f95cf42f320>
Run Code Online (Sandbox Code Playgroud)
也许有办法以某种方式使用它0x7f95cf42f320
?
据我所知,在Python中,常规的c ++样式变量赋值被替换为对东西的引用即
a=[1,2,3]
b=a
a.append(4)
print(b) #gives [1,2,3,4]
print(a) #gives [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
但我仍然困惑为什么与基本类型的类似情况,例如.整数的工作方式不同?
a=1
b=a
a+=1
print(b) # gives 1
print(a) # gives 2
Run Code Online (Sandbox Code Playgroud)
但等等,当我们考虑循环时,它会变得更加混乱!
li=[1,2,3]
for x in li:
x+=1
print(li) #gives [1,2,3]
Run Code Online (Sandbox Code Playgroud)
这是我的预期,但如果我们这样做会发生什么:
a,b,c=1,2,3
li=[a,b,c]
for x in li:
x+=1
print(li) #gives [1,2,3]
Run Code Online (Sandbox Code Playgroud)
也许我的问题应该是如何遍历整数列表并在没有map()的情况下更改它们,因为我需要一个if语句.我唯一能做的就是没用
for x in range(len(li)):
Do stuff to li[x]
Run Code Online (Sandbox Code Playgroud)
将整数打包在一个元素列表中.但必须有更好的方法.
用XML等
<a>
<b>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
我需要添加类似的兄弟姐妹
<a>
<b>
</b>
<b'>
</b'>
</a>
Run Code Online (Sandbox Code Playgroud)
ElementTree是否具有添加兄弟节点的功能?如果没有,我想我需要一个函数来获取父节点并添加一个子节点,我该怎么办呢?
我有一个关于python的问题.
我有变量a
,b
,c
和d
.
我有以下几行:
if not isinstance(a, int) or not isinstance(b, int) \
or not isinstance(c, int) or not isinstance(d, int) \
or not isinstance(a, float) or not isinstance(b, float)\
or not isinstance(c, float) or not isinstance(d, float):
do something
Run Code Online (Sandbox Code Playgroud)
是否可以缩短此代码?
谢谢!
我想知道';'的含义是什么?签到说明
if l<p:
if t>0:
if int(num)>=int(lst):
Run Code Online (Sandbox Code Playgroud)
在这个问题的代码中: 优化python代码
可能是第三条指令中的错误,'; ='而不是'!='
但我无法想象其他两行中人物的意义.
请问有人可以解释一下吗?
python ×12
assert ×1
del ×1
dictionary ×1
elementtree ×1
file ×1
for-loop ×1
function ×1
indices ×1
isinstance ×1
lambda ×1
list ×1
loops ×1
repr ×1
user-input ×1
validation ×1
variables ×1
xml ×1