Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing a list by reference
#3
You're not passing lists to switch, you are passing the values in the list. So the actual exchange isn't involving the lists.

Is this homework? Because this is just a really bad idea all around. But assuming it's homework, have you covered tuple assignment?

data[x], data[y] = data[y], data[x]
That will handle what the swap function does. Otherwise you will need to either do the swap stuff in the sortList function, or pass three things to the swap function: The whole list and the two indexes.

Note that your loop is going to mess it all up. You are looping through all of the indexes. So say you start the loop for x == 5. The first y value will be 0, and you will be comparing data[5] to data[0], which is way before data[5]. You need to look into sorting algorithms. This kind of looks like bubble sort, but there you just check adjacent indexes, and you check them until no further swaps are needed.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Passing a list by reference - by virgik89 - Jun-30-2018, 03:07 AM
RE: Passing a list by reference - by gontajones - Jun-30-2018, 03:18 AM
RE: Passing a list by reference - by ichabod801 - Jun-30-2018, 03:23 AM
RE: Passing a list by reference - by virgik89 - Jun-30-2018, 03:54 AM
RE: Passing a list by reference - by volcano63 - Jun-30-2018, 10:26 AM
RE: Passing a list by reference - by ljmetzger - Jun-30-2018, 08:12 PM
RE: Passing a list by reference - by virgik89 - Jul-01-2018, 04:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,713 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Passing List of Objects in Command Line Python usman 7 3,250 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,593 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Mathplotlib - passing reference to axs to function qmfoam 5 3,012 Aug-17-2020, 09:02 PM
Last Post: qmfoam
  reference in pop function for linked list oloap 0 1,595 Mar-14-2020, 05:52 PM
Last Post: oloap
  set a new object node in a linked list via reference oloap 2 2,142 Mar-13-2020, 09:45 PM
Last Post: oloap
  Problem with List Reference CH_NoLuck 1 1,706 Feb-22-2020, 03:27 AM
Last Post: CH_NoLuck
  Passing an argument by reference Exsul 12 4,789 Aug-22-2019, 07:29 AM
Last Post: DeaD_EyE
  Passing by reference or value CanadaGuy 4 3,010 Nov-12-2018, 08:44 PM
Last Post: CanadaGuy
  List 3 dimensions reference does not work Mario793 1 2,672 Mar-02-2018, 12:35 AM
Last Post: ka06059

Forum Jump:

User Panel Messages

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