0

I have one array:

one = ['telephone', 'first_name', 'second_name']

another array:

two = ['first_name', 'second_name', 'telephone']

Can I sort two just like one? In no particular order that is? I always want it ordered it as one

this function:

def sort_list(list1, list2): 
    zipped_pairs = zip(list2, list1) 
    z = [x for _, x in (zipped_pairs)]    
    return z 

three = sort_list(two, one)

THis is sorting the zipped array which I don't want

nb_nb_nb
  • 1,243
  • 11
  • 36
  • Its not the same, and thats what I tried. In that it is ordering the zipped list which is not what I want – nb_nb_nb May 21 '20 at 16:44
  • 3
    Do the two lists always contain the same items? Why not just copy the list? Also what do you mean by "this is sorting the zipped array"? What output do you get versus the output you want? – Neil May 21 '20 at 16:45
  • 2
    What is stopping you from doing `two[:] = one` or so? I don't understand what you're trying to acheive here. Are you looking for a sort index? – Mad Physicist May 21 '20 at 16:51

2 Answers2

1

The sort_list function below should do the trick

# Declare lists from OP example
one = ['telephone', 'first_name', 'second_name']
two = ['first_name', 'second_name', 'telephone']

# Sorting function
def sort_list(a,b):
    # If lists one and two arent of equal size, quit
    if (len(a) != len(b)):
        print("Lengths do not match. Exiting")
        return
    # Otherwise...
    else:
        # Create a new temp list equal to the sizeof one and two
        new_list = [None] * len(a)
        # Loop through the second list
        for x in b:
            # For each object, find where its index is in list one, and set that as the new index for temp list
            new_list[a.index(x)] = x

    # Return the temp list
    return new_list

# Print out before
print("Before: {}".format(two))
# Sort list two
two = sort_list(one, two)
# Print out after
print("After: {}".format(two))

Yields:

Before: ['first_name', 'second_name', 'telephone']
After: ['telephone', 'first_name', 'second_name']
artemis
  • 6,857
  • 11
  • 46
  • 99
  • Am I being an idiot or, does saying new_list[a.index(x)]=x not just have the effect of creating a copy of a? – Neil May 21 '20 at 16:49
  • 1
    This sounds like a really fancy way to say `two[:] = one` or even just `list(one)` – Mad Physicist May 21 '20 at 16:53
  • @noob if this works for you, please accept it as the correct answer – artemis May 21 '20 at 16:54
  • @MadPhysicist Don't you know that a developer's job is to write code, not to solve problems? What if wundermahn bills for each line of code they write? – Neil May 21 '20 at 16:58
  • @Neil. While my user name is only 90% accurate (I sold out and became an engineer), I've never been exactly a developer. But I have heard stories... – Mad Physicist May 21 '20 at 18:11
0

Unless I'm missing something, what you're doing, and what the other answers do, is just make a copy of a.

I therefore suggest the neater:

three = [x for x in one]
Neil
  • 3,020
  • 4
  • 25
  • 48