过滤掉不遵守某个谓词的数据结构中的所有元素的最佳方法是什么?即一种类似于函数式编程语言中的"过滤"方法的方法.
我有一个纯Python的项目和一个基本的插件系统:你编写一个模块来定义一个具有特定接口和名称的类,程序导入模块,然后根据需要实例化该类.
目前,插件都来自特定文件夹(主.py文件所在的子目录).我希望能够将它们放在磁盘上的其他位置,并指示程序在特定位置查找插件.对于一次性动态导入,我能以比修改更简洁的方式执行此操作sys.path吗?我不想污染这种全球性的.
相关:我可以依靠sys.path[0]成为脚本的路径,即使它与当前工作目录(os.getcwd())不同吗?
编辑:我忘了提 - 我希望能够从几个不同的文件夹中获取插件,用户指定插件文件夹的路径.目前,这些文件夹中的每一个都被设置为一个包(带有__init__.py); 如果它导致问题,我可以简单地废弃它.
我做了两个名为House and Window的课程.然后,我列出了包含四个房屋的清单.House的每个实例都有一个Windows列表.我正在尝试遍历每个房子的窗户并打印它的ID.但是,我似乎得到了一些奇怪的结果:S我非常感谢任何帮助.
#!/usr/bin/env python
# Minimal house class
class House:
ID = ""
window_list = []
# Minimal window class
class Window:
ID = ""
# List of houses
house_list = []
# Number of windows to build into each of the four houses
windows_per_house = [1, 3, 2, 1]
# Build the houses
for new_house in range(0, len(windows_per_house)):
# Append the new house to the house list
house_list.append(House())
# Give the new house an ID
house_list[new_house].ID = str(new_house) …Run Code Online (Sandbox Code Playgroud) 我试图继承xrange(使用in以及迭代来实现成员资格测试)。但我收到以下错误消息:
TypeError: Error when calling the metaclass bases
type 'xrange' is not an acceptable base type
Run Code Online (Sandbox Code Playgroud)
有什么特别之处xrange?
我将如何执行以下操作:
>>> num_decimal_places('3.2220')
3 # exclude zero-padding
>>> num_decimal_places('3.1')
1
>>> num_decimal_places('4')
0
Run Code Online (Sandbox Code Playgroud)
我正在考虑这样做:
len((str(number) if '.' in str(number) else str(number) + '.').rstrip('0').split('.')[-1])
Run Code Online (Sandbox Code Playgroud)
还有另一种更简单的方法吗?
我有一个 for 循环,它会从一组 URL 中获取一个 URL 并访问该 URL 并执行其他一些操作,但它花费了很长时间,所以我想我可以通过一些多重处理来加速它,但我很挣扎这样做。
感谢您的帮助。
def accessAndSaveFiles(urlSet, user, verboseFlag):
with multiprocessing.Pool(os.cpu_count()) as pool:
pool.starmap(processURL, zip(itertools.repeat(urlSet), user, verboseFlag))
def processURL(url, user, verboseFlag):
filePath = some_path
img_data = requests.get(url, allow_redirects=True)
open(filePath, 'wb').write(img_data.content)
def main():
...
accessAndSaveFiles(urlSet, user, verboseFlag)
...
Run Code Online (Sandbox Code Playgroud)
我在“pool.starmap(processURL, zip(itertools.repeat(urlSet), user, verboseFlag))”行上收到错误,提示“TypeError: zip argument #3 must support iteration”。我不想迭代这个项目,我只想每次发送相同的值。
我想要做的是在下面的代码中打印整数0到5,但我得到的只是迭代器的地址?
def main():
l = []
for i in range(0,5):
l.append(i)
it = iter(l)
for i in range(0,5):
print it
it.next()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud) 如果我在文件中有这段代码:
import inspect
def sample(p1):
print(p1)
return 1
print(inspect.getsource(sample))
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,它按预期工作:在最后一行,sample打印该函数的源代码。
但是,如果我使用相同的代码创建一个字符串,并将其提供给exec,如下所示:
source = """import inspect
def sample(p1):
print(p1)
return 1
print(inspect.getsource(sample))
"""
exec(source)
Run Code Online (Sandbox Code Playgroud)
失败并显示消息OSError: could not get source code。
如何在使用运行代码时使其工作exec- 调用动态执行的代码字符串的inspect.getsource一部分?
所以我正在尝试编写一个 Python 3 函数来接受一个字符串,删除元音并在没有元音的情况下返回它。我写了下面的代码,但它似乎只去掉了部分元音,而保留了一些未受影响。
def remove_vowels(string):
vowels = ['a','e','i','o','u']
newstring = ""
for letter in string:
if letter in vowels:
newstring = string.replace(letter,””)
else:
pass
return newstring
Run Code Online (Sandbox Code Playgroud) all()在序列中找到 False 后是否立即返回 False?
尝试运行这段代码:
def return_true():
print('I have just been printed')
return True
print(all((False, return_true())))
Run Code Online (Sandbox Code Playgroud)
如您所见,I have just been printed即使前面有 False,也会打印出来。
另一个例子:
def return_false():
print('I have just been printed')
return False
print(any((True, return_false())))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,I have just been printed即使之前有 True,也会在此代码中打印。
python ×9
python-3.x ×2
c++ ×1
inheritance ×1
iterator ×1
list ×1
printing ×1
typeerror ×1
xrange ×1