[Tutor] Selecting from list
hi list: In the following list, is there a simply way to find element less than 200 sandwiched between two numbers greater than 1000. a = [3389, 178, 2674, 2586, 13731, 3189, 785, 1038, 25956, 33551] in a, 178 is between 3389 and 2674. How this particular list can be selected for further processing. (sorry this is not homework question. I want to avoid looping, because I have 300K lines to parse through) Thanks Hs.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Selecting from list
On 18/07/13 18:27, Hs Hs wrote: hi list: Hi, please don't use an old thread to start a new one. This message appeared under a thread dated back in January. I nearly gave up looking for the unread message that my reader said was there. Start a new thread - you'll get better responses if you do. In the following list, is there a simply way to find element less than 200 sandwiched between two numbers greater than 1000. a = [3389, 178, 2674, 2586, 13731, 3189, 785, 1038, 25956, 33551] You example is ambiguous. What should happen in this example: a = [3389, 178, 66, 2674, 2586, 13731, 3189, 785, 1038, 25956, 33551] Should it list 178 and 66, one of them(which?) or neither? Or what about: a = [389, 178, 2674, 2586, 13731, 3189, 785, 1038, 25956, 33551] Should 178 now be ignored because 389 is lower than 1000? (sorry this is not homework question. I want to avoid looping, because I have 300K lines to parse through) If you have to parse 300K lines you will need a loop. It may not be explicit but it will be there. Get used to that idea and worry about how you process the data. And maybe making sure you only loop over everything once! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Selecting from list
On 19/07/13 10:18, Jim Mooney wrote: On 18 July 2013 10:27, Hs Hs ilhs...@yahoo.com wrote: [...] (sorry this is not homework question. I want to avoid looping, because I have 300K lines to parse through) Thanks Hs. Not sure what you want to do. If you only want to fulfill the test once, here is a way without a loop, using a list comprehension. A list comprehension *is* a loop. It even includes a for inside it. I really don't understand why people so often say things like I have a bunch of stuff to do repeatedly, but I want to do it without a loop. To put it another way, I want to repeat something without repeating it. WTF??? The only way to avoid a loop *somewhere* is to have a parallel-processing computer with at least as many parallel processes as you have things to repeat. So if Hs has 300K lines to process, he would need 300K processors, one per line. Since that's impractical unless you're Google or the NSA[1] you're going to need a loop, the only question is whether it is an *explicit* loop or an *implicit* loop. For example, a standard for-loop: for line in many_lines: process(line) or a list-comprehension: [process(line) for line in many_lines] are explicit loops. The map built-in is implicit: map(process, many_lines) So are many of numpy's array functions. But regardless of whether *you* write the loop, or Python does it for you, there is still a loop. [1] Hi guys! -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor