Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with arguments in two lists

I have two lists xscat and yscat. I would like the list comprehension to pick up x and y in xscat and yscat respectively. Resulting list should contain peaks([x[0], y[0]]), peaks([x[1], y[1]]) , etc

xscat=yscat=[-1, -1.5,5] [peaks([x,y]) for x,y in xscat,yscat] 

Can you find any solution using comprehensions ? or other ways to put it (map)?

like image 666
kiriloff Avatar asked Feb 07 '12 21:02

kiriloff


People also ask

How do you pass two lists as an argument in Python?

The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc) and returns a list of results or map object. Note : You can pass as many iterable as you like to map() function.

Can a function argument be a list?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

Can a function have multiple arguments?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function. This video doesn't have any notes.


2 Answers

zip is what you want:

[peaks([x,y]) for x,y in zip(xscat,yscat)] 
like image 156
Ned Batchelder Avatar answered Sep 21 '22 08:09

Ned Batchelder


You need to use zip:

[peaks([x,y]) for (x,y) in zip(xscat, yscat)] 
like image 45
Matt Fenwick Avatar answered Sep 17 '22 08:09

Matt Fenwick