小编alw*_*btc的帖子

Python:如何使用getattr获取对象属性的属性?

我该如何评价

a = myobject.id.number
Run Code Online (Sandbox Code Playgroud)

如果myobject是,则返回None None

内置getattr?也许getattr(myobject, "id.number", None)

python attributes object

5
推荐指数
2
解决办法
1838
查看次数

多处理可加快python中的执行时间

我有4个python列表,其中包含2个项目的内部列表:

a = [[1,2], [3,4], ...]
b = [[5,2], [2,4], ...]
c = [[7,2], [5,4], ...]
d = [[8,2], [4,4], ...]
Run Code Online (Sandbox Code Playgroud)

我可以像这样连续地对内部列表求和:

for list in [a,b,c,d]:
    total = 0
    for [x,y] in list:
        total += x + y
    print("total is: ", total)
Run Code Online (Sandbox Code Playgroud)

假设每个列表的每个求和操作需要5秒钟,因此连续累加4个列表将需要20秒。

如果使用multiprocessing,是否可以同时对4个列表求和,而所有4个求和操作将仅需5秒而不是20秒?

python multiprocessing

5
推荐指数
1
解决办法
59
查看次数

如何将字符串拆分为python中不包含空格的单词?

我的字符串是:

"This     is a      string"
Run Code Online (Sandbox Code Playgroud)

我想把它变成一个列表:

["This", "is", "a", "string"]
Run Code Online (Sandbox Code Playgroud)

我使用该split(" ")方法,但它添加了空格作为列表元素.请帮忙,

最好的祝福

python string whitespace list

4
推荐指数
1
解决办法
1242
查看次数

如何在Python中使用open函数创建文件?

在Linux环境中,我想创建一个文件并在其中写入文本:

HTMLFILE: "$MYUSER/OUTPUT/myfolder/mytext.html"
f = open(HTMLFILE, 'w')

IOError: [Errno 2] No such file or directory: "$MYUSER/OUTPUT/myfolder/mytext.html"
Run Code Online (Sandbox Code Playgroud)

我有读/写权限做"$ MYUSER/OUTPUT/myfolder /"目录.

为什么我会收到此错误?为什么不创建mytext.html文件?

python linux file

4
推荐指数
2
解决办法
1654
查看次数

如何在python中获取列表列表的统计信息?

我有一份清单清单:

[[1,2], [1,2,4], [1,2,3,4], [4,5,6], [1,9], [1,2,4]]
Run Code Online (Sandbox Code Playgroud)

我想以下列格式获取列表统计信息:

number of lists with 2 elements : 2
number of lists with 3 elements : 3
number of lists with 4 elements : 1
Run Code Online (Sandbox Code Playgroud)

这样做的最好(最pythonic)方式是什么?

python statistics list

4
推荐指数
3
解决办法
1210
查看次数

为什么函数在Python中以“返回0”而不是“返回”结尾?

您能否解释一下“返回0”和“返回”之间的区别?例如:

do_1():
    for i in xrange(5):
        do_sth()
    return 0

do_2():
    for i in xrange(5):
        do_sth()
    return 
Run Code Online (Sandbox Code Playgroud)

上面两个功能有什么区别?

python return function

4
推荐指数
2
解决办法
2万
查看次数

如何在 wx.grid (wxpython) 中将整个行/列设置为只读?

以下代码将单元格设置为只读,但如何在 wx.grid 中将整个行/列(例如第 3 列)设置为只读?

import wx.grid as gridlib

myGrid = gridlib.Grid(panel)
myGrid.SetReadOnly(3, 3, True)
Run Code Online (Sandbox Code Playgroud)

python wxpython row readonly

4
推荐指数
1
解决办法
5274
查看次数

如何设置wxpython Grid顶部标题?

当我用wxpython创建一个网格时,我得到顶部列标题为"A","B","C"......

import wx
import wx.grid as gridlib

########################################################################
class MyForm(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="A Simple Grid")
        panel = wx.Panel(self)

        myGrid = gridlib.Grid(panel)
        myGrid.CreateGrid(12, 8)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(myGrid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是我想设置自定义列标题:

在此输入图像描述

这该怎么做?

python grid wxpython header

4
推荐指数
1
解决办法
4608
查看次数

在wx.grid wxpython中使用鼠标悬停在单元格上时的工具提示消息

我有一个wx.grid表,当我将鼠标悬停在一个单元格上时,我想设置一个工具提示,我在下面尝试了Mike Driscoll的推荐,它可以工作,但是我不能用鼠标拖动选择多个单元格,它允许我只选择最多1个单元格,请帮助:

self.grid_area.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)

    def onMouseOver(self, event):
        '''
        Method to calculate where the mouse is pointing and
        then set the tooltip dynamically.
        '''

        # Use CalcUnscrolledPosition() to get the mouse position
        # within the
        # entire grid including what's offscreen
        x, y = self.grid_area.CalcUnscrolledPosition(event.GetX(),event.GetY())

        coords = self.grid_area.XYToCell(x, y)
        # you only need these if you need the value in the cell
        row = coords[0]
        col = coords[1]
        if self.grid_area.GetCellValue(row, col):
            if self.grid_area.GetCellValue(row, col) == "ABC":
                event.GetEventObject().SetToolTipString("Code is abc")
            elif self.grid_area.GetCellValue(row, …
Run Code Online (Sandbox Code Playgroud)

grid wxpython tooltip mousehover

4
推荐指数
1
解决办法
2176
查看次数

如何在python中创建使用**(双星)语法的对象时传递额外的参数?

page_nameNotebookPage下面创建对象时如何传递额外的参数" " ?我收到以下错误:

class NotebookPage(wx.Panel):

    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)


NotebookPage(self, name='NotebookPage0', page_name=page)


TypeError: 'page_name' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)

我需要page_name在创建NotebookPage对象时传递参数.但由于page_name不是一个有效的论据wx.Panel,我得到了这个错误.

python arguments function object

4
推荐指数
1
解决办法
279
查看次数