Questions tagged «python»

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

5
使用熊猫将字符串前缀添加到字符串列中的每个值
我想在熊猫数据帧的所述列中的每个值的开头附加一个字符串(优雅)。我已经弄清楚该如何做,目前正在使用: df.ix[(df['col'] != False), 'col'] = 'str'+df[(df['col'] != False), 'col'] 这似乎是一件微不足道的事情-您是否知道其他任何方式(可能还会将该字符添加到该列为0或NaN的行中)? 如果还不清楚,我想转一下: col 1 a 2 0 变成: col 1 stra 2 str0

3
如何在python抽象类中创建抽象属性
在以下代码中,我创建了一个基本抽象类Base。我希望所有从其继承的类都Base提供该name属性,因此我将该属性设置为@abstractmethod。 然后,我创建了一个Base名为的子类,该子类Base_1旨在提供一些功能,但仍保持抽象。中没有name属性Base_1,但是python实例化了该类的对象而没有错误。一个人如何创建抽象属性? from abc import ABCMeta, abstractmethod class Base(object): __metaclass__ = ABCMeta def __init__(self, strDirConfig): self.strDirConfig = strDirConfig @abstractmethod def _doStuff(self, signals): pass @property @abstractmethod def name(self): #this property will be supplied by the inheriting classes #individually pass class Base_1(Base): __metaclass__ = ABCMeta # this class does not provide the …

2
打印测试执行时间并使用py.test固定慢速测试
我正在使用py.test在CI服务器上运行单元测试。测试使用通过网络获取的外部资源。有时测试跑步者花费的时间太长,导致测试跑步者被中止。我不能在本地重复这些问题。 有没有一种方法可以使py.test打印出(缓慢)测试的执行时间,因此固定有问题的测试变得更加容易?
118 python  pytest 


4
如何包括外部Python代码以在其他文件中使用?
如果文件中有一组方法,是否可以将这些文件包含在另一个文件中,但不带任何前缀(即文件前缀)调用它们? 所以,如果我有: [Math.py] def Calculate ( num ) 我怎么这样称呼它: [Tool.py] using Math.py for i in range ( 5 ) : Calculate ( i )
117 python 


6
列表理解甚至在理解范围之后也会重新绑定名称。这是正确的吗?
理解与范围界定存在一些意外的相互作用。这是预期的行为吗? 我有一个方法: def leave_room(self, uid): u = self.user_by_id(uid) r = self.rooms[u.rid] other_uids = [ouid for ouid in r.users_by_id.keys() if ouid != u.uid] other_us = [self.user_by_id(uid) for uid in other_uids] r.remove_user(uid) # OOPS! uid has been re-bound by the list comprehension above # Interestingly, it's rebound to the last uid in …



13
从sklearn导入时出现ImportError:无法导入名称check_build
尝试从sklearn导入时出现以下错误: >>> from sklearn import svm Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> from sklearn import svm File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module> from . import check_build ImportError: cannot import name check_build 我正在使用python 2.7,scipy-0.12.0b1 superpack,numpy-1.6.0 superpack,scikit-learn-0.11我有一台Windows 7机器 我已经检查了几个有关此问题的答案,但没有一个给出解决此错误的方法。

10
通过索引访问Python字典的元素
考虑一个像 mydict = { 'Apple': {'American':'16', 'Mexican':10, 'Chinese':5}, 'Grapes':{'Arabian':'25','Indian':'20'} } 例如,如何访问该词典的特定元素?例如,我想在对Apple的第一个元素进行某种格式设置后再打印第一个元素,在我们的例子中,该格式仅是“ American”? 附加信息上面的数据结构是通过在python函数中解析输入文件创建的。一旦创建,它在该运行中将保持不变。 我在函数中使用此数据结构。 因此,如果文件发生更改,则下次运行此应用程序时,文件的内容将有所不同,因此此数据结构的内容将有所不同,但格式将相同。因此,您在我的职能中看到我,我不知道Apple中的第一个元素是“ American”或其他任何元素,因此我不能直接使用“ American”作为键。
117 python  dictionary 

4
来自字符串源的Python xml ElementTree?
ElementTree.parse从文件读取,如果字符串中已经有XML数据,该如何使用? 也许我在这里错过了一些东西,但是必须有一种使用ElementTree的方法,而无需将字符串写出到文件中并再次读取它。 xml.etree.elementtree
117 python  xml 

5
Python:绑定未绑定方法?
在Python中,有没有办法绑定未绑定的方法而不调用它? 我正在编写一个wxPython程序,对于某个类,我决定将所有按钮的数据分组为一个类级别的元组列表是很不错的,如下所示: class MyWidget(wx.Window): buttons = [("OK", OnOK), ("Cancel", OnCancel)] # ... def Setup(self): for text, handler in MyWidget.buttons: # This following line is the problem line. b = wx.Button(parent, label=text).Bind(wx.EVT_BUTTON, handler) 问题是,因为所有的值handler都是未绑定方法,所以我的程序爆炸得很厉害,我哭了。 我正在网上寻找解决方案,该方案应该是一个相对简单,可解决的问题。不幸的是我找不到任何东西。现在,我正在functools.partial解决此问题,但是没有人知道是否存在一种干净,健康,Pythonic的方式将未绑定的方法绑定到实例并继续传递它而不调用它吗?
117 python  class  methods  bind 

6
如何在PyCharm中选择Python版本?
我拥有PyCharm 1.5.4,并使用“打开目录”选项在IDE中打开文件夹的内容。 我选择了Python 3.2版(它显示在“外部库”节点下)。 如何选择其他版本的Python(已经安装在计算机上),以便PyCharm改用该版本?

4
无法使用Ctrl-C终止Python脚本
我正在使用以下脚本测试Python线程: import threading class FirstThread (threading.Thread): def run (self): while True: print 'first' class SecondThread (threading.Thread): def run (self): while True: print 'second' FirstThread().start() SecondThread().start() 它在Kubuntu 11.10上的Python 2.7中运行。Ctrl+ C不会杀死它。我还尝试为系统信号添加处理程序,但这没有帮助: import signal import sys def signal_handler(signal, frame): sys.exit(0) signal.signal(signal.SIGINT, signal_handler) 为了终止进程,我使用Ctrl+ 将程序发送到后台后通过PID将其终止Z,这不会被忽略。为什么Ctrl+ C被如此持久地忽略?我该如何解决?
117 python  linux 

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.