Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In For loop using two elements in python

In python list I have two items per element, 1st a str, second a float

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

I want use a for loop both item in the element

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) 
like image 488
Merlin Avatar asked Jan 26 '26 22:01

Merlin


2 Answers

Change your for loop to something like this:

for str_data, float_data in L:
    # str_data is the string, float_data is the Decimal object
like image 136
Andrew Clark Avatar answered Jan 29 '26 11:01

Andrew Clark


Two ways:

First you could access the members of row:

#For string:
row[0]
#For the number:
row[1]

Or you specify your loop this way:

for (my_string, my_number) in l:
like image 31
tillsten Avatar answered Jan 29 '26 10:01

tillsten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!