Questions tagged «python-3.x»

对于特定于该语言版本3+的有关Python编程的问题。如果您的问题不是特定于版本的,则使用更通用的[python]标记。将[python-2.x]标记用于Python 2问题。

1
用户定义类的类型提示
似乎找不到确切的答案。我想为一个函数提供类型提示,该类型是我定义的一些自定义类,称为它CustomClass()。 然后让我们说在某个函数中调用它FuncA(arg),我有一个名为的参数arg。键入提示的正确方法FuncA是: def FuncA(arg: CustomClass): 或者是: def FuncA(Arg:Type[CustomClass]):?

9
如何使IPython Notebook运行Python 3?
我是Python的新手。 我安装了Anaconda,效果很好。 我按照Anaconda cmd行说明设置了Python 3环境,效果很好。 我将Anaconda的Python 3环境设置为Pycharm的解释器,效果很好。 我启动了Anaconda“ launcher.app”,并启动了IPython Notebook。但是,iPython Notebook正在运行Python 2而不是3。 经过三个多小时的Google搜索,我无法弄清楚如何将IPython Notebook设置为运行Python 3而不是2。

11
Windows 10无法识别Conda命令
我按照以下说明在Windows 10上安装了Anaconda 4.4.0(Python 3.6版本):https : //www.continuum.io/downloads。但是,当我打开命令提示符窗口并尝试编写时 conda list 我得到了 无法识别'conda'命令... 错误。 我试着跑 set PATH=%PATH%;C:\Users\Alex\Anaconda3 但这没有帮助。我还读到我可能需要编辑.bashrc文件,但是我不知道如何访问该文件以及如何编辑它。



4
什么时候在Python中hash(n)== n?
我一直在玩Python的hash函数。对于小整数,它hash(n) == n总是出现。但是,这不会扩展为大量: >>> hash(2**100) == 2**100 False 我并不感到惊讶,我知道哈希值取值范围有限。这个范围是多少? 我尝试使用二进制搜索来找到最小的数字hash(n) != n >>> import codejamhelpers # pip install codejamhelpers >>> help(codejamhelpers.binary_search) Help on function binary_search in module codejamhelpers.binary_search: binary_search(f, t) Given an increasing function :math:`f`, find the greatest non-negative integer :math:`n` such that :math:`f(n) \le t`. If :math:`f(n) > t` …

3
需要为Python 3.5.1安装urllib2
我正在为Mac运行Python 3.5.1。我想使用urllib2模块。我尝试安装它,但被告知它已被拆分成Python 3 urllib.request并urllib.error用于Python 3。 我的命令(现在不在框架bin目录中运行,因为它不在我的路径中): sudo ./pip3 install urllib.request 返回此: Could not find a version that satisfies the requirement urllib.request (from versions: ) No matching distribution found for urllib.request 在尝试一口气安装之前,我遇到了同样的错误urllib2。


3
ctypes-初学者
我的任务是将ac库“包装”到python类中。在这个问题上,文档令人难以置信。看来他们希望只有高级python用户才能实现ctypes。好吧,我是python的初学者,需要帮助。 一些逐步的帮助将是很棒的。 所以我有我的C库。我该怎么办?我将哪些文件放在哪里?如何导入库?我读到可能有一种方法可以“自动包装”到Python? (通过我在python.net上进行ctypes教程的方式,它不起作用。这意味着我认为他们认为我应该能够完成其余步骤。 实际上,这是我使用其代码得到的错误: File "importtest.py", line 1 >>> from ctypes import * SyntaxError: invalid syntax 我真的可以为此使用一些逐步的帮助!谢谢〜


9
如何在虚拟环境中运行Spyder?
我一直在使用随Anaconda发行版安装的Spyder,后者使用Python 2.7作为默认值。当前,我需要使用Python 3.4设置开发虚拟环境。 经过网上研究后,最重要的两个建议是: 首先建立虚拟环境并指出改变Spyder的偏好,例如在这里 ; 在虚拟环境本身中安装所有Spyder依赖项,例如PyQt4,例如在这里; 两项建议都很繁琐,看起来也不是明智的开发选择。 有没有一种解决方案可以在激活所需的虚拟环境后自动使用所需的Python版本运行Spyder?

10
如何推迟/推迟对f弦的评估?
我正在使用模板字符串生成一些文件,为此我喜欢新的f字符串的简洁性,以减少类似以下内容的我以前的模板代码: template_a = "The current name is {name}" names = ["foo", "bar"] for name in names: print (template_a.format(**locals())) 现在,我可以直接替换变量: names = ["foo", "bar"] for name in names: print (f"The current name is {name}") 但是,有时在其他地方定义模板是有意义的-在代码中较高的位置,或者从文件或其他内容中导入模板。这意味着模板是带有格式标记的静态字符串。字符串上必须发生一些事情,以告诉解释器将字符串解释为新的f字符串,但是我不知道是否有这种事情。 有什么方法可以引入字符串并将其解释为f字符串,从而避免使用.format(**locals())调用? 理想情况下,我希望能够像这样进行编码...(magic_fstring_function我不理解的部分在哪里出现): template_a = f"The current name is {name}" # OR [Ideal2] template_a = magic_fstring_function(open('template.txt').read()) names …

6
如何在Python 3中使用自定义比较功能?
在Python 2.x中,我可以将自定义函数传递给sort和.sort函数 >>> x=['kar','htar','har','ar'] >>> >>> sorted(x) ['ar', 'har', 'htar', 'kar'] >>> >>> sorted(x,cmp=customsort) ['kar', 'htar', 'har', 'ar'] 因为用我的语言,辅音是伴随着这个顺序 "k","kh",....,"ht",..."h",...,"a" 但是在Python 3.x中,看起来我无法传递cmp关键字 >>> sorted(x,cmp=customsort) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'cmp' is an invalid keyword argument for this function 有其他选择吗?或者我也应该编写自己的排序函数吗? 注意:我通过使用“ k”,“ kh”等进行了简化。实际字符是Unicode,甚至更复杂,有时在辅音前后都有元音,所以我完成了自定义比较功能,因此这一部分还可以。唯一的问题是我无法将自定义比较功能传递给sort或.sort

4
为什么在Python 2.7中自愿使用印刷括号?
在Python 2.7中,以下两者将执行相同的操作 print("Hello, World!") # Prints "Hello, World!" print "Hello, World!" # Prints "Hello, World!" 但是以下内容不会 print("Hello,", "World!") # Prints the tuple: ("Hello,", "World!") print "Hello,", "World!" # Prints the words "Hello, World!" 在Python 3.x中,括号print是强制性的,本质上使其成为一个函数,但是在2.7中,两者都可以使用不同的结果。我print在Python 2.7中还应该了解什么?

7
pip抛出TypeError:尝试安装新软件包时parse()得到了意外的关键字参数'transport_encoding'
我正在使用最新版本的Anaconda3。我刚刚安装了它,我正在尝试下载一些软件包。我正在使用Anaconda Prompt。尝试使用pip做任何事情(包括升级现有软件包)时,我得到以下回溯。 Exception: Traceback (most recent call last): File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run wb.build(autobuilding=True) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line 487, in _prepare_file req_to_install, finder) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line …

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.