Questions tagged [argument-unpacking]

Use this tag for questions related with argument unpacking, a technique that allows Arrays and Traversable objects to be extracted/unpacked into argument lists/sequences.

is usually found in PHP, Python and Go.

144 questions
0
votes
3 answers

Difference between a loop in a function call and listing all the arguments explicitly

I have a function that sorts a list of lists by the first list. When I use the function with the variables like so: sort_lists(IN[0],IN[1],IN[2]) it works perfectly. Although, as I don't know how many lists my input contains, I want to use this as…
0
votes
2 answers

Display list items with custom separator using print() function

Can someone explain to me why when I'm trying to pass unpacked data into print function using asterisk, the optional argument "end" is applied only for the last list's element, and for the rest is default (space) l = ['a', 'b', 'c'] print(*l,…
llwafelll
  • 53
  • 1
  • 4
0
votes
0 answers

new style getargs format but argument is not a tuple error callng floodfill

I am not sure what is going on. This thing worked before and now it dosn't. def generate_unity_texture(self, monKey_data): width, height = self.texture_image.size eye_position = (int(0.88 * width), int(0.88 * height)) …
0
votes
0 answers

List slicing and unpacking

Working on a codefights problem. Got stuck on the solution, or rather, solving the way codefights wanted me too. Here is the solution def listBeautifier(a): res = a[:] while res and res[0] != res[-1]: a, *res, d = res return…
RhythmInk
  • 513
  • 1
  • 4
  • 18
0
votes
1 answer

Tuple of tuple given on slice generation

I have this code: class Foo(object): def __getitem__(self, *args): print len(args), type(args) print args This gives args as a tuple: >>> x[1:] 1 (slice(1, None, None),) But this gives args as a tuple of…
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
0
votes
1 answer

Concept of unpacking a list in Python-- conflicting rules of syntax

>>> x = [1,3] >>> x [1, 3] >>> x[0] 1 >>> x[1] 3 >>> x,y = [1,3] >>> x 1 >>> y 3 >>> x[0] Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not subscriptable As I understand it, a list is a value in…
efw
  • 449
  • 3
  • 16
0
votes
6 answers

Writing a function of two variables as a function in one variable

Let's say I have the following function that is in 2 variables - def banana(x,y): return exp(((-x**2/200))-0.5*(y+0.05*(x**2) - 100*0.05)**2) and I would like to write it as - def banana(x): where x here is a vector of two variables; if…
tattybojangler
  • 1,003
  • 3
  • 15
  • 25
0
votes
0 answers

Syntax Query in Python Statement

[b for _, b in sorted(enumerate(arr, 1), key=lambda x: int.__mul__(*x))] is a list comprehension in Python meant to sort an array in ascending order while using the value at each place multipled by a 1-based index So [4, 3, 1] would be sorted based…
Starlight
  • 111
  • 1
  • 2
0
votes
1 answer

Packing and unpacking dictionary

Obviously I am missing something very easy here but I cannot get the answer. The question is why the code: def func1(arg1, *arg2): print arg1 print arg2 arg1=1 arg2 = [1,2,3] func1(arg1, *arg2) gives 1 (1, 2, 3) while def func2(arg1,…
akotronis
  • 230
  • 2
  • 12
0
votes
1 answer

what is wrong with my system, unable to unpack dict. having unicode objects?

In [1]: l1 = lambda *args, **kw: args In [2]: l1(**{'name':'hello'}) Out[2]: () In [3]: l1(**{u'name':'hello'}) --------------------------------------------------------------------------- TypeError Traceback (most…
shahjapan
  • 13,637
  • 22
  • 74
  • 104
0
votes
1 answer

swap axes in 3d wirefram plot (matplotlib) due to SyntaxError: only named arguments may follow *expression

I'm on Python 3.4.3 and cannot upgrade the system. My issue is that I want do generate 3d wireframe plot using matplotlib and mpl_toolkits.mplot3d ax.plot_wireframe(*a,b, rstride=1, cstride=2) >> SyntaxError: only named arguments may follow…
rtime
  • 349
  • 6
  • 23
0
votes
1 answer

Tuple unpacking in argspec

I've used python2 for years without even knowing about this feature, but apparently tuple unpacking is supported in function defs: >>> def foo(a, (b, c)): ... print a, b, c ... >>> t = (2, 3) >>> foo(1, t) 1 2 3 Defaults are also allowed,…
wim
  • 338,267
  • 99
  • 616
  • 750
0
votes
4 answers

unpacking a string with list comprehension python

I got the following code. hand = '6C 7C 8C 9C TC'.split() so hand is now a list of strings ['6C', '7C', '8C', '9C', 'TC'] then ranks = ["--23456789TJKA".index(r) for r, s in hand] ranks is now [6, 7, 8, 9, 10] The aim is to give card's rank the…
0
votes
1 answer

Matlab Box plot handles/property values

I am trying to access the values used to create a boxplot(). I created a plot: diagram(returns); Found handles of the outliers: o = findobj(diagram,'tag','Outliers'); get the data for the outliers: ydata = get(o,'YData'); problem is it returns…
user3385769
  • 161
  • 6
  • 16
0
votes
1 answer

What would be the best way to handle language files in NodeJS

I am currently working on a website and I want to implement a language file. Meaning, I want to be able to have a file called language.js, which contains an object of all code-generated strings. An example: var languageStrings = { …
Ruben Rutten
  • 1,659
  • 2
  • 15
  • 30
1 2 3
9
10