打印列表中的每个项目

时间:2016-12-20 12:48:59

标签: python list for-loop exception

当我执行附加代码时,我希望它:

            for sub_list in mainList:
            sub_list = sub_list.split(",")
            if len(sub_list) == 9:
                for i in sub_list:
                    try:
                        i = int(i)
                    except ValueError:
                        return "Error! has to contain integers only"
                    else:
                        i = str(i)
                print(sub_list)
            elif len(sub_list) > 9:
                return 'Error! more than 9 numbers in a line'

对于mainList中的所有子列表(总共9个子列表)。 我怎么能这样做?

['3', '8', '0', '4', '0', '0', '7', '0', '2']
['0', '0', '0', '7', '2', '0', '0', '0', '0']
['0', '0', '0', '7', '2', '0', '0', '0', '0']
['0', '2', '4', '8', '0', '6', '9', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '1', '2', '0', '3', '5', '4', '0']
['0', '0', '0', '0', '5', '8', '0', '0', '0']
['9', '0', '3', '0', '0', '4', '0', '2', '8']
['0', '0', '8', '0', '0', '0', '0', '5', '7']
['3', '8', '0', '4', '0', '0', '7', '0', '2']
['0', '0', '0', '7', '2', '0', '0', '0', '0']
['0', '0', '0', '7', '2', '0', '0', '0', '0']
['0', '2', '4', '8', '0', '6', '9', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '1', '2', '0', '3', '5', '4', '0']
['0', '0', '0', '0', '5', '8', '0', '0', '0']
['9', '0', '3', '0', '0', '4', '0', '2', '8']
['0', '0', '8', '0', '0', '0', '0', '5', '7']

当我执行代码时,会出现这种情况:

#First we set up the data structure. 
#In this simple model we have N variables, and each variable has a value at each time period. 
#There are T time periods.
N <- 2
T <- 20
variables <- data.frame(matrix(0,T,N))
#I assign names to the variables, and check that I've given the right number of names
Names <- c("Movement", "AIpapers")
if(length(Names)==N){names(variables) <- Names} else {print("Error")}

#Now I assign the basic function that, given parameter values, runs a simulation over T time periods.
indeffbasic <- function(a0=5000, b0=100, a1, b1){
    for (i in 1:T) {
        variables[i, "Movement"] <- (if(i-1>0){a1* variables[i-1, "Movement"]}else{a0})
        variables[i, "AIpapers"] <- (if(i-1>0){variables[i-1, "AIpapers"]}else{b0}) + (if(i-3>0){b1*variables[i-3, "Movement"]}else {0})
    }
    return(variables)
}

#This function works:
indeffbasic(a1=10, b1=2)

#Since I want a1 and b1 to be randomly generated each time, 
#I define a function that randomly generates these values and returns them
genpar <- function () {
    a1 <- rnorm(1, 1.1, 0.02)
    b1 <- rnorm(1)
    parameters <- c(a1, b1)
    return(parameters)
}

#This function also seems to work
genpar()

#Now I define a function that randomly generates a1 and b1
#and then passes them to the indeffbasic function I defined above
#so each time I call this is like a run of the simulation.
indeff <- function(a0=5000, b0=100) {
    parameters <- as.list(c(a0, b0, genpar()))
    names(parameters) <- c("a0", "b0", "a1", "b1")
    return(do.call(indeffbasic(), parameters))
}

#But this doesn't work: it returns "Error: argument "a1" is missing, with no default"
indeff()

2 个答案:

答案 0 :(得分:0)

试试这个 -

for sub_list in mainList:
    if len(sub_list) == 9:
        ans = []
        for num in sub_list:
            ans.append(str(num))
    return ','.join(ans)

答案 1 :(得分:0)

一个问题是i = int(i)。这不会更新列表中的值。 而不是:将它们变为整体,你可以使用列表理解:(我尽可能少地修改)

for sub_list in mainList:
    sub_list = sub_list.split(",")
    if len(sub_list) == 9:
        try:
            # Create a new list that is printed
            print([int(i) for i in sub_list])
        except ValueError:
            return "Error! has to contain integers only"
    elif len(sub_list) > 9:
        return 'Error! more than 9 numbers in a line'
    else:
        return 'Error! less than 9 numbers in a line'

这样做的缺点是,如果您的上一个列表中有错误,则会打印该列表之前的所有其他有效列表。如果您只想在没有错误的情况下打印它们,则必须将它们保存在打印的(临时)列表中,或者在for循环后返回。