Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

18
多重处理:如何在类中定义的函数上使用Pool.map?
当我运行类似: from multiprocessing import Pool p = Pool(5) def f(x): return x*x p.map(f, [1,2,3]) 它工作正常。但是,将其作为类的函数: class calculate(object): def run(self): def f(x): return x*x p = Pool() return p.map(f, [1,2,3]) cl = calculate() print cl.run() 给我以下错误: Exception in thread Thread-1: Traceback (most recent call last): File "/sw/lib/python2.6/threading.py", line 532, in __bootstrap_inner …

10
减少matplotlib图中的左右边距
我正在努力处理matplotlib中的地块边距。我使用下面的代码来生成图表: plt.imshow(g) c = plt.colorbar() c.set_label("Number of Slabs") plt.savefig("OutputToUse.png") 但是,我得到了一个在绘图的两边都有很多空白的输出图形。我已经搜索过Google并阅读了matplotlib文档,但似乎找不到如何减少这种情况的方法。
178 python  matplotlib 



7
functools部分如何做?
我无法了解部分功能在functools中的工作方式。我从这里有以下代码: >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) 3 >>> def sum2(x, y): return x + y >>> incr2 = functools.partial(sum2, 1) >>> incr2(4) 5 现在排队 incr = lambda y : sum(1, y) …

4
元组比较在Python中如何工作?
我一直在阅读Core Python编程书,作者展示了一个类似的示例: (4, 5) < (3, 5) # Equals false 所以,我想知道为什么/为什么等于假?python如何比较这两个元组? 顺便说一句,这本书没有解释。

12
相对导入-ModuleNotFoundError:没有名为x的模块
这是我第一次真正坐下来尝试python 3,但似乎失败了。我有以下两个文件: test.py config.py config.py中定义了一些函数以及一些变量。我将其简化为以下内容: config.py debug = True test.py import config print (config.debug) 我也有一个 __init__.py 但是,出现以下错误: ModuleNotFoundError: No module named 'config' 我知道py3约定要使用绝对导入: from . import config 但是,这导致以下错误: ImportError: cannot import name 'config' 因此,我对此无所适从……任何帮助将不胜感激。:)


8
Python 3将范围转换为列表
我正在尝试列出其中的数字1-1000。显然,这会令人烦恼,因此我试图创建一个包含范围的列表。在Python 2中,似乎: some_list = range(1,1000) 本来可以用,但是在Python 3中,范围类似于xrangePython 2? 谁能对此提供一些见识?
178 python  python-3.x  list  range 


11
将Unicode转换为ASCII且在Python中没有错误
我的代码只是抓取一个网页,然后将其转换为Unicode。 html = urllib.urlopen(link).read() html.encode("utf8","ignore") self.response.out.write(html) 但是我得到了UnicodeDecodeError: Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 507, in __call__ handler.get(*groups) File "/Users/greg/clounce/main.py", line 55, in get html.encode("utf8","ignore") UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 2818: ordinal not in range(128) 我认为这意味着HTML在某处包含一些错误的Unicode尝试。我可以删除导致问题的任何代码字节而不出错吗?

1
让JSON对象接受字节或让urlopen输出字符串
使用Python 3,我需要从URL请求json文档。 response = urllib.request.urlopen(request) 该response对象是带有read和readline方法的类似文件的对象。通常,可以使用在文本模式下打开的文件来创建JSON对象。 obj = json.load(fp) 我想做的是: obj = json.load(response) 但是,此方法不起作用,因为urlopen以二进制模式返回文件对象。 解决方法当然是: str_response = response.read().decode('utf-8') obj = json.loads(str_response) 但这感觉不好... 有没有更好的方法可以将字节文件对象转换为字符串文件对象?还是我缺少任何一个参数urlopen或json.load给出编码?

1
Python中的异步方法调用?
我想知道Python中是否有任何用于异步方法调用的库。如果您可以做这样的事情会很棒 @async def longComputation(): <code> token = longComputation() token.registerCallback(callback_function) # alternative, polling while not token.finished(): doSomethingElse() if token.finished(): result = token.result() 或异步调用非异步例程 def longComputation() <code> token = asynccall(longComputation()) 在语言核心中拥有更完善的策略,这将是很棒的。是否考虑过?


12
为什么Python的原始字符串文字不能以单个反斜杠结尾?
从技术上讲,文档中描述了任意数量的反斜杠。 >>> r'\' File "<stdin>", line 1 r'\' ^ SyntaxError: EOL while scanning string literal >>> r'\\' '\\\\' >>> r'\\\' File "<stdin>", line 1 r'\\\' ^ SyntaxError: EOL while scanning string literal 似乎解析器可以将原始字符串中的反斜杠视为常规字符(这不是原始字符串的全部含义吗?),但是我可能缺少明显的东西。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.