20608

Sorting a list by function leads to wrong result [closed]

Question:

When passing a list as an argument in function, why is the following list not changed?

def foo(*x): y=sorted(x) print(y) a=[3,2,1]

The function is returning [[3, 2, 1]], not [[1,2,3]]. Why is this happening? Is this something to do with call by value?

Answer1:

Why creating another function? You can just do this

>>> a = [3,2,1] >>> sorted(a) [1, 2, 3]

However if you want to create another function. You have to call it.

def foo(x): y=sorted(x) return y a = [3,2,1] print(foo(a))

Answer2:

As Blue Monday says, there's no need to create a new function for this, you should just call the sorted function. However, here are a couple of other ways to fix your code.

a = [3, 2, 1] def foo1(*x): y = sorted(*x) print(x, y) return y print(foo1(a)) def foo2(*x): y = sorted(x) print(x, y) return y print(foo2(*a)) print(foo2(5, 4, 7, 6))

<strong>output</strong>

([3, 2, 1],) [1, 2, 3] [1, 2, 3] (3, 2, 1) [1, 2, 3] [1, 2, 3] (5, 4, 7, 6) [4, 5, 6, 7] [4, 5, 6, 7]

Note that foo2 expects us to pass in the items to sort as individual arguments. We can pass it a list (or tuple) by using the * "splat" operator in the function call. See <a href="https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">Unpacking Argument Lists</a> in the official Python tutorial.

Answer3:

because the argument of your function is specified as *a, which is like saying your argument is a tuple of undefined dimension

when you try to sort a tuple with a nested list, the value will not change

infact as result you got a list of list (you got [[3, 2, 1]] not [3, 2, 1])

if you try this, it will work

def foo(*x): y=sorted(x[0]) print(y)

Recommend

  • Can you add a mouse listener to an object in paintComponent
  • RMongo dbGetQueryForKeys(), what is the structure of “keys,” and how do I sub-key them?
  • How send Ajax to remote server in extjs6?
  • Java library for reading multipart/form-data http body containing multiple files
  • LINQ to Entities does not recognize the method
  • PHP create range of dates
  • Full calendar business hour constrain on day click event
  • submit button to contingent pages [duplicate]
  • Drupal: create a node with employee working hours
  • File upload php, get only file name
  • UIButton Borders Function Only Gives Back White Borders
  • Convert two columns Pandas data frame to dictionary of list with first column as keys
  • Responsive left sidebar open close
  • Possible to get mouse events fired when cursor is outside page?
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Javascript Callbacks with Object constructor
  • How to make Safari send if-modified-since header?
  • jquery mobile loadPage not working
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?