0

I am trying to find the most efficient method of sorting, testing and assignment through a multi-array in python.

Here is a multi array: containing data of users

persons = [['Person 1', 15, 'USA', 'BLACK'],
           ['Person 2', 12, 'AUS', 'WHITE'],
           ['Person 3', 34, 'CAN', 'PINK'],
           ['Person 4', 18, 'CHINA', 'BLUE']]

Here is my code for sorting through. It doesn't work. Result: Getting 'NOT FOUND!'.

    persons_profile = []
    for i, item in enumerate(persons):

        persons_profile .append(item)
        if(persons_profile [0] == 'Person 1'):
             person_id = persons_profile [0]
             person_age = persons_profile [1]
             person_country = persons_profile [2]
             person_color = persons_profile [3]
             print "Person id", persons_profile [O]
             some_function()

        elif(persons_profile [0] == 'Person 2'):
             person_id = persons_profile [0]
             person_age = persons_profile [1]
             person_country = persons_profile [2]
             person_color = persons_profile [3]
             print "Person id", persons_profile [O]
             some_function()

        elif(persons_profile [0] == 'Person 3'):
            person_id = persons_profile [0]
             person_age = persons_profile [1]
             person_country = persons_profile [2]
             person_color = persons_profile [3]
             print "Person id", persons_profile [O]
             some_function()

        elif(persons_profile [0] == 'Person 4'):
            person_id = persons_profile [0]
             person_age = persons_profile [1]
             person_country = persons_profile [2]
             person_color = persons_profile [3]
             print "Person id", persons_profile [O]
             some_function()

        else:
            print "NOT FOUND!"

What is the best way to sort and test if the item 0, 1, 2, 3 in index 0, 1, 2, 3 in the multi-array equal something?

1 Answer 1

1
persons_profile.append(item)

Here you are putting list item into list persons_profile.

and here:

if persons_profile[0] == 'Person 1':

You are comparing str with list.

And finally since it always False you are going to else branch.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @xi_ How do I go about comparing an item of str/int type inside a list inside list persons_profile? – Code2016 12 mins ago
@Code2016 you should go inside list: if persons_profile[0][0] == 'Person 1':

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.