小编use*_*317的帖子

如果列表索引存在,请执行X.

在我的程序中,用户输入数字n,然后输入n字符串数,这些字符串存储在列表中.

我需要编写代码,如果存在某个列表索引,则运行一个函数.

由于我已经嵌套if语句,因此这变得更加复杂len(my_list).

这是我现在拥有的简化版本,它不起作用:

n = input ("Define number of actors: ")

count = 0

nams = []

while count < n:
    count = count + 1
    print "Define name for actor ", count, ":"
    name = raw_input ()
    nams.append(name)

if nams[2]: #I am trying to say 'if nams[2] exists, do something depending on len(nams)
    if len(nams) > 3:
        do_something
    if len(nams) > 4
        do_something_else

if nams[3]: #etc.
Run Code Online (Sandbox Code Playgroud)

python python-2.7

115
推荐指数
6
解决办法
23万
查看次数

编码西格玛公式?

Python初学者,运行2.7

我找不到Pythonic方法来编写以下公式.我的目标是有一个单一的功能,我可以输入值(在以前的尝试我只发现了一个高度重复的暴力解决方案).

式

必要的信息:

如果您不知道sigma符号的含义:请看这里

# 'v' is the formula
# 'i' 'j' 'k' are actors with numerical attributes 'c' 's' and 'x'
# 'n' = length of the following lists (length is identical for all four lists)

values_1 = [Bob, Sue, Mary, Jo...] # i, j, and k are any 3 values drawn from this list  
                        # under the condition that i != j != k

values_2 = [5,6,7,8...] # c is drawn from this list. 
                        # if …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

8
推荐指数
1
解决办法
3万
查看次数

函数使用print进行迭代但不返回

Python新手在这里,运行2.7.

我正在尝试创建一个程序,该程序使用函数生成文本,然后将函数生成的文本输出到文件.

当只在powershell中打印函数时(如:http://codepad.org/KftHaO6x),它会迭代,因为我希望它:

def writecode (q, a, b, c):
    while b < q:
        b = b + 1
        print "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,b,c,a,a,a,b,c)
        print "votes%d_%d.append(v%d_%d_%d)" % (b,c,a,b,c,)
        print "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,c,b,a,a,a,c,b)
        print "votes%d_%d.append(v%d_%d_%d)" % (c,b,a,c,b)

writecode (5,1,0,4)
Run Code Online (Sandbox Code Playgroud)

当尝试将函数输出到文件中时(例如:http://codepad.org/8GJpp9QY),它只给出1个值,即不迭代:

def writecode (q, a, b, c):
    while b < q:
        b = b + 1
        data_to_write = "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,b,c,a,a,a,b,c)
        data_to_write_two = "votes%d_%d.append(v%d_%d_%d)" % (b,c,a,b,c,)
        data_to_write_three = "v%d_%d_%d = …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

将不同的预测方法传递给R中的层次时间序列预测?

我有一个分层的时间序列,其中底层系列都表现出间歇性的需求.使用Hyndman的HTS软件包在层次结构中实现最佳组合似乎是有利的.使用Kourentzes的MAPA包进行间歇性需求的多重聚合预测似乎也是有利的.从本质上讲,我想做的事情如下:

forecast(my_hts, method='comb', fmethod='MAPA')

但是,我不清楚是否/如何将两者结合起来,因为forecast.gts()只接受fmethod=c("ets", "arima", "rw").

是否有一种聪明的方法可以传递不同的预测方法forecast.gts()而不必撕掉代码?

举例澄清我的意思:

library(hts)
library(MAPA)
set.seed(1)

#note intermittent demand of bottom level time series
x <- ts(rpois(365, lambda=0.05), frequency=365, start=2014)
y <- ts(rpois(365, lambda=0.07), frequency=365, start=2014)

#it's easy to make a MAPA forecast for the top-level time series
#but this isn't an optimal hierarchical forecast
mapasimple(x+y)

#it's also easy to make this a HTS and make an optimal hierarchical forecast
#but now I cannot use MAPA
z <- hts(data.frame(x,y))) …
Run Code Online (Sandbox Code Playgroud)

r time-series hierarchical forecasting

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

标签 统计

python ×3

python-2.7 ×3

forecasting ×1

hierarchical ×1

r ×1

time-series ×1