473,783 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem of function calls from map()


Hi, there.

'lines' is a large list of strings each of which is seperated by '\t'
>>lines = ['bla\tbla\tblah ', 'bh\tb\tb', ... ]
I wanna split each string into a list. For speed, using map() instead
of 'for' loop. 'map(str.split, lines)' works fine , but...
when I was trying:
>>l = map(str.split(' \t'), lines)
I got "TypeError: 'list' object is not callable".

To avoid function call overhead, I am not willing to use lambda function
either. So how to put '\t' argument to split() in map() ?

Thanks.

Aug 21 '06 #1
6 2263
Dasn wrote:
So how to put '\t' argument to split() in map() ?
How much is the lambda costing you, according to your profiler?

Anyway, what you really want is a list comprehension:

l = [line.split('\t' ) for line in lines]

Aug 21 '06 #2
Dasn wrote:
>
Hi, there.

'lines' is a large list of strings each of which is seperated by '\t'
>>>lines = ['bla\tbla\tblah ', 'bh\tb\tb', ... ]

I wanna split each string into a list. For speed, using map() instead
of 'for' loop. 'map(str.split, lines)' works fine , but...
when I was trying:
>>>l = map(str.split(' \t'), lines)

I got "TypeError: 'list' object is not callable".

To avoid function call overhead, I am not willing to use lambda function
either. So how to put '\t' argument to split() in map() ?
You can't. Use a lambda or list-comprehension.
map(lambda l: l.split("\t"), lines)

[l.split("\t") for l in lines]
Diez
Aug 21 '06 #3
"Dasn" <da**@bluebottl e.comwrote in message
news:ma******** *************** *************** *@python.org...
>
Hi, there.

'lines' is a large list of strings each of which is seperated by '\t'
>lines = ['bla\tbla\tblah ', 'bh\tb\tb', ... ]

I wanna split each string into a list. For speed, using map() instead
of 'for' loop.
Try this. Not sure how it stacks up for speed, though. (As others have
suggested, if 'for' loop is giving you speed heartburn, use a list
comprehension.)

In this case, splitUsing is called only once, to create the embedded
function tmp. tmp is the function that split will call once per list item,
using whatever characters were specified in the call to splitUsing.

-- Paul

data = [
"sldjflsdfj\tls jdlj\tlkjsdlkfj ",
"lsdjflsjd\tlsj dlfdj\tlskjdflk j",
"lskdjfl\tlskdj flj\tlskdlfkjsd ",
]

def splitUsing(char s):
def tmp(s):
return s.split(chars)
return tmp

for d in map(splitUsing( '\t'), data):
print d
Aug 21 '06 #4
>>tmp is the function that split will call once per list item

should be

tmp is the function that *map* will call once per list item

-- Paul
Aug 21 '06 #5
Paul McGuire wrote:
"Dasn" <da**@bluebottl e.comwrote in message
news:ma******** *************** *************** *@python.org...
>>
Hi, there.

'lines' is a large list of strings each of which is seperated by '\t'
>>lines = ['bla\tbla\tblah ', 'bh\tb\tb', ... ]

I wanna split each string into a list. For speed, using map() instead
of 'for' loop.

Try this. Not sure how it stacks up for speed, though. (As others have
suggested, if 'for' loop is giving you speed heartburn, use a list
comprehension.)

In this case, splitUsing is called only once, to create the embedded
function tmp. tmp is the function that split will call once per list item,
using whatever characters were specified in the call to splitUsing.

-- Paul

data = [
"sldjflsdfj\tls jdlj\tlkjsdlkfj ",
"lsdjflsjd\tlsj dlfdj\tlskjdflk j",
"lskdjfl\tlskdj flj\tlskdlfkjsd ",
]

def splitUsing(char s):
def tmp(s):
return s.split(chars)
return tmp

for d in map(splitUsing( '\t'), data):
print d
And why is this better than

map(lambda t: t.split('\t'), data)

?

Georg
Aug 22 '06 #6
"Georg Brandl" <g.************ *@gmx.netwrote in message
news:ec******** **@news.albasan i.net...
Paul McGuire wrote:
"Dasn" <da**@bluebottl e.comwrote in message
news:ma******** *************** *************** *@python.org...
>
Hi, there.

'lines' is a large list of strings each of which is seperated by '\t'
lines = ['bla\tbla\tblah ', 'bh\tb\tb', ... ]

I wanna split each string into a list. For speed, using map() instead
of 'for' loop.
<snip>

def splitUsing(char s):
def tmp(s):
return s.split(chars)
return tmp

for d in map(splitUsing( '\t'), data):
print d

And why is this better than

map(lambda t: t.split('\t'), data)

?

Georg
Hmm, "better" is a funny word. My posting was definitely more verbose, but
verbosity isn't always bad.

In defense of brevity:
- often (but not always) runs faster
- usually easier to understand as a single gestalt (i.e., you don't have to
jump around in the code, or grasp the intent of a dozen or more lines, when
one or a few lines do all the work), but this can be overdone

In defense of verbosity:
- usually more explicit, as individual bits of logic are exposed as separate
functions or statements, and anonymous functions can be given more
descriptive names
- usually easier to understand, especially for language newcomers
- separate functions can be compiled by psyco

Of course, such generalizations invite obvious extremes and counterexamples .
Prime number algorithms compacted into one-liners are anything but quick to
understand; conversely, I've seen a 40-line database function exploded into
>100 classes (this was in Java, so each was also a separate file!) in
pursuit of implementing a developer's favorite GOF pattern.

This idiom (as used in the splitUsing case) of returning a callable from a
function whose purpose is to be a factory for callables seems to be a common
one in Python, I think I've seen it go by different names: currying, and
closures being most common, and decorators are another flavor of this idea.
Perhaps these idioms ("idia"?) emerged when "lambda" was on Guido's Py3K
chopping block.

So I wouldn't really hold these two routines up for "betterness " - the OP's
performance test shows them to be about the same. To summarize his
performance results (times in CPU secs):
- explicit "for" loop - 20.510 (309130 total function calls; 154563 to
split and 154563 to append)
- list comprehension - 12.240 (154567 total function calls; 154563 to split)
- map+lambda - 20.480 (309130 total function calls; 154563 to <lambdaand
154563 to split)
- map+splitUsing - 21.900 (309130 total function calls; 154563 to tmp and
154563 to split)

The big winner here is the list comprehension, and it would seem it outdoes
the others by halving the number of function calls. Unfortunately, most of
our currying/closure/decorator idioms are implemented using some sort of
"function-calls-an-embedded-function" form, and function calls are poison to
performance in Python (and other languages, too, but perhaps easier to
observe in Python). Even the anonymous lambda implementation has this same
issue.

So the interesting point here is to go back to the OP's OP, in which he
states, "For speed, [I'm] using map() instead of 'for' loop." As it turns
out, map() isn't much of a win in this case. The real, "best" solution is
the list comprehension, not only for speed, but also for ease of readability
and understanding. It's tough to beat this:

return [s.split('\t') for s in lines]

for clarity, explicity, brevity, and as it happens, also for speed.

-- Paul
Aug 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10777
by: Jim Bond | last post by:
Hello, I have found myself in the position of having to manage & "hack" a web site built by someone else. I am trying to make a modification to the javascript code and have been unable to get this working. I admit that I am a total javascript beginner, but have read google & purchased the o'reilley javascript book. So far, all I've been able to achieve are numerous javascript errors! Currently, we have a drop-down box which triggers...
6
6079
by: Richard Berg | last post by:
Hello, I need implementation advice: I am writing a program that should call some API functions in a specific order. The APIs are for another application, not Windows APIs. The order and type of the calls is determined at runtime. There are a couple of hundred of those APIs that can be called, and they all take different parameters. What I need is somehow queue the calls with their parameters and then dequeue and execute them one by...
5
2156
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the called function, I am using p1->find(1) which is returning a valid iterator and not going to the end. I am returning p1 from the called function. But in the calling function, find(1) is going to the end, i.e unable to find the key 1, which was...
4
2404
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
6
3493
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
2599
by: Andy Baxter | last post by:
I have an image scrolling in a viewport for a panoramic image viewer. The viewport can be resized to several set resolutions so people can adjust the size according to their bandwidth. There are hotspots on the image which have been defined using an image map. This map is generated in javascript using the following code (in the object 'tour.'): makeImageMap: function() { // creates an HTML nodeset which is an imagemap for the current...
5
7107
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the connection of an open Asyncore socket. Calling the socket.close() nor the socket.shutdown(2) calls seem to work. The only way I can close the connection without creating the error below is to have the client close the connection. I have the...
2
2309
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 = FooFactory::create("./saved/foo2.eb287ac8"); Now, Foo is an abstract base class, and FooFactory contains a static function which again calls static create functions on Foo1 or Foo2 (see code for all classes below). The problem is that all these functions are static, but I also...
2
3155
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error message as in alert box as " Internet Explorer cannot open the Internet site http://google.citycarrentals.com.au/viewalllocations.php . Operation aborted". It is working in Mozilla . Here i mentioned my code . I am facing this problem several...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9946
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.