Home > Net >  How to iterate mutli-dimensional arrays in Python?
How to iterate mutli-dimensional arrays in Python?

Time:02-10

Community. I'm working with a Pandas Dataframe and I've reached a point where I need to sample two columns from the DataFrame. I want to do this by converting the sample to a multi-dimensional array but I do not know how to go from there. This is what I have done so far:

for i,j in abcarray:
    if 'positive' in j:
        print(abcarray[i])
        get_image("img1")
    elif 'negative' in j:
        print(abcarray[i])
        get_image("img2")
    elif 'neutral' in j:
        print(abcarray[i])
        get_image("img3")

This is only supposed to print the ith element while the jth element is whatever I've specified. Now usually in Java, I could have a counter variable that iterates through the rows, but how do you do this in Python? I know that I could use "range(len(abcarray))" but it still doesn't help. I'm very new to Python but I have to use Python for this for obvious reasons.

Edit: Adding a bit more context here after the first few replies. So get_image is a function that checks the value and returns the appropriate image. That part of the code works absolutely fine so I did not include that.

The Dataframe is from a CSV but the sample looks like this

["Review Review Review Text Text Text Text" 'positive'] ["Review Review Review Text Text Text Text" 'negative'] ["Review Review Review Text Text Text Text" 'neutral'] ["Review Review Review Text Text Text Text" 'positive'] ["Review Review Review Text Text Text Text" 'positive']

CodePudding user response:

I've taken a stab at what I think you're trying to do. The key is that you've got a nested list which is something like:

myList = [['review is like this', 'positive'],
          ['i dont like', 'negative'],
          ['meh','neutral']]

Doing a for loop the way you've done is not how it's done in python. For loops iterate over objects (such as lists and arrays) and return the element that it's currently up to (which can be any data type). If you want to return the index that it's up to you can either use the range(len(list)) method which you've mentioned , or use

for ix, i in enumerate(myList):
     ...

which returns the index ix and the element of the list i.

In regards to your problem, I assume you just want something as simple as:

def get_image(string):
    #dummy function which just shows which image is being called
    print(string)

for i in myList:
    print(i[0]) #this is the same no matter the if statement
    if 'positive' in i:
        get_image("img1")
    elif 'negative' in i:
        get_image("img2")
    elif 'neutral' in i:
        get_image("img3")

Which outputs:

review is like this
img1
i dont like
img2
meh
img3
  • Related