如何纠正Python中的元组函数循环?

时间:2016-09-13 04:51:18

标签: python loops tuples

这是一个程序,其中每一行使用元组分成对,这样每个字母都有一个对应的数字作为A:6,B:6,C:35 ..etc如果一个值小于10,那么字母表被转换为N.以下是代码。我发现我的代码不会在代码的最后部分循环遍历元组函数。它只接受一个序列而不会遍历另一个序列

{{1}}

1 个答案:

答案 0 :(得分:0)

##start
function db_prepare(){
    $MySqlConnection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $MySqlConnection.ConnectionString = "server=$MySQLServerName;user id=$Username;password=$Password;database=$MySQLDatenbankName;pooling=false"
    $MySqlConnection.Open()
    $MySqlCommand = New-Object MySql.Data.MySqlClient.MySqlCommand
    $MySqlCommand.Connection = $MySqlConnection
    $MySqlCommand.CommandText = "INSERT INTO `whatever` (col1,col2...) VALUES (@va1,@va2...)"
}
while($true){
    if($MySqlConnection.State -eq 'closed'){ db_prepare() }

    ## do the event reading and data formating stuff
    ## bild some variables to set as sql param values

    $MySQLCommand.Parameters.AddWithValue("@va1",$variable_for_1)
    $MySQLCommand.Parameters.AddWithValue("@va2",$variable_for_2)
    .
    .
    .

    Try{  $MySqlCommand.ExecuteNonQuery() | Out-Null }
    Catch{ <# error handling #> }

}

给出:

all_inputs = []  # <---- add this
for i in range(0,ll):
    sq = l[i]
    sequence = sq.split(" ")[0] ## Stores only the alphabets
    qualities = sq.split(" ")[1:] ## Stores only the numeric
    qualities = filter(None, qualities)
    for sub in sequence:
        if sub == "-":
            idx = list(sequence).index(sub)
            qualities.insert(idx,"0")
    # also add this ***********************
    all_inputs.append((sequence, qualities))

pairs = []
# change this *******************************
for sequence, qualities in all_inputs:
    print sub 
    new_list = []
    for x in range(len(sequence)):
        print x
        new_tuple = (sequence[x], qualities[x]) 
        print new_tuple
        if int(qualities[x]) < 10: 
            new_tuple = ("Z",   qualities[x]) 
        new_list.append(new_tuple)
    pairs.append(new_list)
print pairs