在我的程序中,用户输入数字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初学者,运行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新手在这里,运行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) 我有一个分层的时间序列,其中底层系列都表现出间歇性的需求.使用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)