Python Forum
Python sorts all arrays instead of one.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python sorts all arrays instead of one.
#1
Hello. I want only to sort c1. Using that code it sorts both c and c1
def f(v):
    x=v[0]
    y=v[1]
    return x**2 + x*y + y**2 - 6*x - 9*y
v1 = [0, 0]
v2 = [1, 0]
v3 = [0, 1]
v=[v1,v2,v3]
c=[f(v1),f(v2),f(v3)]
c1=c
c1.sort()
print(*c)
print(*c1)
How to get sorted only c1?
Reply
#2
https://python-forum.io/Thread-List-modi...8#pid90218
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Make a copy of the list. The inline sort mutates the list.
It's like appending, removing or inserting items.

The names c and c1 referring to the same object.

If you don't want to mutate the list, you can use the built-in function sorted(). This does not mutate the object, sorted creates a new list, which is sorted.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2 Different Sorts in a Dataframe stylingpat 1 1,891 Apr-14-2021, 03:02 PM
Last Post: stylingpat
  Writing a lambda function that sorts dictionary GJG 1 1,989 Mar-09-2021, 06:44 PM
Last Post: buran
  Arrays in MATLAB and PYTHON cokhuatlanyeuthuongconhetmuc 2 2,156 Jul-24-2020, 10:47 AM
Last Post: cokhuatlanyeuthuongconhetmuc
  No arrays in Python? twilkinson 2 1,760 Jan-29-2020, 04:42 PM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020