推荐学习书目
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
sculley
V2EX  ›  Python

求帮忙改写一个循环

  •  
  •   sculley · Jan 16, 2015 · 3650 views
    This topic created in 4160 days ago, the information mentioned may be changed or developed.
    
    以上是一段统计代码的一部分,data_list 是待统计数据, func_list 是统计用到的一些函数,功能相似不相同,循环深度由func_list而定,其他需要的信息再回复吧。

    n-1是下标,不是真的-1哈~
    Supplement 1  ·  Jan 16, 2015
    n-1的问题还是换个图吧
    
    Supplement 2  ·  Jan 16, 2015
    添加了个完整些的demo,功能就是把列表中的元素根据元素属性分组并计算一些东西出来,demo的输出和代码见下面
    
    Supplement 3  ·  Jan 16, 2015
    请帮忙的就是98行到106行之间的代码如果处理的函数个数不定的时候要怎么写,还没想到怎么写递归
    Supplement 4  ·  Jan 16, 2015
    已解决
    .js
    7 replies    2015-01-16 15:11:59 +08:00
    loading
        1
    loading  
       Jan 16, 2015 via Android
    你能把下标直接写n-1,而不是n_1……
    我不敢帮你,我不想玩《无尽的任务-最新资料篇》
    sculley
        2
    sculley  
    OP
       Jan 16, 2015
    图里的写法只是方便表示,程序里没有这些,真的不知道怎么改这种循环
    sumhat
        3
    sumhat  
       Jan 16, 2015
    请独立完成作业
    ligyxy
        4
    ligyxy  
       Jan 16, 2015
    用map或者pandas里的apply函数

    这代码看得太吃力,没法细说了
    ruoyu0088
        5
    ruoyu0088  
       Jan 16, 2015
    用递归就可以解决吧。你给一个能运行的嵌套循环的例子,才好帮你改写。否则不知道结果对不对。
    zerh925
        6
    zerh925  
       Jan 16, 2015
    data_list = [obj1, obj2, ..., objn]
    func_list = [ST1_func, ST2_func, ..., STn_func]
    STout_list = []
    STout_list.append(ST1_func(data_list))

    for index, func in enumerate(func_list[1:]):
    out_list.append(func(out_list[index-1])

    NO WARRANTY!!!
    wangyongbo
        7
    wangyongbo  
       Jan 16, 2015
    def a(d, func):
    if len(func) == 1:
    rturn func[0][x]
    r = func[0](x)
    for k,v in r.iteritems():
    r[k] = a(v, func[1:])
    return r

    a(data_list, func_list)

    这样对不对?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   908 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 22:26 · PVG 06:26 · LAX 15:26 · JFK 18:26
    ♥ Do have faith in what you're doing.