Re: Finding the name of a function while defining it

2012-12-29 Thread Jussi Piitulainen
Abhas Bhattacharya writes: [...] If i call one() and two() respectively, i would like to see one and two. I dont have much knowledge of lambda functions, neither am i going to use them, so that's something I cant answer. It's not about lambda. The following does not contain lambda. What

Re: how to detect the encoding used for a specific text data ?

2012-12-20 Thread Jussi Piitulainen
iMath writes: how to detect the encoding used for a specific text data ? The practical thing to do is to try an encoding and see whether you find the expected frequent letters of the relevant languages in the decoded text, or the most frequent words. This is likely to help you decide between

Re: how to detect the encoding used for a specific text data ?

2012-12-20 Thread Jussi Piitulainen
iMath writes: which package to use ? Read the text in as a bytes object (bytes), then it has a .decode method that you can experiment with. Strings (str) are Unicode and have an .encode method. These methods allow you to specify a desired encoding and and what to do when there are errors.

Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file

2012-12-19 Thread Jussi Piitulainen
dgcosgr...@gmail.com writes: Hi Iam just starting out with python...My code below changes the txt file into a list and add them to an empty dictionary and print how often the word occurs, but it only seems to recognise and print the last entry of the txt file. Any help would be great. tm

Re: Running a python script under Linux

2012-12-13 Thread Jussi Piitulainen
Steven D'Aprano writes: I have a Centos system which uses Python 2.4 as the system Python, so I set an alias for my personal use: [steve@ando ~]$ which python alias python='python2.7' /usr/local/bin/python2.7 When I call python some_script.py from the command line, it runs

Re: scope, function, mutable

2012-12-04 Thread Jussi Piitulainen
gusa...@gmail.com writes: What is the appropriate definition for the following behavior in Python 2.7 (see code below). Both functions have assignment in it (like x = ) so I assume, that x is a local variable in both functions. It's a local variable in both functions because it's a formal

Re: Understanding '?' in regular expressions

2012-11-16 Thread Jussi Piitulainen
krishna.k.kish...@gmail.com writes: Can someone explain the below behavior please? re1 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]?[ ]*?){1,3}') re.findall(re_obj,'1000,1020,1000') ['1000'] re.findall(re_obj,'1000,1020, 1000') ['1020', '1000'] However when I use [\,]?? instead

Re: Multi-dimensional list initialization

2012-11-07 Thread Jussi Piitulainen
Steven D'Aprano writes: On Wed, 07 Nov 2012 00:23:44 +, MRAB wrote: I prefer the term reference semantics. Oh good, because what the world needs is yet another name for the same behaviour. - call by sharing - call by object sharing - call by object reference - call by object -

Re: a prob.. error in prog ..dont knw how to correct

2012-10-21 Thread Jussi Piitulainen
inshu chauhan writes: I am new to python and have a little problem to solve .. i have an array with x, y, z co-ordinates in it as a tuple. I am trying to find the distance between each point and sorting the points according to the min distance.. i have tried a prog but m stuck bcoz of this

Re: How to use while within the command in -c option of python?

2012-10-13 Thread Jussi Piitulainen
Chris Angelico writes: Here's a side challenge. In any shell you like, start with this failing statement, and then fix it without retyping anything: sikorsky@sikorsky:~$ python -c a=1; if a: print(a) File string, line 1 a=1; if a: print(a) ^ SyntaxError: invalid syntax

Re: Unpaking Tuple

2012-10-09 Thread Jussi Piitulainen
Dave Angel writes: On 10/09/2012 02:07 AM, Bob Martin wrote: in 682592 20121008 232126 Prasad, Ramit wrote: [snip mess] How does one unpack this post? ;-) Since that's not the way it arrived here, i have to ask, how do you get these posts? Are you subscribed to individual messages by

Re: mangled messages (was: Unpaking Tuple)

2012-10-09 Thread Jussi Piitulainen
Tim Chase writes: On 10/09/12 02:22, Jussi Piitulainen wrote: in 682592 20121008 232126 Prasad, Ramit wrote: [snip mess] How does one unpack this post? ;-) Since that's not the way it arrived here, i have to ask, how do you get these posts? I see a carriage return rendered as ^M

Re: #!/usr/bin/env python vs. #!/usr/bin/python?

2012-09-28 Thread Jussi Piitulainen
Gilles writes: #!/usr/bin/env python #!/usr/bin/python What's the difference? Not much if your python is /usr/bin/python: env looks for python and finds the same executable. When python is not /usr/bin/python but something else that is still found by your system, /usr/bin/env still finds

Re: regular expression : the dollar sign ($) work with re.match() or re.search()

2012-09-26 Thread Jussi Piitulainen
iMath writes: I only know the dollar sign ($) will match a pattern from the end of a string, but which method does it work with, re.match() or re.search() It works with both. With re.match, the pattern has to match at the start of the string _and_ the $ has to match the end of the string (or

Re: regular expression : the dollar sign ($) work with re.match() or re.search()

2012-09-26 Thread Jussi Piitulainen
Alister writes: On Wed, 26 Sep 2012 10:48:00 +0300, Jussi Piitulainen wrote: iMath writes: I only know the dollar sign ($) will match a pattern from the end of a string, but which method does it work with, re.match() or re.search() It works with both. With re.match, the pattern

Re: Exact integer-valued floats

2012-09-21 Thread Jussi Piitulainen
Steven D'Aprano writes: Python floats can represent exact integer values (e.g. 42.0), but above a certain value (see below), not all integers can be represented. For example: py 1e16 == 1e16 + 1 # no such float as 10001.0 True py 1e16 + 3 == 1e16 + 4 # or

Re: A little morning puzzle

2012-09-19 Thread Jussi Piitulainen
Neal Becker writes: I have a list of dictionaries. They all have the same keys. I want to find the set of keys where all the dictionaries have the same values. Suggestions? Literally-ish: { key for key, val in ds[0].items() if all(val == d[key] for d in ds) } --

Re: Boolean function on variable-length lists

2012-09-12 Thread Jussi Piitulainen
Libra writes: Hello, I need to implement a function that returns 1 only if all the values in a list satisfy given constraints (at least one constraint for each element in the list), and zero otherwise. For example, I may have a list L = [1, 2, 3, 4] and the following constraints: L[0]

Re: Boolean function on variable-length lists

2012-09-12 Thread Jussi Piitulainen
Libra writes: On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote: So you would associate each constraint with an index. You could maintain a list of constraints and apply it to the values as follows: Yes, even though there could be more constraints for each

Re: Objects in Python

2012-08-23 Thread Jussi Piitulainen
Steven D'Aprano writes: On Wed, 22 Aug 2012 23:49:17 -0500, Evan Driscoll wrote: On 8/22/2012 18:58, Ben Finney wrote: You haven't discovered anything about types; what you have discovered is that Python name bindings are not variables. In fact, Python doesn't have variables – not as

Re: Objects in Python

2012-08-22 Thread Jussi Piitulainen
shaun writes: I'm having an issue its my first time using python and i set up a class one of the methods is supposed to return a string but instead returns: bound method Param.returnString of Param.Param instance at 0x00C 389E0 Im very new to python and the object orientated feature

Re: Regex Question

2012-08-18 Thread Jussi Piitulainen
Frank Koshti writes: not always placed in HTML, and even in HTML, they may appear in strange places, such as h1 $foo(x=3)Hello/h1. My specific issue is I need to match, process and replace $foo(x=3), knowing that (x=3) is optional, and the token might appear simply as $foo. To do this, I

Re: [newbie] problem with data (different behaviour between batch and interactive use)

2012-06-27 Thread Jussi Piitulainen
Jean Dupont writes: If I start python interactively I can separate the fields as follows: measurement=+3.874693E01,+9.999889E03,+9.91E+37,+1.876[...] print measurement[0] 0.3874693 [...] The script does this: measurement=serkeith.readline().replace('\x11','').replace([...] print

Re: Py3.3 unicode literal and input()

2012-06-18 Thread Jussi Piitulainen
jmfauth writes: Thinks are very clear to me. I wrote enough interactive interpreters with all available toolkits for Windows r = input() u'a Traceback (most recent call last): File stdin, line 1, in module SyntaxError: u'a Er, no, not really :-) --

Re: Py3.3 unicode literal and input()

2012-06-18 Thread Jussi Piitulainen
Andrew Berg writes: On 6/18/2012 11:32 AM, Jussi Piitulainen wrote: jmfauth writes: Thinks are very clear to me. I wrote enough interactive interpreters with all available toolkits for Windows r = input() u'a Traceback (most recent call last): File stdin, line 1, in module

Re: Passing ints to a function

2012-06-09 Thread Jussi Piitulainen
stayvoid writes: You want to unpack the list: function(*a)  # like function(a[0], a[1], a[2], ...) Awesome! I forgot about this. Here's something you could have thought of for yourself even when you didn't remember that Python does have special built-in support for applying a function

Re: Dynamic comparison operators

2012-05-24 Thread Jussi Piitulainen
Alain Ketterlin writes: mlangenho...@gmail.com writes: I would like to pass something like this into a function test(val1,val2,'=') and it should come back with True or False. def test(x,y,c): return c(x,y) Call with: test(v1,v2, lambda x,y:x=y ). A bit noisy imho. Re noisy:

Re: numpy (matrix solver) - python vs. matlab

2012-05-02 Thread Jussi Piitulainen
someone writes: except it would be nice to learn some things for future use (for instance understanding SVD more - perhaps someone geometrically can explain SVD, that'll be really nice, I hope)... The Wikipedia article looks promising to me:

Re: Trouble splitting strings with consecutive delimiters

2012-05-01 Thread Jussi Piitulainen
deuteros writes: I'm using regular expressions to split a string using multiple delimiters. But if two or more of my delimiters occur next to each other in the string, it puts an empty string in the resulting list. For example: re.split(':|;|px', width:150px;height:50px;float:right)

Re: finding a regular expression in a file

2012-04-24 Thread Jussi Piitulainen
S.B writes: Hello friends. Newb question here. I'm trying to find an efficient way to grep a file with python. The problem is that all the solutions I find on the web read a line at a time from the file with a for line in loop and check each line for the RE instead of sweeping through the

Re: can I overload operators like =, - or something like that?

2012-04-20 Thread Jussi Piitulainen
Kiuhnm writes: On 4/20/2012 17:50, Nobody wrote: On Thu, 19 Apr 2012 12:28:50 -0700, dmitrey wrote: can I somehow overload operators like =, - or something like that? (I'm searching for appropriate overload for logical implication if a then b) You cannot create new operators, but

Re: Regular expressions, help?

2012-04-19 Thread Jussi Piitulainen
Sania writes: So I am trying to get the number of casualties in a text. After 'death toll' in the text the number I need is presented as you can see from the variable called text. Here is my code I'm pretty sure my regex is correct, I think it's the group part that's the problem. I am using

Re: Regular expressions, help?

2012-04-19 Thread Jussi Piitulainen
Sania writes: On Apr 19, 2:48 am, Jussi Piitulainen jpiit...@ling.helsinki.fi wrote: Sania writes: So I am trying to get the number of casualties in a text. After 'death toll' in the text the number I need is presented as you can see from the variable called text. Here is my code

Re: string interpolation for python

2012-04-02 Thread Jussi Piitulainen
Yingjie Lan writes: Clearly dynamic strings are much more powerful, allowing arbitrary expressions inside. It is also more terse and readable, since we need no dictionary. ... On the implementation, I would suppose new  syntax is needed (though very small). I don't think you need any new

Re: convert string to bytes without changing data (encoding)

2012-03-28 Thread Jussi Piitulainen
Peter Daum writes: ... I was under the illusion, that python (like e.g. perl) stored strings internally in utf-8. In this case the conversion would simple mean to re-label the data. Unfortunately, as I meanwhile found out, this is not the case (nor the apple encoding ;-), so it would indeed

Re: Documentation, assignment in expression.

2012-03-26 Thread Jussi Piitulainen
Kiuhnm writes: On 3/26/2012 10:52, Devin Jeanpierre wrote: On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm kiuhnm03.4t.yahoo...@mail.python.org wrote: On 3/25/2012 15:48, Tim Chase wrote: The old curmudgeon in me likes the Pascal method of using = for equality-testing, and := for assignment

Re: Python math is off by .000000000000045

2012-02-22 Thread Jussi Piitulainen
Alec Taylor writes: Simple mathematical problem, + and - only: 1800.00-1041.00-555.74+530.74-794.95 -60.9500045 That's wrong. Not by much. I'm not an expert, but my guess is that the exact value is not representable in binary floating point, which most programming languages use

Re: Postpone evaluation of argument

2012-02-11 Thread Jussi Piitulainen
Righard van Roy writes: Hello, I want to add an item to a list, except if the evaluation of that item results in an exception. I could do that like this: def r(x): if x 3: raise(ValueError) try: list.append(r(1)) except: pass try: list.append(r(5))

Re: Explanation about for

2012-01-10 Thread Jussi Piitulainen
Νικόλαος Κούρας writes: So that means that for host, hits, agent, date in dataset: is: for host, hits, agent, date in (foo,7,IE6,1/1/11) and then: for host, hits, agent, date in (bar,42,Firefox,2/2/10) and then: for host, hits, agent, date in (baz,4,Chrome,3/3/09) So

Re: % is not an operator [was Re: Verbose and flexible args and kwargs syntax]

2011-12-15 Thread Jussi Piitulainen
rusi writes: On Dec 15, 3:58 pm, Chris Angelico wrote: On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote:  42 = 2 mod 5  2 = 42 mod 5 It might make more sense to programmers if you think of it as written: 42 = 2, mod 5 2 = 42, mod 5 ChrisA For the record I should say

Re: % is not an operator [was Re: Verbose and flexible args and kwargs syntax]

2011-12-14 Thread Jussi Piitulainen
Steven D'Aprano writes: On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: [quoting Jussi Piitulainen jpiit...@ling.helsinki.fi] They recognize modular arithmetic but for some reason insist that there is no such _binary operation_. But as I said, I don't understand their concern. (Except

Re: Verbose and flexible args and kwargs syntax

2011-12-14 Thread Jussi Piitulainen
Nick Dokos writes: Jussi Piitulainen wrote: They recognize modular arithmetic but for some reason insist that there is no such _binary operation_. But as I said, I don't understand their concern. (Except the related concern about some programming languages, not Python, where the remainder

Re: % is not an operator [was Re: Verbose and flexible args and kwargs syntax]

2011-12-14 Thread Jussi Piitulainen
Eelco writes: On 14 dec, 09:56, Jussi Piitulainen wrote: But I think the argument there are several such functions, therefore, _in mathematics_, there is no such function is its own caricature. Indeed. Obtaining a well defined function is just a matter of picking a convention

Re: % is not an operator [was Re: Verbose and flexible args and kwargs syntax]

2011-12-14 Thread Jussi Piitulainen
rusi writes: On Dec 14, 1:56 pm, Jussi Piitulainen jpiit...@ling.helsinki.fi wrote: Is someone saying that _division_ is not defined because -42 div -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The two operations, div and rem, need to be defined together

Re: % is not an operator [was Re: Verbose and flexible args and kwargs syntax]

2011-12-14 Thread Jussi Piitulainen
Steven D'Aprano writes: On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: I'm not misunderstanding any argument. There was no argument. There was a blanket pronouncement that _in mathematics_ mod is not a binary operator. I should learn to challenge such pronouncements and ask

Re: Verbose and flexible args and kwargs syntax

2011-12-12 Thread Jussi Piitulainen
Eelco Hoogendoorn writes: As for %; it is entirely unclear to me why that obscure operation ever got its own one-character symbol. Ill take 'mod', or even better, 'modulus' any day of the week. The modulus is not the result but one of the arguments: when numbers x and y are congruent modulo n

Re: Verbose and flexible args and kwargs syntax

2011-12-12 Thread Jussi Piitulainen
Eelco writes: The modulus is not the result but one of the arguments: when numbers x and y are congruent modulo n (stated in terms of the modulo operation: x mod n = y mod n), the modulus is n. A word for x mod n is remainder. I agree about the obscurity of using the percent sign as the

Re: Verbose and flexible args and kwargs syntax

2011-12-12 Thread Jussi Piitulainen
Terry Reedy writes: On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: Past experience in mathematics newsgroups tells me that some mathematicians do not accept the existence of any remainder operator at all. Even though they carry hour/minute/second remindering devices on their bodies

Re: Verbose and flexible args and kwargs syntax

2011-12-12 Thread Jussi Piitulainen
Eelco writes: They recognize modular arithmetic but for some reason insist that there is no such _binary operation_. But as I said, I don't understand their concern. (Except the related concern about some programming languages, not Python, where the remainder does not behave well with

Re: Dynamic variable creation from string

2011-12-08 Thread Jussi Piitulainen
Terry Reedy writes: On 12/7/2011 7:03 PM, Steven D'Aprano wrote: On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: Is there a way to create three variables dynamically inside Sum in order to re write the function like this? I should have mentioned in my earlier response that 'variable'

Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Jussi Piitulainen
Roy Smith writes: Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. try: songs = [Song(id) for id in song_ids] except Song.DoesNotExist: print unknown song id (%d) % id Is id guaranteed to be in scope in the print

Re: Usefulness of the not in operator

2011-10-13 Thread Jussi Piitulainen
Chris Angelico writes: On Sun, Oct 9, 2011 at 12:07 AM, Jussi Piitulainen wrote: But both negations can be avoided by modus tollens. If you are able to start the car, the key is in the ignition. But this translation implies looking at the result and ascertaining the state, which is less

Re: Usefulness of the not in operator

2011-10-08 Thread Jussi Piitulainen
Mel writes: Steven D'Aprano wrote: candide wrote: So what is the usefulness of the not in operator ? Recall what Zen of Python tells There should be one-- and preferably only one --obvious way to do it. And not in is the obvious way to do it. If the key is not in the

Re: Negativ nearest interger?

2011-09-22 Thread Jussi Piitulainen
joni writes: Have a simple question in the Integer calculator in Python 2.65 and also 2.7.. The consol showing: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) ... -7/3 -3 -3 are more wrong than -2. Negativ number seems not to round to nearest interger, but the integer UNDER the

Re: How do I automate the removal of all non-ascii characters from my code?

2011-09-13 Thread Jussi Piitulainen
Alec Taylor writes: Hmm, nothing mentioned so far works for me... Here's a very small test case: python -u Convert to Creole.py File Convert to Creole.py, line 1 SyntaxError: Non-ASCII character '\xe2' in file Convert to Creole.py on line 1, but no encoding declared; see

Re: Something is rotten in Denmark...

2011-06-05 Thread Jussi Piitulainen
rusi writes: On Jun 3, 11:17 am, Jussi Piitulainen wrote: rusi writes: So I tried: Recast the comprehension as a map Rewrite the map into a fmap (functionalmap) to create new bindings def fmap(f,lst):     if not lst: return []     return [f(lst[0])] + fmap(f, lst[1

Re: Something is rotten in Denmark...

2011-06-05 Thread Jussi Piitulainen
rusi writes: On Jun 5, 5:03 pm, Jussi Piitulainen wrote: rusi writes: On Jun 3, 11:17 am, Jussi Piitulainen wrote: rusi writes: So I tried: Recast the comprehension as a map Rewrite the map into a fmap (functionalmap) to create new bindings def fmap(f,lst

Re: Something is rotten in Denmark...

2011-06-03 Thread Jussi Piitulainen
rusi writes: So I tried: Recast the comprehension as a map Rewrite the map into a fmap (functionalmap) to create new bindings def fmap(f,lst): if not lst: return [] return [f(lst[0])] + fmap(f, lst[1:]) Still the same effects. Obviously I am changing it at the wrong place...

Re: Something is rotten in Denmark...

2011-06-03 Thread Jussi Piitulainen
Alain Ketterlin writes: Gregory Ewing writes: Alain Ketterlin wrote: But going against generally accepted semantics should at least be clearly indicated. Lambda is one of the oldest computing abstraction, and they are at the core of any functional programming language. Yes, and

Re: Something is rotten in Denmark...

2011-06-02 Thread Jussi Piitulainen
Alain Ketterlin writes: Steven D'Aprano writes: I agree it's not intuitive. But where does it say that programming language semantics must always be intuitive? Nowhere. But going against generally accepted semantics should at least be clearly indicated. Lambda is one of the oldest

Re: Comparison operators in Python

2011-06-01 Thread Jussi Piitulainen
Anirudh Sivaraman writes: I am a relative new comer to Python. I see that typing is strongly enforced in the sense you can't concatenate or add a string and an integer. However comparison between a string and an integer seems to be permitted. Is there any rationale behind this ? In Python 3

Re: Something is rotten in Denmark...

2011-05-31 Thread Jussi Piitulainen
harrismh777 writes: fs=[] fs = [(lambda n: i + n) for i in range(10)] [fs[i](1) for i in range(10)] [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] === not good ( that was a big surprise! . . . ) ( let's try it another way . . . ) The ten functions share the same i. The

Re: Something is rotten in Denmark...

2011-05-31 Thread Jussi Piitulainen
Thomas Rachel writes: Am 31.05.2011 12:08 schrieb Jussi Piitulainen: The same sharing-an-i thing happens here: fs = [] for i in range(4): ...fs.append(lambda n : i + n) ... fs[0](0) 3 And the different private-j thing happens here: gs = [] for i in range(4

Re: scope of function parameters

2011-05-30 Thread Jussi Piitulainen
Laurent Claessens writes: Le 30/05/2011 11:02, Terry Reedy a écrit : On 5/30/2011 3:38 AM, Laurent wrote: Cool. I was thinking that 5 was the name, but 5.__add__(6) File stdin, line 1 5.__add__(6) Try 5 .__add__(6) What is the rationale behind the fact to add a space

Re: Python 3.2 bug? Reading the last line of a file

2011-05-25 Thread Jussi Piitulainen
tkp...@hotmail.com writes: Looking through the docs did not clarify my understanding of the issue. Why can I not split on '\t' when reading in binary mode? You can split on b'\t' to get a list of byteses, which you can then decode if you want them as strings. You can decode the bytes to get a

Re: Faster Recursive Fibonacci Numbers

2011-05-17 Thread Jussi Piitulainen
geremy condra writes: or O(1): φ = (1 + sqrt(5)) / 2 def fib(n): numerator = (φ**n) - (1 - φ)**n denominator = sqrt(5) return round(numerator/denominator) Testing indicates that it's faster somewhere around 7 or so. And increasingly inaccurate from 71 on. --

Re: checking if a list is empty

2011-05-08 Thread Jussi Piitulainen
Steven D'Aprano writes: Lisp uses the empty list and the special atom NIL as false values, any other s-expression is true. Scheme is different: it defines a special false atom, and empty lists are considered true. In Ruby, I'll inject a pedantic note: there is only one false value in both

Re: Feature suggestion -- return if true

2011-04-19 Thread Jussi Piitulainen
Gregory Ewing writes: Chris Angelico wrote: Question: How many factorial functions are implemented because a program needs to know what n! is, and how many are implemented to demonstrate recursion (or to demonstrate the difference between iteration and recursion)? :) (I can't get to

Re: os.path.walk() to get full path of all files

2011-03-17 Thread Jussi Piitulainen
Laurent Claessens writes: file_list = [] for root, _, filenames in os.walk(root_path): for filename in filenames: file_list.append(os.path.join(root, filename)) What does the notation _ stands for ? Is it a sort of /dev/null ? x, _, y = 1, hukairs, 3 x, y

Re: How on Factorial

2010-10-27 Thread Jussi Piitulainen
Geobird writes: @ Ulrich : Tx @ Rebert : Appreciate your interpretation. It made me think about ternary operation . Say (a b) and x or y Are all ternary operations prone to ...( in your words ) It exploits short-circuit evaluation

Re: [OFF] sed equivalent of something easy in python

2010-10-27 Thread Jussi Piitulainen
Daniel Fetchinson writes: This question is really about sed not python, hence it's totally off. But since lots of unix heads are frequenting this list I thought I'd try my luck nevertheless. ... using python. The pattern is that the first line is deleted, then 2 lines are kept, 3 lines are

Re: [OFF] sed equivalent of something easy in python

2010-10-27 Thread Jussi Piitulainen
Jussi Piitulainen writes: Daniel Fetchinson writes: This question is really about sed not python, hence it's totally off. But since lots of unix heads are frequenting this list I thought I'd try my luck nevertheless. ... using python. The pattern is that the first line is deleted

Re: Has Next in Python Iterators

2010-10-25 Thread Jussi Piitulainen
Kelson Zawack writes: The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] where you want to loop until you see not a 2 and then you want to loop until you see not a 3. In this situation you cannot use a for loop as follows: ... because it will eat the 1 and not allow the second

Re: 30 is True

2010-09-15 Thread Jussi Piitulainen
Yingjie Lan writes: I am not sure how to interprete this, in the interactive mode: 30 is True False (30) is True True 3 (0 is True) True Why did I get the first 'False'? I'm a little confused. It is interpreted as equivalent to this: 3 0 and 0 is True False From the

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns the swapcased string. Could be, but the main use case seems to be for palindrome

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Richard Arts writes: On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s):    def reversed(s):        return s[::-1]    return s == reversed(s) That seems like a bit of overkill... Why would you want to define

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Steven D'Aprano writes: On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Arnaud Delobelle writes: Also, I an not aware that it is customary in python to name predicate functions with a p suffix - Python is not Lisp! Just to clarify my position: I did not mean to imply that names like palindromep might be customary in Python - clearly they are not - and I am quite

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Paul Rubin writes: Ian writes: On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Ian writes: If you want to or must do it recursively. (Shown in pseudo code to make the logic clearer) def isPalindrome(pal) ''' test pal (a list) is a palindrome ''' if length of pal = 1 return True # all one letter strings are palindromes. if first equals last

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Dave Angel writes: Jussi Piitulainen wrote: Ian writes: Of course, the simpler way is to use the definition of a Palindrome as the same backwards and forwards. def isPalindrome(pal) return pal == pal.reverse Agreed. But is there any nicer way to spell .reverse than [::-1] in Python

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
MRAB writes: On 27/08/2010 20:43, Jussi Piitulainen wrote: Dave Angel writes: Jussi Piitulainen wrote: Agreed. But is there any nicer way to spell .reverse than [::-1] in Python? There is .swapcase() but no .reverse(), right? There can't be a .reverse() method on string, because it's

Re: split string into multi-character letters

2010-08-25 Thread Jussi Piitulainen
Jed writes: alphabet = ['a','b','c','ch','d','u','r','rr','o'] #this would include the whole alphabet but I shortened it here theword = 'churro' I would like to split the string 'churro' into a list containing: 'ch','u','rr','o' All non-overlapping matches, each as long as can be, and

Re: pythonize this!

2010-06-16 Thread Jussi Piitulainen
Lie Ryan writes: On 06/15/10 21:49, superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) [...] Probably bending the rules a little bit: sum(x**2 -

Re: pythonize this!

2010-06-15 Thread Jussi Piitulainen
superpollo writes: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) my solution: s = 0 for i in range(1, 2011): ... s += i**2 ... if not (i+1)%5: ...

Re: long int computations

2010-05-03 Thread Jussi Piitulainen
Peter Otten writes: Victor Eijkhout wrote: I have two long ints, both too long to convert to float, but their ratio is something reasonable. How can I compute that? The obvious (1.*x)/y does not work. import fractions x = 12345 * 10**1000 y = 765 * 10**1000 float(x) Traceback

Re: import antigravity

2010-03-18 Thread Jussi Piitulainen
Alf P. Steinbach writes: The point is, if he's upset about Chris quoting that, then he's probably unaware that he's posting it in plaintext himself. The complaint was not about quoting but about using in public. Chris sent his piece to three addresses. From his headers, redacted:

Re: Partly erratic wrong behaviour, Python 3, lxml

2010-03-05 Thread Jussi Piitulainen
Stefan Behnel writes: Jussi Piitulainen, 04.03.2010 22:40: Stefan Behnel writes: Jussi Piitulainen, 04.03.2010 11:46: I am observing weird semi-erratic behaviour that involves Python 3 and lxml, is extremely sensitive to changes in the input data, and only occurs when I name a partial

Partly erratic wrong behaviour, Python 3, lxml

2010-03-04 Thread Jussi Piitulainen
Dear group, I am observing weird semi-erratic behaviour that involves Python 3 and lxml, is extremely sensitive to changes in the input data, and only occurs when I name a partial result. I would like some help with this, please. (Python 3.1.1; GNU/Linux; how do I find lxml version?) The test

Re: Partly erratic wrong behaviour, Python 3, lxml

2010-03-04 Thread Jussi Piitulainen
Stefan Behnel writes: Jussi Piitulainen, 04.03.2010 11:46: I am observing weird semi-erratic behaviour that involves Python 3 and lxml, is extremely sensitive to changes in the input data, and only occurs when I name a partial result. I would like some help with this, please. (Python

Re: Partly erratic wrong behaviour, Python 3, lxml

2010-03-04 Thread Jussi Piitulainen
This is the full data file on which my regress/Tribug exhibits the behaviour that I find incomprehensible, described in the first post in this thread. The comment in the beginning of the file below was written before I commented out some records in the data, so the actual numbers now are not ten

Re: Bizarre arithmetic results

2010-02-12 Thread Jussi Piitulainen
Terry Reedy writes: On 2/11/2010 11:23 AM, Jussi Piitulainen wrote: Robert Kern writes: On 2010-02-11 06:31 AM, Shashwat Anand wrote: There is a little issue here that ' -.1 ** .1' should give you error message. That is it. No, fractional powers of negative numbers are perfectly

Re: Bizarre arithmetic results

2010-02-12 Thread Jussi Piitulainen
Terry Reedy writes: On 2/12/2010 4:40 AM, Jussi Piitulainen wrote: Terry Reedy writes: On 2/11/2010 11:23 AM, Jussi Piitulainen wrote: Robert Kern writes: On 2010-02-11 06:31 AM, Shashwat Anand wrote: There is a little issue here that ' -.1 ** .1' should give you error message

Re: Bizarre arithmetic results

2010-02-11 Thread Jussi Piitulainen
Terrence Cole writes: Can someone explain to me what python is doing here? Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47) [GCC 4.3.4] on linux2 Type help, copyright, credits or license for more information. -0.1 ** 0.1 -0.7943282347242815 a = -0.1; b = 0.1 a ** b

Re: Bizarre arithmetic results

2010-02-11 Thread Jussi Piitulainen
Robert Kern writes: On 2010-02-11 06:31 AM, Shashwat Anand wrote: There is a little issue here that ' -.1 ** .1' should give you error message. That is it. No, fractional powers of negative numbers are perfectly valid mathematically. The result is a complex number. In Python 3 (what the

Re: problem with lambda / closures

2009-12-01 Thread Jussi Piitulainen
Terry Reedy writes: definitions. Lambda expressions create functions just like def statements and are not closures and do not create closure unless nested within another function definition. Thinking otherwise is Seems quite closed in the top level environment to me: Python 2.3.4 (#1, Jul

Re: problem with lambda / closures

2009-11-30 Thread Jussi Piitulainen
Benjamin Kaplan writes: On Monday, November 30, 2009, Louis Steinberg wrote: I have run into what seems to be a major bug, but given my short exposure to Python is probably just a feature: running Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on

Re: installing lxml ?

2009-11-11 Thread Jussi Piitulainen
7stud writes: I'm trying to install lxml, but I can't figure out the installation instructions. Here: ... My os is mac os x 10.4.11. But this: STATIC_DEPS=true easy_install lxml is not a valid command: $ sudo STATIC_DEPS=true easy_install lxml Password: sudo: STATIC_DEPS=true:

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Jussi Piitulainen
Miles Kaufmann writes: [...] I'm curious what algorithm calls for random numbers on a closed interval. The Box-Muller transform, polar form. At least Wikipedia says so. -- http://mail.python.org/mailman/listinfo/python-list

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Jussi Piitulainen
Esmail writes: random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the original design specifications as possible though I

<    1   2   3   4   5   6   7   >