Hello,
I'd like to apply a function to elements of a nested list and wondered
if there is anything more idiomatic and/or shorter than this recursive
way: def recur_map(f, data):
.... if isinstance(data, list):
.... mapped_list = []
.... for i in data:
.... mapped_list.append(recur_map(f, i))
.... return mapped_list
.... else:
.... return f(data)
.... recur_map(lambda x: x*2, [[1, 2], 3, 4, [5, 6, [7, 8]]])
[[2, 4], 6, 8, [10, 12, [14, 16]]]
Thanks,
alex 2 7549
I think for most purposes a program like this is short enough:
def recur_map2(fun, data):
if hasattr(data, "__iter__"):
return [recur_map2(fun, elem) for elem in data]
else:
return fun(data)
data = [set([1, 2]), [3], 4, [5, {6:4}, [7, 8]]]
print recur_map2(lambda x: x*2, data)
Bye,
bearophile
Uglier than yours, but down to two lines:
def recur_map(f, data):
return [ not hasattr(x, "__iter__") and f(x) or recur_map(f, x) for x
in data ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Carlos Ribeiro |
last post by:
Hello all,
Here I am using some deeply nested, tree-like data structures. In some
situations I need to traverse the tree; the old-style way to do it is
to write a recursive method on the node...
|
by: Oxmard |
last post by:
Armed with my new O'Reilly book Optimizing Oracle Performance I have been
trying to get a better understanding of how Oracle works.
The book makes the statement, " A database cal with dep=n + 1...
|
by: Rodusa |
last post by:
I am having problem to apply updates into this function below. I tried
using cursor for updates, etc. but no success. Sql server keeps telling
me that I cannot execute insert or update from inside...
|
by: aurora |
last post by:
I love generator and I use it a lot. Lately I've been writing some
recursive generator to traverse tree structures. After taking closer look
I have some concern on its performance.
Let's take...
|
by: Nicolas Vigier |
last post by:
Hello,
I have in my python script a function that look like this :
def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
if type(arg1) is ListType:
for a in arg1:
my_function(a, arg2,...
|
by: Mike MacSween |
last post by:
Regular viewers may want to turn off now.
This will be an orchestral management system. Musicians and other staff
being booked/paid for jobs.
A job may contain other jobs, e.g:
World Tour...
|
by: Gary Manigian |
last post by:
I have 2 tables, one-to-many, that contain bills of material(BOMs):
tblBOM:
lngBOMID (PK)
strAssemblyPartNo
strDescription
tblBOMDetail:
lngBOMDetailID (PK)
lngBOMID (FK)
|
by: Dennis Haney |
last post by:
Hi
As far as I can tell, the pull_up_IN_clauses does not optimize
recursively. Am I totally misguided here?
Index: plan/subselect.c...
|
by: Gentr1 |
last post by:
Hi everybody!
I am presently working on a Genetic Programming API in python.
I have a bit of a problem at the moment...
For some specific reasons, I am using nested lists data structure to...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |