python - Certain elements in for loop outputting differently -
i have array this:
array=[['001', 'playing cards', '0.99', 2, 1.98], ['003', 'keyboard', '12.99', 1, 12.99], ['n/a', 'unavailable', 'n/a', 'n/a', 'n/a'], ['002', 'notebook', '0.59', 4, 2.36]]
in program, have loop this:
for subarray in array: element in subarray: length=((20 if element==subarray[1] else 6)-(len(str(element)))) if element != subarray[4]: print((element)," "*length,end=" ") else: print((element)," "*length)
if run this, program outputs:
001 playing cards 0.99 2 1.98 003 keyboard 12.99 1 12.99 n/a unavailable n/a n/a n/a 002 notebook 0.59 4 2.36
what want program output is:
001 playing cards 0.99 2 1.98 003 keyboard 12.99 1 12.99 n/a unavailable n/a n/a n/a 002 notebook 0.59 4 2.36
no matter how @ code, don't understand why program not outputting sub array containing "unavailable" rest, have made sure structure of array , number of elements in each array same reason still outputs "unavailable" part on new line. apologise if haven't worded question best wondering if spot i'm doing wrong here.
when check element != sub_list[4]
not same thing "is element in index 4", since have multiple "n/a"
values in row same last 1 (therefore thinking end of row) instead can keep count of index on enumerate
:
for i,element in enumerate(sub_array): ... if != 4: #check index instead ...
or break lines after each inner loop:
for subarray in array: element in subarray: length=((20 if element==subarray[1] else 6)-(len(str(element)))) print((element)," "*length,end=" ")
note above uses hard understand way of doing padding, way might bit easier understand:
for subarray in array: element in subarray: if element == subarray[1]: padding = 20 else: padding = 6 print(str(element).ljust(padding), end=" ") print() #end of line
Comments
Post a Comment