我是Python和Linux的新手,我需要帮助,我尝试使用matplotlib以下列方式显示一个简单的图:
from matplotlib import pyplot as plt
plt.plot([5,6,7,8], [7,3,8,3])
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时python3 test.py,我得到以下输出:
/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3agg.py:18: UserWarning: The Gtk3Agg backend is known to not work on Python 3.x with pycairo. Try installing cairocffi.
"The Gtk3Agg backend is known to not work on Python 3.x with pycairo. "
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 69, in on_draw_event
buf, cairo.FORMAT_ARGB32, width, height)
NotImplementedError: Surface.create_for_data: Not Implemented yet.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 69, in on_draw_event
buf, cairo.FORMAT_ARGB32, width, …Run Code Online (Sandbox Code Playgroud) 我最近逐步完成了CPython源代码,特别是在编译期间查看类的符号表条目.
我碰到了typedef struct _symtable_entry结构的以下条目:
[-- other entries --]
unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
closure over __class__
should be created */
[-- other entries --]
Run Code Online (Sandbox Code Playgroud)
我似乎无法理解它,也无法找到实际设置的python代码示例ste_needs_class_closure == 1.在其他失败的尝试中,我尝试了以下方法:
class foo:
y = 30
def __init__(self):
self.x = 50
def foobar(self):
def barfoo():
print(self.x)
print(y)
return barfoo
Run Code Online (Sandbox Code Playgroud)
但即使它执行,执行ste_needs_class_closure期间的价值0也不1是我希望的那样.
实际上改变这个值的功能对它drop_class_free没有多大帮助.不幸的是,它也没有任何赞美它的评论.
它实际上用于analyze_block评论:
/* Check if any local variables must be converted to …Run Code Online (Sandbox Code Playgroud) 这里针对1.3.4版本的sphinx-doc拿破仑Google风格文档字符串示例显示函数/方法的可选参数应记录如下:
param2 (str, optional): The second parameter. Defaults to None.
Second line of description should be indented.
Run Code Online (Sandbox Code Playgroud)
但由于版本1.4a0同一页面这里显示了以下的方式做同样的事情:
param2 (Optional[str]): The second parameter. Defaults to None.
Second line of description should be indented.
Run Code Online (Sandbox Code Playgroud)
但是我没有在文档中看到对这种新语法的任何解释.在哪里可以找到有关使用sphinx-doc的拿破仑扩展的Google风格文档字符串支持的新语法的更多信息?
使用Python 3词典时,我不得不做这样的事情:
d=dict()
if 'k' in d:
d['k']+=1
else:
d['k']=0
Run Code Online (Sandbox Code Playgroud)
我似乎记得有一种本地方式来做这件事,但是正在查看文档并找不到它.你知道这是什么吗?
我跟进了这个教程:https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html,当我做50 000个请求时,一切正常.但我需要进行1百万个API调用,然后我对此代码有问题:
url = "http://some_url.com/?id={}"
tasks = set()
sem = asyncio.Semaphore(MAX_SIM_CONNS)
for i in range(1, LAST_ID + 1):
task = asyncio.ensure_future(bound_fetch(sem, url.format(i)))
tasks.add(task)
responses = asyncio.gather(*tasks)
return await responses
Run Code Online (Sandbox Code Playgroud)
因为Python需要创建100万个任务,它基本上只是滞后然后Killed在终端中打印消息.是否有任何方法可以使用预先制作的(或列表)网址的发生器?谢谢.
鉴于以下代码:
a = '1'
if a == 1:
print 'yes'
else:
print 'no'
Run Code Online (Sandbox Code Playgroud)
我们得到输出为no。
Python 如何将字符串值与这里的 int 值进行比较 ( if a == 1)?在 C 中,这样的比较会产生错误,因为这是在比较不同的类型。
来自Python书籍:
学习Python。第五版,第 #727 页
我读了以下内容:
如果Python在搜索路径上只找到一个字节码文件并且没有找到源代码,那么它只是直接加载字节码;这意味着您可以将程序仅作为字节代码文件发送,并避免发送源代码
但是当在 Python 3.5 上尝试相同的操作时,它不起作用:
~/Python/Module_Test$ cat a.py
a = "abc"
l = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
导入模块'a'创建的字节码文件如下:
~/Python/Module_Test/__pycache__$ ls
a.cpython-35.pyc
Run Code Online (Sandbox Code Playgroud)
现在我删除了该'a.py'文件并从字节码目录中导入了该模块'a':
~/Python/Module_Test/__pycache__$ python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'a'
Run Code Online (Sandbox Code Playgroud)
我什至尝试将字节码目录添加到搜索路径,但仍然无法加载模块:
>>> …Run Code Online (Sandbox Code Playgroud) 有人可以确定这些符号bytes是什么吗?乍一看,我倾向于认为"十六进制",但我不认识像什么事情xf1Y和e1fl有:
b'vy\xe9\xb5\xa2\xba\xf1Y\xe8\xe1fl\x1d\x87\xacC'
Run Code Online (Sandbox Code Playgroud)
当我用东西编码时,我得到了这个some_text.encode('utf-8').
我试图获得我可以传递给使用Python 2的字节字符串的加密方法的字节.
如何将a转换list为嵌套的`词典'?
例如:
l = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我想将它转换为如下所示的字典:
{1: {2: {3: {4: {}}}}}
Run Code Online (Sandbox Code Playgroud) 我有一个项目与属性的列表"Type"和"Time"我想快速总结的时间为每个"Type"并追加到另一个列表.该列表如下所示:
Items = [{'Name': A, 'Type': 'Run', 'Time': 5},
{'Name': B, 'Type': 'Walk', 'Time': 15},
{'Name': C, 'Type': 'Drive', 'Time': 2},
{'Name': D, 'Type': 'Walk', 'Time': 17},
{'Name': E, 'Type': 'Run', 'Time': 5}]
Run Code Online (Sandbox Code Playgroud)
我想做一些像这样工作的事情:
Travel_Times=[("Time_Running","Time_Walking","Time_Driving")]
Run=0
Walk=0
Drive=0
for I in Items:
if I['Type'] == 'Run':
Run=Run+I['Time']
elif I['Type'] == 'Walk':
Walk=Walk+I['Time']
elif I['Type'] == 'Drive':
Drive=Drive+I['Time']
Travel_Times.append((Run,Walk,Drive))
Run Code Online (Sandbox Code Playgroud)
随着Travel_Times最后看起来像这样:
print(Travel_Times)
[("Time_Running","Time_Walking","Time_Driving")
(10,32,2)]
Run Code Online (Sandbox Code Playgroud)
对于列表理解或类似的东西来说,这似乎很容易有效collections.Counter,但我无法弄清楚.我想到的最好的方法是为每个"类型"使用单独的列表推导,但这需要重复遍历列表.我很感激有关如何加快它的任何想法.
谢谢
python ×10
python-3.x ×8
dictionary ×2
python-3.5 ×2
aiohttp ×1
byte ×1
closures ×1
equality ×1
iteration ×1
list ×1
matplotlib ×1
plot ×1
ubuntu ×1