Skip to main content
 首页 » 编程设计

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

2025年05月04日10lidabo

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

L= [('A', Decimal('52.00')), ('B', Decimal('87.80')), ('G', Decimal('32.50'))] 

我想使用for循环元素中的两个项目

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)  

请您参考如下方法:

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

for str_data, float_data in L: 
    # str_data is the string, float_data is the Decimal object