Questions tagged «python»

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


1
如何连接str和int对象?
如果我尝试执行以下操作: things = 5 print("You have " + things + " things.") 我在Python 3.x中收到以下错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be str, not int ...以及Python 2.x中的类似错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' …

13
如何找到pg_config路径
在这里完成新手操作,尝试将Django设置为可与PostgreSQL一起使用。 我正在使用Mac OS X 10.6.8。我还安装了PostgreSQL 9.3 pip install psycopg2在终端中运行时,出现以下错误 Downloading/unpacking psycopg2 Downloading psycopg2-2.5.2.tar.gz (685kB): 685kB downloaded Running setup.py (path:/private/var/folders/A9/A99cs6x0FNusPejCVkYNTE+++TI/-Tmp-/pip_build_bengorman/psycopg2/setup.py) egg_info for package psycopg2 Error: pg_config executable not found. Please add the directory containing pg_config to the PATH or specify the full executable path with the option: python setup.py build_ext --pg-config …

19
查找两个字符串之间的公共子字符串
我想比较2个字符串并保持匹配,在比较失败的地方分开。 因此,如果我有2个字符串- string1 = apples string2 = appleses answer = apples 另一个示例,因为字符串可能包含多个单词。 string1 = apple pie available string2 = apple pies answer = apple pie 我敢肯定有一种简单的Python方式可以做到这一点,但是我无法解决,感谢您的帮助和解释。

8
如何使用python opencv2在Windows中的图像上写文本
我想在图像上放一些文字。我将代码编写为: cv2.putText(image,"Hello World!!!", (x,y), cv2.CV_FONT_HERSHEY_SIMPLEX, 2, 255) 它给出错误,说“模块”对象没有属性“ CV_FONT_HERSHEY_SIMPLEX” 查询 我不能使用上述字体类型吗?我在Internet上搜索,但只找到了与initFont与Opencv C ++有关的语法。然后我想到了使用putText传递字体类型作为参数。但这对我不起作用。 有什么建议?
75 python  windows  opencv 


14
Python对象删除自身
为什么不起作用?我试图使一个类的实例本身删除。 >>> class A(): def kill(self): del self >>> a = A() >>> a.kill() >>> a <__main__.A instance at 0x01F23170>

15
获取lxml中标签内的所有文本
我想编写一个代码片段<content>,在以下所有三个实例中(包括代码标签),都将在lxml中的标签中捕获所有文本。我已经尝试过了,tostring(getchildren())但是那样会错过标签之间的文本。我没有太多运气在API中搜索相关功能。你能帮我吗? <!--1--> <content> <div>Text inside tag</div> </content> #should return "<div>Text inside tag</div> <!--2--> <content> Text with no tag </content> #should return "Text with no tag" <!--3--> <content> Text outside tag <div>Text inside tag</div> </content> #should return "Text outside tag <div>Text inside tag</div>"
75 python  parsing  lxml 

4
仅显示图例Python Matplotlib中的某些项目
我目前正在绘制大量生物分类数据的堆叠条形图,只希望在图例中显示重要物种(在约500种中,我希望显示约25种)。有没有简单的方法可以做到这一点?下面是我的代码: labels=['0','20','40','60','80','100','120'] ax1=subj1df.plot(kind='barh', stacked=True,legend=True,cmap='Paired', grid=False) legend(ncol=2,loc=2, bbox_to_anchor=(1.05, 1), borderaxespad=0.) label1=['Baseline','8h','24h','48h','96h','120h'] ax1.set_yticklabels(label1, fontdict=None, minor=False) plt.title('Subject 1 Phyla',fontweight='bold') plt.savefig('Subject1Phyla.eps', format='eps', dpi=1000) ax1.set_xticklabels(labels) 编辑:尝试将其添加为仅显示一个图例条目,但是仅返回一个空图例: h, l = ax1.get_legend_handles_labels() legend(l[4],h[4],ncol=2,loc=2, bbox_to_anchor=(1.05, 1), borderaxespad=0.)



3
如何使用importlib.import_module在Python中导入模块
我正在尝试importlib.import_module在Python 2.7.2中使用并遇到奇怪的错误。 考虑以下目录结构: 一种 | +-__init__.py -b | +-__init__.py -c.py a/b/__init__.py 具有以下代码: 导入importlib mod = importlib.import_module(“ c”) (在真实代码中"c"有一个名称。) 尝试到时import a.b,产生以下错误: >>>导入ab 追溯(最近一次通话): 文件“”,第1行,位于 在第3行的文件“ a / b / __ init__.py”中 mod = importlib.import_module(“ c”) import_module中的文件“ /opt/Python-2.7.2/lib/python2.7/importlib/__init__.py”,第37行 __import __(名称) ImportError:没有名为c的模块 我想念什么? 谢谢!

4
对点分隔的数字列表进行排序,例如软件版本
我有一个包含版本字符串的列表,例如: versions_list = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"] 我想对其进行排序,所以结果将是这样的: versions_list = ["1.0.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"] 数字的优先顺序显然应该从左到右,并且应该是降序。所以1.2.3来之前2.2.3和2.2.2来之前2.2.3。 如何在Python中执行此操作?
75 python 

12
如何在Python中从NIC获取IP地址?
在Unix上的Python脚本中发生错误时,将发送一封电子邮件。 如果IP地址是测试服务器192.168.100.37,我被要求在电子邮件的主题行中添加{Testing Environment}。这样,我们就可以拥有一个脚本版本,并可以判断电子邮件是否来自测试服务器上混乱的数据。 但是,当我在Google上搜索时,我会不断找到以下代码: import socket socket.gethostbyname(socket.gethostname()) 但是,这给了我127.0.1.1的IP地址。当我使用ifconfig我得到这个 eth0 Link encap:Ethernet HWaddr 00:1c:c4:2c:c8:3e inet addr:192.168.100.37 Bcast:192.168.100.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:75760697 errors:0 dropped:411180 overruns:0 frame:0 TX packets:23166399 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:59525958247 (59.5 GB) TX bytes:10142130096 (10.1 GB) Interrupt:19 Memory:f0500000-f0520000 lo Link encap:Local …
75 python  unix  networking  nic 

4
使用自定义比较功能对列表列表进行排序
我知道有几个这样的问题,但是它们似乎对我没有用。 我有一个列表,5元素乘以50。我想通过对每个元素应用自定义比较功能来对列表进行排序。此函数计算要对元素进行排序的列表的适用性。我创建了两个函数,比较和适应性: def compare(item1, item2): return (fitness(item1) < fitness(item2)) 和 def fitness(item): return item[0]+item[1]+item[2]+item[3]+item[4] 然后我尝试通过以下方式致电给他们: sorted(mylist, cmp=compare) 要么 sorted(mylist, key=fitness) 要么 sorted(mylist, cmp=compare, key=fitness) 要么 sorted(mylist, cmp=lambda x,y: compare(x,y)) 我也尝试了具有相同参数的list.sort()。但是无论如何,函数都不会将列表作为参数,而是作为参数None。我不知道为什么,这主要来自C ++,这与我对回调函数的任何想法相矛盾。如何使用自定义功能对列表进行排序? 编辑 我发现了我的错误。在创建原始列表的链中,一个函数未返回任何内容,但使用了返回值。抱歉打扰了

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.