『疑』python's Unhashable type and comprehensive list - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
/div>
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Hualin
V2EX    Python

『疑』python's Unhashable type and comprehensive list

  •  
  •   Hualin 2012-09-02 23:40:07 +08:00 3002 次点击
    这是一个创建于 4860 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家好,最近在一小程序,思路是的:

    有一堆,每包括其坐和好性字段,一下代有方式,原我不在站代:

    方案一
    == python code ==
    ins = 1000 # 例的
    dim = 100 # 坐的

    import numpy as np

    class attributes: # 的性容器
    def __init__(self, ins):
    self.TRE = np.empty(shape(ins, 1), dtype = str) # 真序列
    self.PRD = np.empty(shape(ins, 1), dtype = str) # 估序列
    self.ERR = np.empty(shape(ins, 1), dtype = bool) # 估序列
    self.DSC = np.empty(shape(ins, 1), dtype = float) # 估值序列

    class space: # 空容器
    def __ init__(self, ins, dim):
    self.mtx = np.array(shape = (ins, dim), dtype = float) # 坐矩
    self.atb = attributes(ins) # 性容器

    == python code ==
    :性字段是的成量,不好查,如果要展,必承,而往後的代如果要用字段也要承原有的,然後字段操作的代。


    方案二

    == python code ==

    class dot: #
    def __init__(self):
    # 在此定你的格式
    self.cdt = (0,0) # 二坐
    self.atb = {TRE: false, 'PRD': False, 'ERR': False} # 性

    class space: # 空容器
    def __init__(self, ins):
    self.dataset = [dot() for each in range(ins)]

    == python code ==
    :定,而不是一序列行定。字典的成量方便了今後代的查和展,也不用新方法,可以查字段作。

    但代效率不高,因:
    方案一是定一序列,用的是 numpy 的 ndarray。後的操作基本也是一集,即於一序列的子集,了方便用 numpy 固有的操作 feature。

    方案二更多的是用 python 自身的,而非 numpy。但不可兼得,因 字典法添加一序列子,因 ndarray 是 unhashable type,故只能方案二也就只能定。

    後我注意到可以用 comprehensive list 表式。例如,如果我後集操作,其用一循把要操作的遍一,是可以用 numpy 的 feature,但要建一 numpy.ndarray 的例:
    arr = np.ndarray([each.cdt for each in self.dateset])

    代是美了,但我不知道的效率如何,有有方案一那直接定一序列然後直接其行操作快?

    所以教大家方案我哪,是另有方式?

    的有多,最近一 python 的疑惑。Thank you for reading。
    5 条回复    1970-01-01 08:00:00 +08:00
    Hualin
        1
    Hualin  
    OP
       2012-09-02 23:51:01 +08:00
    直接了,Comprehensive list 表式的效率怎?跟直接循比?估是一的吧。
    ewangke
        2
    ewangke  
       2012-09-03 01:36:09 +08:00
    @Hualin 建议google一下generator

    http://lmgtfy.com/q=python+generator
    ewangke
        3
    ewangke  
       2012-09-03 01:37:26 +08:00
    Hualin
        4
    Hualin  
    OP
       2012-09-03 02:00:53 +08:00
    @ewangke comprehensive list 感用了。
    比如:
    arr = np.ndarray([d.cdt for d in lstdot if d.atb['keyword'] == 'example'])
    ewangke
        5
    ewangke  
       2012-09-03 03:05:04 +08:00
    @Hualin generator用的是yield,理论上绝不会比list comprehension慢,尤其在处理I/O的时候。

    建议先写最直观的代码实现逻辑,再profile一下,看看空间有没有性能问题,问题在哪里。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2950 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 13:47 PVG 21:47 LAX 05:47 JFK 08:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86