Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread Steven D'Aprano
On Mon, Mar 23, 2015 at 10:17:11PM -0400, Dave Angel wrote: > On 03/23/2015 09:42 PM, boB Stepp wrote: > >Can you give me a ballpark number for "large", where this would start > >making a meaningful difference? > > > > Not really. See Steve's response for some numbers. o_O Have you borrowed Gu

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread Dave Angel
On 03/23/2015 10:17 PM, Dave Angel wrote: On 03/23/2015 09:42 PM, boB Stepp wrote: Not really. See Steve's OOPS. Peter's > response for some numbers. If I had to guess, I'd say that for lists over 100 items, you should use bisect or equivalent. But I'd also say you should have one algo

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread Steven D'Aprano
On Mon, Mar 23, 2015 at 08:42:23PM -0500, boB Stepp wrote: > On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: > > The catch to a list comprehension is it has to visit all the elements, while > > a binary search would visit log-base-2 of them. So instead of 1 > > elements, you'd be searchin

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread Dave Angel
On 03/23/2015 09:42 PM, boB Stepp wrote: On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: The catch to a list comprehension is it has to visit all the elements, while a binary search would visit log-base-2 of them. So instead of 1 elements, you'd be searching about 14 items. I suspect

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread boB Stepp
On Thu, Mar 19, 2015 at 4:52 AM, Peter Otten <__pete...@web.de> wrote: > Dave Angel wrote: [...] > By the way, if you were to use a plain old loop the expected speedup over > the listcomp would be 2. You can break out of the loop when you have found > the gap, after iterating over one half of the

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-23 Thread boB Stepp
On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: > The catch to a list comprehension is it has to visit all the elements, while > a binary search would visit log-base-2 of them. So instead of 1 > elements, you'd be searching about 14 items. I suspected as much, but had not verified this.

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-20 Thread Peter Otten
Patrick Thunstrom wrote: The generalized problem: L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test value being searched for. I need to know the indices i and i + 1, which I need to interpol

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-19 Thread Patrick Thunstrom
>>> The generalized problem: >>> >>> L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . >>> Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test >>> value being searched for. I need to know the indices i and i + 1, >>> which I need to interpolate based on where Vt falls. >>>

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-19 Thread Peter Otten
Dave Angel wrote: > On 03/19/2015 12:20 AM, boB Stepp wrote: >> I hope extolling the beauty and power of Python on this list is >> allowed, because I have had a large "WOW!!!" moment tonight. I had a >> problem I was working on at work this afternoon. I have a list of ~ >> 10,000 floating point nu

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-18 Thread Mark Lawrence
On 19/03/2015 04:20, boB Stepp wrote: I hope extolling the beauty and power of Python on this list is allowed, because I have had a large "WOW!!!" moment tonight. I had a problem I was working on at work this afternoon. I have a list of ~ 10,000 floating point numbers, which run from largest to s

Re: [Tutor] List comprehensions to search a list--amazing!

2015-03-18 Thread Dave Angel
On 03/19/2015 12:20 AM, boB Stepp wrote: I hope extolling the beauty and power of Python on this list is allowed, because I have had a large "WOW!!!" moment tonight. I had a problem I was working on at work this afternoon. I have a list of ~ 10,000 floating point numbers, which run from largest t

[Tutor] List comprehensions to search a list--amazing!

2015-03-18 Thread boB Stepp
I hope extolling the beauty and power of Python on this list is allowed, because I have had a large "WOW!!!" moment tonight. I had a problem I was working on at work this afternoon. I have a list of ~ 10,000 floating point numbers, which run from largest to smallest. There are duplicates scattered

Re: [Tutor] list comprehensions

2009-10-09 Thread Kent Johnson
On Fri, Oct 9, 2009 at 1:21 AM, wesley chun wrote: > [generator expressions] are > "lazy" because you iterate over the values one at a time instead of > creating the entire list. they are slightly slower but save memory. I don't think you can make a blanket statement comparing speed of list comp

Re: [Tutor] list comprehensions

2009-10-08 Thread wesley chun
> I've been studying python now for a few weeks and I've recently come > into list comprehensions. [...] > Those make sense to me. The way I understand them is: > do something to x for each x in list, with an optional qualifier. that's pretty much correct. > On the other hand I've seen a few exa

Re: [Tutor] list comprehensions

2009-10-08 Thread Oxymoron
Hello, On Wed, Oct 7, 2009 at 6:57 AM, Christer Edwards wrote: > > do something to x for each x in list, with an optional qualifier. > To be more precise: http://docs.python.org/tutorial/datastructures.html#list-comprehensions "Each list comprehension consists of an expression followed by a fo

Re: [Tutor] list comprehensions

2009-10-08 Thread Gregor Lingl
Christer Edwards schrieb: I've been studying python now for a few weeks and I've recently come into list comprehensions. Some of the examples that I've found make sense, and I find them readable and concise. In particular I'm referring to the python docs on the topic (http://docs.python.org/tut

[Tutor] list comprehensions

2009-10-08 Thread Christer Edwards
I've been studying python now for a few weeks and I've recently come into list comprehensions. Some of the examples that I've found make sense, and I find them readable and concise. In particular I'm referring to the python docs on the topic (http://docs.python.org/tutorial/datastructures.html#list

Re: [Tutor] List comprehensions

2008-04-10 Thread bob gailer
ith a built-in > would help. Maybe not? > > Hope that helps. > > Dinesh > > > - Original Message - > *From:* Kent Johnson <mailto:[EMAIL PROTECTED]> > *To:* Dinesh B Vadhia <mailto:[EMAIL PROTECTED]> > *Cc:* tutor@python.org <mail

Re: [Tutor] List comprehensions

2008-04-10 Thread Kent Johnson
Alan Gauld wrote: > One possibility is that the javascript fetches the list back on the > first few characters and caches it on the browser Here is an autocomplete widget I use that can do exactly that: http://www.dyve.net/jquery/?autocomplete Kent ___

Re: [Tutor] List comprehensions

2008-04-10 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote >> application/service. Each time a user inputs a character, the >> character >> is sent to the backend Python program which searches for the >> character >> in a list of >10,000 string items. Once it finds the character, >> the >> backend will return

Re: [Tutor] List comprehensions

2008-04-10 Thread Kent Johnson
Dinesh B Vadhia wrote: > Kent > > I'm using a Javascript autocomplete plugin for an online web > application/service. Each time a user inputs a character, the character > is sent to the backend Python program which searches for the character > in a list of >10,000 string items. Once it finds

Re: [Tutor] List comprehensions

2008-04-10 Thread W W
My guess, though I'm not sure, is that google uses hashes... why? Because they're a *ahem* load faster than loops, and the reason is they replace the repetitive nature of a loop by using some type of formula. Exactly /how/ this is implemented, I'm not sure. A simple example of the speed differenc

Re: [Tutor] List comprehensions

2008-04-10 Thread linuxian iandsd
i think you are using ajax ... which undoubdetly uses an sql database since its based on queries run from whithin the application in the browser whithout the need for refreshing the page ... i would suggest you try serching internet for something like "google autocomplete feature" & i guess the qu

Re: [Tutor] List comprehensions

2008-04-10 Thread linuxian iandsd
also if you need to go for 2 results I propose you use filters & interactive menus which will help you tailor the query to the users desires & thus limit the query results. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinf

Re: [Tutor] List comprehensions

2008-04-10 Thread Alan Gauld
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote i > I'm using a Javascript autocomplete plugin for an online > web application/service. Each time a user inputs a character, > the character is sent to the backend Python program which > searches for the character in a list of >10,000 string items.

Re: [Tutor] List comprehensions

2008-04-09 Thread Dinesh B Vadhia
ed sensible that replacing the for loop with a built-in would help. Maybe not? Hope that helps. Dinesh - Original Message - From: Kent Johnson To: Dinesh B Vadhia Cc: tutor@python.org Sent: Wednesday, April 09, 2008 1:48 PM Subject: Re: [Tutor] List comprehensions Dinesh B Vadhia wr

Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 4:48 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > You could use > [ sys.stdout.write(some operation on item) for item in data ] > > but I consider this bad style and I seriously doubt you will see any > difference in performance. This really isn't a good idea. It will

Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 4:15 PM, Dinesh B Vadhia <[EMAIL PROTECTED]> wrote: > Sorry, let's start again. This version really isn't any more helpful than the first one. I know you corrected the sample code, but you haven't addressed any of the fundamental questions that Kent or I asked. > I want to

Re: [Tutor] List comprehensions

2008-04-09 Thread Kent Johnson
Dinesh B Vadhia wrote: > Here is a for loop operating on a list of string items: > > data = ["string 1", "string 2", "string 3", "string 4", "string 5", > "string 6", "string 7", "string 8", "string 9", "string 10", "string 11"] > > result = "" > for item in data: > result = item > pr

Re: [Tutor] List comprehensions

2008-04-09 Thread linuxian iandsd
a potentially better structure preferably a built-in. > > Hope this makes sense! Thank-you. > > Dinesh > > > > - Original Message - *From:* Kent Johnson <[EMAIL PROTECTED]> > *To:* Dinesh B Vadhia <[EMAIL PROTECTED]> > *Cc:* tutor@python.org > *Sent:

Re: [Tutor] List comprehensions

2008-04-09 Thread Dinesh B Vadhia
l Message ----- From: Kent Johnson To: Dinesh B Vadhia Cc: tutor@python.org Sent: Wednesday, April 09, 2008 12:40 PM Subject: Re: [Tutor] List comprehensions Dinesh B Vadhia wrote: > Here is a for loop operating on a list of string items: > > data = ["string 1", "str

Re: [Tutor] List comprehensions

2008-04-09 Thread linuxian iandsd
On Wed, Apr 9, 2008 at 7:44 PM, Jerry Hill <[EMAIL PROTECTED]> wrote: > On Wed, Apr 9, 2008 at 7:12 AM, Dinesh B Vadhia > <[EMAIL PROTECTED]> wrote: > > I want to replace the for loop with a List Comrehension (or whatever) to > > improve performance (as the data list will be >10,000]. At each sta

Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 7:12 AM, Dinesh B Vadhia <[EMAIL PROTECTED]> wrote: > I want to replace the for loop with a List Comrehension (or whatever) to > improve performance (as the data list will be >10,000]. At each stage of > the for loop I want to print the result ie. List comprehensions are fo

Re: [Tutor] List comprehensions

2008-04-09 Thread Kent Johnson
Dinesh B Vadhia wrote: > Here is a for loop operating on a list of string items: > > data = ["string 1", "string 2", "string 3", "string 4", "string 5", > "string 6", "string 7", "string 8", "string 9", "string 10", "string 11"] > > result = "" > for item in data: > result = item + "\n" >

[Tutor] List comprehensions

2008-04-09 Thread Dinesh B Vadhia
Here is a for loop operating on a list of string items: data = ["string 1", "string 2", "string 3", "string 4", "string 5", "string 6", "string 7", "string 8", "string 9", "string 10", "string 11"] result = "" for item in data: result = item + "\n" print result I want to replace the for loo

Re: [Tutor] List comprehensions

2005-03-24 Thread Karl Pflästerer
On 23 Mrz 2005, [EMAIL PROTECTED] wrote: > map is (probably) going to be removed in Python3000 :-( So it's > probably better to not get into the habit of using it. Au contraire. If enough people use it and are used using it the risk for `map' getting removed will be a lot lower. Furthermore t

Re: [Tutor] List comprehensions

2005-03-23 Thread Liam Clarke
Geez, that's pretty bad. I've got to stop playing with code when I'm tired. Thanks all. On Wed, 23 Mar 2005 20:24:32 -, Alan Gauld <[EMAIL PROTECTED]> wrote: > > Is there a way to apply multiple actions within one list > comprehension? > > Write a function? > > def foo(i): ># do stuf

Re: [Tutor] List comprehensions

2005-03-23 Thread Alan Gauld
> Is there a way to apply multiple actions within one list comprehension? Write a function? def foo(i): # do stuff to i return i [foo(i) for i in alist] Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tut

Re: [Tutor] List comprehensions

2005-03-23 Thread John Fouhy
Ryan Davis wrote: I think map is a little cleaner is some cases. Not sure if its more Pythonic, I'm still trying to figure out exactly what that means. map is (probably) going to be removed in Python3000 :-( So it's probably better to not get into the habit of using it. -- John. __

Re: [Tutor] List comprehensions

2005-03-23 Thread Kent Johnson
Liam Clarke wrote: Is there any guides to this (possibly obtuse) tool? http://docs.python.org/tut/node7.html#SECTION00714 http://www.amk.ca/python/2.0/index.html#SECTION00060 Kent ___ Tutor maillist - Tutor@python.org htt

Re: [Tutor] List comprehensions

2005-03-23 Thread Sean Perry
Liam Clarke wrote: Is there any guides to this (possibly obtuse) tool? Think of it this way. A list comprehension generates a new list. So, you should think about list comps whenever you have old_list -> new_list style behavior. There are two advantages to list comps over map: 1) a list comp can

RE: [Tutor] List comprehensions

2005-03-23 Thread Ryan Davis
lf Of Liam Clarke Sent: Wednesday, March 23, 2005 5:44 AM To: Tutor Tutor Subject: [Tutor] List comprehensions Hi, Is there a way to apply multiple actions within one list comprehension? i.e. instead of a = [] for i in x: i.pop(3) g = [ int(item) for item in i] a.append(g) Is there

Re: [Tutor] List comprehensions

2005-03-23 Thread Kent Johnson
Liam Clarke wrote: Hi, Is there a way to apply multiple actions within one list comprehension? i.e. instead of a = [] for i in x: i.pop(3) g = [ int(item) for item in i] a.append(g) You can nest list comps. Except for the pop, the above can be written a = [ [ int(item) for item in

[Tutor] List comprehensions

2005-03-23 Thread Liam Clarke
Hi, Is there a way to apply multiple actions within one list comprehension? i.e. instead of a = [] for i in x: i.pop(3) g = [ int(item) for item in i] a.append(g) Is there any guides to this (possibly obtuse) tool? Regards, Liam Clarke PS I can see how nested list comprehensio

Re: [Tutor] List comprehensions

2005-01-13 Thread Bob Gailer
At 07:03 AM 1/13/2005, Blake Winton wrote: What about "x === item" Perhaps we should propose an extension to Python: Assignment Statement: name = [[name] = ] ... expression-list. The absence of a name would create an anonymous variable just as lambda creates anonymous functions. Reminds me of a

Re: [Tutor] List comprehensions

2005-01-13 Thread Kent Johnson
Blake Winton wrote: Kent Johnson wrote: If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Does that mean that there are invalid expressions? I'd enjoy seeing an example. I suppose if it's an expressio

Re: [Tutor] List comprehensions

2005-01-13 Thread Blake Winton
Kent Johnson wrote: If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Does that mean that there are invalid expressions? I'd enjoy seeing an example. I suppose if it's an expression, it must be valid,

Re: [Tutor] List comprehensions

2005-01-13 Thread Max Noel
On Jan 13, 2005, at 04:13, Bob Gailer wrote: I like Kent's response. foobar(item)/0 is a "valid" expression. It fits the grammar of expressions. The fact that it raises an exception does not make it an invalid expression. Consider foobar(item)/xyz. It is valid. If xyz == 0 then it will also rai

Re: [Tutor] List comprehensions

2005-01-12 Thread Bob Gailer
At 07:05 PM 1/12/2005, Kent Johnson wrote: I suppose if it's an expression, it must be valid, eh? Otherwise it's something else. At 06:41 PM 1/12/2005, Max Noel wrote: On Jan 13, 2005, at 01:13, Bob Gailer wrote: At 04:48 PM 1/12/2005, Kent Johnson wrote: If you mean for j to be a list of foobar(i

Re: [Tutor] List comprehensions

2005-01-12 Thread Kent Johnson
I suppose if it's an expression, it must be valid, eh? Otherwise it's something else. Bob Gailer wrote: At 04:48 PM 1/12/2005, Kent Johnson wrote: If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Do

Re: [Tutor] List comprehensions

2005-01-12 Thread Max Noel
On Jan 13, 2005, at 01:13, Bob Gailer wrote: At 04:48 PM 1/12/2005, Kent Johnson wrote: If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Does that mean that there are invalid expressions? I'd enjoy se

Re: [Tutor] List comprehensions

2005-01-12 Thread Bob Gailer
At 04:48 PM 1/12/2005, Kent Johnson wrote: If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Does that mean that there are invalid expressions? I'd enjoy seeing an example. Bob Gailer mailto:[EMAIL PR

Re: [Tutor] List comprehensions

2005-01-12 Thread Liam Clarke
Aah, thank you both - I knew there was a way to do it. Regards, Liam Clarke PS John - another Kiwi, seems to be a lot of NZers involved in Python & Python projects. Is it the No. 8 wire freedom of Python? ; ) On Wed, 12 Jan 2005 18:48:39 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > If you

Re: [Tutor] List comprehensions

2005-01-12 Thread Kent Johnson
If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Kent Liam Clarke wrote: Hi, Am I able to achieve something like this - def foobar(); # stuff return x=[1,1000] for j=foobar(item) for item in x

Re: [Tutor] List comprehensions

2005-01-12 Thread jfouhy
Quoting Liam Clarke <[EMAIL PROTECTED]>: > As a comprehension, if you get what I mean... Can I build a for loop > with a function in for x in x part? > > Ack. Having difficulty summing it up properly. I'm not sure exactly what you mean, so my answer is "probably". >>> from math import sin >>> a

[Tutor] List comprehensions

2005-01-12 Thread Liam Clarke
Hi, Am I able to achieve something like this - def foobar(); # stuff return x=[1,1000] for j=foobar(item) for item in x: As a comprehension, if you get what I mean... Can I build a for loop with a function in for x in x part? Ack. Having difficulty summing it up properly. -- 'There