Home > Enterprise >  how can I iterate over rows and columns in different variables using python?
how can I iterate over rows and columns in different variables using python?

Time:07-20

for x in content:
   print(x)

it prints all the contents line by line but I am trying to store those values in each variable and update variable when line changes using python the contents are as follows. [1,2,3], [4,5,8],[3,3,1]. I have a content from csv in a variable named content.

CodePudding user response:

a = [1, 2, 3, 4, 5, 8, 3, 3, 1]
for i in range(0, len(a), 3):
    x = a[i]
    y = a[i   1]
    z = a[i   2]
    print(x, y, z)
    print([x, y, z])

I hope it will help you. Correct me if wrong.

CodePudding user response:

You can try This:

content= [[1,2,3], [4,5,8],[3,3,1]]
for x in content:
    a, b, c = [x[i] for i in range(len(x))]
    
print(a)
print(b)
print(c)

output:

3
3
1
  • Related