list - How to loop with string formatting in Python? -
i have 2 variables constructing new string from, 1 of variables isn't looping correctly. expecting several values, resultant output first values sequence. how can construct/place string formatting correctly correctly loops through either variable properly? if move string formatting either loop, loops properly, i.e. if in count, counts appear correctly, items not.
code:
for dsr in lservicerequests: dlistofla311electronicwaste = dsr["listofla311electronicwaste"] if("la311electronicwaste" in dlistofla311electronicwaste): lla311electronicwaste = dlistofla311electronicwaste["la311electronicwaste"] dla311electronicwaste in lla311electronicwaste: v_ewaste_item_count= dla311electronicwaste[k_ewaste_item_count] print v_ewaste_item_count
output:
1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size) 1 1 1 2 computers laptops/tablets cell phone tv (any size)
code list comprehension:
dsr in lservicerequests: k_ewaste_info = ' ' dlistofla311electronicwaste = dsr["listofla311electronicwaste"] if("la311electronicwaste" in dlistofla311electronicwaste): lla311electronicwaste = dlistofla311electronicwaste["la311electronicwaste"] dla311electronicwaste in lla311electronicwaste: v_ewaste_type = dla311electronicwaste[k_ewaste_type] # print v_ewaste_type v_ewaste_item_info = '{0}, {1} '.format(v_ewaste_item_count, v_ewaste_type) if k_ewaste_info != v_ewaste_item_info: k_ewaste_info = v_ewaste_item_info print v_ewaste_item_info
output list comprehension:
2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size) 2, computers 2, laptops/tablets 2, cell phone 2, tv (any size)
Comments
Post a Comment