[Python] Iterating through list not working

Soldato
Joined
12 May 2014
Posts
5,354
I'm trying to iterate through a list but python isn't pulling any elements in from the list. I've checked and the list is definitely populated, it is a list and it contains no empty elements. Any ideas how to fix this?

Here is the first half of the function that is given me trouble
def assemble_compound(self):
#function attempts to construct compound units from the units in instance
#Get all compound units

def compare_expo (cu_dict, ist_dict):
#Compares the exponential
for thing in cu_dict.keys():
if cu_dict[thing] != ist_dict[thing]:
return False

return True

ul, ul_cu, ue, ug = func_comp(cu)

print ("tick")
print (ul)
for item in ul:
print ("item tick: {}".format(item))

print ("Tock")

#Get compound
#check is units are present
#check units are the same expo

print ("Check: {}".format(ul))

print ("rev grp: {}".format(self.unit_grp_rev.keys()))
print ("self.expo: {}".format(self.unit_expo))
for item in ul:
#Check that all groups necessary for compound unit is present and that the
#exponential are at least larger than necessary
#Only considers exact matches
print (ul)
print (ug)
print (ue)
print ("item: ".format(item))
print ("ug: ".format(ug[item]))
print ("ue: ".format(ue[item]))

Here is the output
tick
['N']
item tick: N
Tock
Check: ['N']
rev grp: dict_keys(['length', 'mass', 'other'])
self.expo: {'s': -2.0, 'm': 1, 'kg': 1}
['N']
{'N': {'s': 'second', 'm': 'length', 'kg': 'mass'}}
{'N': {'s': -2.0, 'm': 1.0, 'kg': 1.0}}
item:
ug:
ue:

In the first for loop (which is for testing purposes) it iterate through ul fine. But in the second for loop it appears to be having trouble
 
the reason my code wasn't working as expected was because my list of supported units wasn't complete. However that didn't explain why the outputs where coming up blank.

@AHarvey
Do you mean like github? and if yes are there any good free alternatives?
 
the code is a mess but anyway, you arent using the string format method correctly, missing curky braces {}
 
Back
Top Bottom