在Python中使用两个元素的For循环

Mer*_*lin 2 python for-loop

在 python 列表中,每个元素有两个项目,第一个是 str,第二个是 float

L= [('A', Decimal('52.00')), ('B', Decimal('87.80')), ('G', Decimal('32.50'))]
Run Code Online (Sandbox Code Playgroud)

for loop我想在元素中使用两个项目

NewL= []
for row in L:

    ### do something with str
    InSql= "SELECT  " % str
    f= csr.execute(InSql)
    ns = list(f)  

    ###do something with float
    InSql= "SELECT  " % float
    f= csr.execute(InSql)
    nf = list(f) 

    NewL.append(str, float,ns, nf) 
Run Code Online (Sandbox Code Playgroud)

And*_*ark 5

将循环更改for为如下所示:

for str_data, float_data in L:
    # str_data is the string, float_data is the Decimal object
Run Code Online (Sandbox Code Playgroud)

  • 顺序会被保留,因此只要“L”中的每个元组都以相同的方式排序,就可以保证“str_data”将是字符串,“float_data”将是“Decimal”。 (2认同)