Apply function to each element of a list
Using the built-in standard library map
:
>>> mylis = ['this is test', 'another test']
>>> list(map(str.upper, mylis))
['THIS IS TEST', 'ANOTHER TEST']
In Python 2.x, map
constructed the desired new list by applying a given function to every element in a list.
In Python 3.x, map
constructs an iterator instead of a list, so the call to list
is necessary. If you are using Python 3.x and require a list the list comprehension approach would be better suited.
Apply function to all items in a list Python
You can use map
approach:
list(map(myCoolFunction, my_list))
This applies defined function on each value of my_list
and creates a map object (3.x). Calling a list()
on it creates a new list.
Apply function to all the elements (lists of strings) of a column to convert into floats
I think the error is due to the presence of NaN
values in the column c
, one way to fix this is to remove the NaN values before applying the map function:
df['c'] = df['c'].dropna().apply(lambda x: list(map(float, x)))
Apply function to a list of lists
you don't need for
loops to do so. You can directly work with lapply
:
lapply(dati_fault, \(x) colMeans(do.call(rbind, x)))
This does the following: for each entry of dati_fault
(i.e. each sublist of 31 matrices) these matrices are bound together (using rbind
) into one single matrix with 310 rows and 5 columns. Then, colMeans
is applied to this matrix.
If you are not familiar with the shorthand notation for anonymous functions (i.e. \(x)
) you can read about it here.
apply a function over each element of an iterable with sublists
Sounds like recursion should be able to solve that:
a = [1,2,3]
b = [[1,2,3], [4,5,6]]
c = [[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]]]
f = lambda x : x+1
def apply(iterable, f):
# suggestion by Jérôme:
# from collections.abc import Iterable and use
# isinstance(iterable, collections.abc.Iterable) so it works for tuples etc.
if isinstance(iterable, list):
# apply function to each element
return [apply(w, f) for w in iterable]
else:
return f(iterable)
print(apply(a, f)) # [2,3,4]
print(apply(b, f)) # [[2,3,4],[5,6,7]]
print(apply(c, f)) # [[[2,3,4],[5,6,7]],[[8,9,10],[11,12,13]]]
How to apply function to elements of a list?
And what's wrong with
for i in my_things:
i.size = "big"
You don't want to use neither map
nor list comprehansion because they actually create new lists. And you don't need that overhead, do you?
apply function to elements over a list
In this case, maybe you're better off with your data in an array
rather than a list?
#Recreate data
A <- list(a=matrix(1,5,10),b=matrix(2,5,10))
#Convert to array
A1 <- array(do.call(cbind,A),dim = c(5,10,2))
#Better way to convert to array
require(abind)
A1 <- abind(A,along = 3)
#Now we can simply use apply
apply(A1,c(1,2),mean)
Apply function on all elements of list and return a new list based on function's return type
Three problems:
- You want
\ x ->
instead of\ list x ->
. map
will give you a list ofBool
s. If you want to know if they're all true, then you need to either useall
instead, or wrap the result inand
.- Unless
Vector
is an alias for some weird function type, you probably meantdistance x y
ordistance (x,y)
instead ofdistance (x y)
.
Apply function in matrix elements of a list in R
We may use lapply
to loop over the list
and apply the function, extract the eigen
values and then do the conversion to data.frame at the end
eigenvalues <- as.data.frame(do.call(cbind,
lapply(DATA, function(x) round(eigen(x)$values, 2))))
-output
> eigenvalues
V1 V2 V3 V4 V5
1 1.77+3.73i 5.33+0.00i 5.11+0.00i -2.52+3.53i -1.87+4.42i
2 1.77-3.73i 1.72+4.13i -5.08+0.00i -2.52-3.53i -1.87-4.42i
3 -0.50+3.97i 1.72-4.13i 2.41+3.87i 2.12+3.32i 2.96+3.44i
4 -0.50-3.97i -4.02+1.85i 2.41-3.87i 2.12-3.32i 2.96-3.44i
5 -3.38+2.06i -4.02-1.85i -2.60+3.46i 3.72+0.00i -4.15+0.00i
6 -3.38-2.06i -3.27+0.00i -2.60-3.46i -3.16+0.30i 1.67+3.35i
7 3.89+0.00i 1.48+2.89i 0.10+3.78i -3.16-0.30i 1.67-3.35i
8 -2.47+3.00i 1.48-2.89i 0.10-3.78i 2.50+1.89i 3.28+1.47i
9 -2.47-3.00i 3.05+0.00i 3.74+0.00i 2.50-1.89i 3.28-1.47i
10 3.51+0.00i -0.97+2.79i 2.38+2.10i -2.69+1.46i -2.88+1.40i
11 2.04+2.29i -0.97-2.79i 2.38-2.10i -2.69-1.46i -2.88-1.40i
12 2.04-2.29i -1.86+2.07i -2.44+0.01i -1.04+2.51i -1.32+2.89i
13 -3.03+0.00i -1.86-2.07i -2.44-0.01i -1.04-2.51i -1.32-2.89i
14 -1.97+1.67i -2.18+0.00i -1.52+1.78i 0.69+2.32i -0.77+2.12i
15 -1.97-1.67i 2.14+0.00i -1.52-1.78i 0.69-2.32i -0.77-2.12i
16 0.81+1.91i 1.61+0.77i 1.93+0.86i 2.23+0.85i 1.40+1.09i
17 0.81-1.91i 1.61-0.77i 1.93-0.86i 2.23-0.85i 1.40-1.09i
18 1.02+0.00i 0.14+1.55i -0.04+1.88i -0.77+0.57i 0.65+0.35i
19 -0.57+0.47i 0.14-1.55i -0.04-1.88i -0.77-0.57i 0.65-0.35i
20 -0.57-0.47i -0.99+0.00i 0.26+0.00i 0.67+0.00i 0.58+0.00i
Related Topics
Count the Number of Non-Zero Elements of Each Column
Lapply to Add Columns to Each Dataframe in a List
Stop Lapply from Printing to Console
Displaying True When Shiny Files Are Split into Different Folders
First Day of the Month from a Posixct Date Time Using Lubridate
Finding Where Two Linear Fits Intersect in R
How to Connect to a Remote Server with Ssh in R
Union of Intersecting Vectors in a List in R
How to Change Angle of Line in Customized Legend in Ggplot2
Identifying Where Value Changes in R Data.Frame Column
Use Rollapply and Zoo to Calculate Rolling Average of a Column of Variables
Handling Errors Before Warnings in Trycatch
How to Start a for Loop in R Programming
Why Does Rm Inside a Function Not Delete Objects
Partially Read Really Large CSV.Gz in R Using Vroom
Different Axis Limits Per Facet in Ggplot2