Re: Reading properties file in Python, except using ConfigParser()
On Friday, October 5, 2012 5:03:01 PM UTC+5:30, Günther Dietrich wrote: > [email protected] wrote: > > > > >How to read properties file in Python? I found ConfigParser() but it has a > > >'section' limitation, so looking for other alternatives. > > > > Have a look at PyYAML. > > > > > > > > Best regards, > > > > Günther Thanks for responding guys. I got the solution from here - http://www.daniweb.com/software-development/python/threads/435902/reading-properties-file-in-python-except-using-configparser# Posting it here for others' benefit.. -- http://mail.python.org/mailman/listinfo/python-list
To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
To get the accurate value of 1 - 0.999 ,how to implement the python algorithm ? BTW ,Windows’s calculator get the accurate value ,anyone who knows how to implement it ? -- http://mail.python.org/mailman/listinfo/python-list
[ANN] pylint 0.26 / logilab-astng 0.24.1
Hi all, I'm very pleased to announce new releases of Pylint and underlying ASTNG library, respectivly 0.26 and 0.24.1. The great news is that both bring a lot of new features and some bug fixes, mostly provided by the community effort. We're still trying to make it easier to contribute on our free software project at Logilab, so I hope this will continue and we'll get even more contritions in a near future, and an even smarter/faster/whatever pylint! So many thanks to all those who made that release, and enjoy! -- Sylvain Thénault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42) Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations Développement logiciel sur mesure: http://www.logilab.fr/services CubicWeb, the semantic web framework:http://www.cubicweb.org -- http://mail.python.org/mailman/listinfo/python-list
Re: How to control the internet explorer?
Thank you On Mon, Oct 8, 2012 at 11:37 AM, alex23 wrote: > On Oct 8, 1:03 pm, yujian wrote: > > I want to save all the URLs in current opened windows, and then close > > all the windows. > > Try mechanize or Selenium. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On 10/08/2012 10:07 AM, iMath wrote:
> To get the accurate value of 1 - 0.999 ,how to implement the
> python algorithm ?
> BTW ,Windows’s calculator get the accurate value ,anyone who knows how to
> implement it ?
Windows calculator is an application, not a programming language. Like
all applications, it has to deal with the finite accuracy of the
underlying processor and language, and choose an algorithm that will
please its users.
The Pentium chip (and its equivalents from AMD), used by Windows
machines and most others, has about 18 digits of accuracy in its binary
floating point math. However, being binary, the data has to be
converted from decimal to binary (when the user types it in) and binary
to decimal (when displaying it). Either of those conversions may have
quantization errors, and it's up to the program to deal with those or
other inaccuracies.
If you subtract two values, either of which may have quantization
errors, and they are quite close, then the apparent error is
magnified. Out of your 18 digits internal accuracy, you now have only
about 2.
Therefore many programs more concerned with apparent accuracy will
ignore the binary floating point, and do their work in decimal. That
doesn't eliminate calculation errors, but only quantization errors.
That makes the user think he is getting more accuracy than he really is.
Since that seems to be your goal, I suggest you look into the Decimal
class, locating in the stdlib decimal.
import decimal
a = decimal.Decimal(4.3)
print(a)
5.0996447286321199499070644378662109375
Note that you still seem to have some "error" since the value 4.3 is a binary
float, and has already been quantized. If you want to avoid the binary stuff
entirely, try going directly from string to Decimal.
b = decimal.Decimal("5.1")
print(b)
5.1
Back to your original contrived example,
c = decimal.Decimal("1.0")
d = decimal.Decimal("0.999")
print(c-d)
1E-15
The Decimal class has the disadvantage that it's tons slower on any modern
machine I know of, but the advantage that you can specify how much precision
you need it to use. It doesn't eliminate errors at all, just one class of them.
e = decimal.Decimal("3.0")
print(c/e)
0.
That of course is the wrong answer. The "right" answer would never stop
printing. We still have a finite number of digits.
print(c/e*e)
0.
"Fixing" this is subject for another lesson, someday.
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list
Re: Question on Python Split
On Monday, October 8, 2012 1:00:52 AM UTC+5:30, [email protected] wrote: > Dear Group, > > > > Suppose I have a string as, > > > > "Project Gutenberg has 36000 free ebooks for Kindle Android iPad iPhone." > > > > I am terming it as, > > > > str1= "Project Gutenberg has 36000 free ebooks for Kindle Android iPad > iPhone." > > > > I am working now with a split function, > > > > str_words=str1.split() > > so, I would get the result as, > > ['Project', 'Gutenberg', 'has', '36000', 'free', 'ebooks', 'for', 'Kindle', > 'Android', 'iPad', 'iPhone.'] > > > > But I am looking for, > > > > ['Project Gutenberg', 'has 36000', 'free ebooks', 'for Kindle', 'Android > iPad', 'iPhone'] > > > > This can be done if we assign the string as, > > > > str1= "Project Gutenberg, has 36000, free ebooks, for Kindle, Android iPad, > iPhone," > > > > and then assign the split statement as, > > > > str1_word=str1.split(",") > > > > would produce, > > > > ['Project Gutenberg', ' has 36000', ' free ebooks', ' for Kindle', ' Android > iPad', ' iPhone', ''] > > > > My objective generally is achieved, but I want to convert each group here in > tuple so that it can be embedded, like, > > > > [(Project Gutenberg), (has 36000), (free ebooks), (for Kindle), ( Android > iPad), (iPhone), ''] > > > > as I see if I assign it as > > > > for i in str1_word: > >print i > >ti=tuple(i) > >print ti > > > > I am not getting the desired result. > > > > If I work again from tuple point, I get it as, > > >>> tup1=('Project Gutenberg') > > >>> tup2=('has 36000') > > >>> tup3=('free ebooks') > > >>> tup4=('for Kindle') > > >>> tup5=('Android iPad') > > >>> tup6=tup1+tup2+tup3+tup4+tup5 > > >>> print tup6 > > Project Gutenberghas 36000free ebooksfor KindleAndroid iPad > > > > Then how may I achieve it? If any one of the learned members can kindly guide > me. > > Thanks in Advance, > > Regards, > > Subhabrata. > > > > NB: Apology for some minor errors. Thank you for nice answer. Your codes and discussions always inspire me. Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On Tue, Oct 9, 2012 at 1:48 AM, Dave Angel wrote: > import decimal > a = decimal.Decimal(4.3) > print(a) > > 5.0996447286321199499070644378662109375 Ah, the delights of copy-paste :) > The Decimal class has the disadvantage that it's tons slower on any modern > machine I know of... Isn't it true, though, that Python 3.3 has a completely new implementation of decimal that largely removes this disadvantage? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On 10/08/2012 11:00 AM, Chris Angelico wrote: > On Tue, Oct 9, 2012 at 1:48 AM, Dave Angel wrote: >> import decimal >> a = decimal.Decimal(4.3) >> print(a) >> >> 5.0996447286321199499070644378662109375 > Ah, the delights of copy-paste :) > >> The Decimal class has the disadvantage that it's tons slower on any modern >> machine I know of... > Isn't it true, though, that Python 3.3 has a completely new > implementation of decimal that largely removes this disadvantage? > > ChrisA I wouldn't know, I'm on 3.2. However, I sincerely doubt if it's within a factor of 100 of the speed of the binary float, at least on pentium-class machines that do binary float in microcode. A dozen years or so ago, when the IEEE floating point standard was still being formed, I tried to argue the committee into including decimal in the standard (which they did much later). Had it been in the standard then, we MIGHT have had decimal fp on chip as well as binary. Then again, the standard was roughly based on the already-existing Intel 8087, so maybe it was just hopeless. I guess it's possible that for some operations, the cost of the byte-code interpreter and function lookup, etc. might reduce the apparent penalty. Has anybody done any timings? -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On Tue, Oct 9, 2012 at 2:13 AM, Dave Angel wrote: > On 10/08/2012 11:00 AM, Chris Angelico wrote: >> On Tue, Oct 9, 2012 at 1:48 AM, Dave Angel wrote: >>> The Decimal class has the disadvantage that it's tons slower on any modern >>> machine I know of... >> Isn't it true, though, that Python 3.3 has a completely new >> implementation of decimal that largely removes this disadvantage? >> >> ChrisA > > I wouldn't know, I'm on 3.2. However, I sincerely doubt if it's within > a factor of 100 of the speed of the binary float, at least on > pentium-class machines that do binary float in microcode. A dozen years > or so ago, when the IEEE floating point standard was still being formed, > I tried to argue the committee into including decimal in the standard > (which they did much later). Had it been in the standard then, we MIGHT > have had decimal fp on chip as well as binary. Then again, the standard > was roughly based on the already-existing Intel 8087, so maybe it was > just hopeless. > > I guess it's possible that for some operations, the cost of the > byte-code interpreter and function lookup, etc. might reduce the > apparent penalty. Has anybody done any timings? Try this, from python-dev list: http://mail.python.org/pipermail/python-dev/2012-September/121832.html It's not as fast as float, but it sure gives a good account for itself. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
Am 08.10.2012 16:07, schrieb iMath: To get the accurate value of 1 - 0.999 ,how to implement the python algorithm ? Algorithms are generally language-agnostic, so what is your question BTW ,Windows’s calculator get the accurate value ,anyone who knows how to implement it ? You should use a library that handles arbitrary-precision floating point numbers, Python's built-in floating point type corresponds to C's double type and that is typically a IEEE float, which means a limited precision. Just search the web for one. If you really want to do it yourself, you could leverage the fact that Python's integral type has a dynamic size, so that it can represent numbers with more than the typical 32 or 64 bits width. BTW: If this is not a homework question, you should ask much more specifically. My anwers are intentionally vague in order to not spoil you the learning effect. Cheers! Uli -- http://mail.python.org/mailman/listinfo/python-list
how to build Object for List data
Hi All I have Data call FM01. Each format have F1.. F50 Fields. And Global Program G1..Gn. The format like below as text file FM01 Fld #FieldValidation 1F1 N/A 2F2 N/A 3F3 Program1,1,2,3 # Add F1 and F2 value to F3 4F4 Program2,1,3,4 # Add F1 and F3 value to F4 ... 50 Seq validation 1 Program3,1,3,4 # max(F1,F3) to F4 .. n How to using python to Read the text file, Build the data as object class ? moonhkt -- http://mail.python.org/mailman/listinfo/python-list
Re: how to build Object for List data
Seq validation 1 Program3,1,3,4 # max(F1,F3) to F4 .. n How to using python to Read the text file, Build the data as object class ? Open the file using the open() command. Then iterate over the lines within a stateful algorithm that parses the lines with regular expressions. What did you try so far? -- http://mail.python.org/mailman/listinfo/python-list
Re: how to build Object for List data
On Mon, Oct 8, 2012 at 2:50 PM, Laszlo Nagy wrote: >> >> Seq validation >> 1 Program3,1,3,4 # max(F1,F3) to F4 >> .. >> n >> How to using python to Read the text file, Build the data as object >> class ? > > Open the file using the open() command. Then iterate over the lines within a > stateful algorithm that parses the lines with regular expressions. before you go the regex route try str.split() since it looks like your columns are separated by tabs or spaces. You could also look into csv package. > > What did you try so far? > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick -- http://mail.python.org/mailman/listinfo/python-list
Insert item before each element of a list
What's the best way to accomplish this? Am I over-complicating it? My gut
feeling is there is a better way than the following:
>>> import itertools
>>> x = [1, 2, 3]
>>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
>>> range(len(x
>>> y
['insertme', 1, 'insertme', 2, 'insertme', 3]
I appreciate any and all feedback.
--Matt
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Mon, Oct 8, 2012 at 1:28 PM, wrote:
> What's the best way to accomplish this? Am I over-complicating it? My gut
> feeling is there is a better way than the following:
>
import itertools
x = [1, 2, 3]
y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
range(len(x
y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]
>
> I appreciate any and all feedback.
Using the "roundrobin" recipe from the itertools documentation:
x = [1, 2, 3]
y = list(roundrobin(itertools.repeat('insertme', len(x)), x))
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Mon, Oct 8, 2012 at 12:28 PM, wrote:
> What's the best way to accomplish this? Am I over-complicating it? My
> gut feeling is there is a better way than the following:
>
> >>> import itertools
> >>> x = [1, 2, 3]
> >>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
> range(len(x
> >>> y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]
>
There is no need to use range and iterate over the indices:
list(itertools.chain.from_iterable(('insertme', i) for i in x))
You could simplify that even farther to a simple list expression:
[('insertme', i) for i in x]
You could also use zip and repeat:
zip(itertools.repeat('insertme'), x) # Use itertools.izip for a generator
rather than a list.
All code is untested.
>
> I appreciate any and all feedback.
>
> --Matt
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On 2012-10-08 20:28, [email protected] wrote: What's the best way to accomplish this? Am I over-complicating it? My gut feeling is there is a better way than the following: import itertools x = [1, 2, 3] y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in range(len(x y ['insertme', 1, 'insertme', 2, 'insertme', 3] I appreciate any and all feedback. Slightly better is: y = list(itertools.chain.from_iterable(('insertme', i) for i in x)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On 8 October 2012 20:28, wrote:
> What's the best way to accomplish this? Am I over-complicating it? My
> gut feeling is there is a better way than the following:
>
> >>> import itertools
> >>> x = [1, 2, 3]
> >>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
> range(len(x
> >>> y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]
>
> I appreciate any and all feedback.
>
That's probably not the best way.
list(itertools.chain.from_iterable(zip(itertools.repeat("insertme"), [1, 2,
> 3])))
(tested)
written like this:
inserts = itertools.repeat("insertme")
itertools.chain.from_iterable(zip(inserts, x))
(untested)
But it's not far. I wouldn't use Ian Kelly's method (no offence), because
of len(x): it's less compatible with iterables. Others have ninja'd me with
good comments, too.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Mon, Oct 8, 2012 at 1:52 PM, Joshua Landau wrote: > But it's not far. I wouldn't use Ian Kelly's method (no offence), because of > len(x): it's less compatible with iterables. Others have ninja'd me with > good comments, too. That's fair, I probably wouldn't use it either. It points to a possible need for a roundrobin variant that truncates like zip when one of the iterables runs out. It would have to do some look-ahead, but it would remove the need for the len(x) restriction on itertools.repeat. -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On 10/08/2012 09:45 PM, Chris Kaynor wrote:
> [('insertme', i) for i in x]
This is not enough, you have to merge it afterwards.
y = [item for tup in y for item in tup]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
[email protected] wrote: > What's the best way to accomplish this? Am I over-complicating it? My > gut feeling is there is a better way than the following: > import itertools x = [1, 2, 3] y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in range(len(x y > ['insertme', 1, 'insertme', 2, 'insertme', 3] Less general than chain.from_iterable(izip(repeat("insertme"), x)): >>> x = [1, 2, 3] >>> y = 2*len(x)*["insertme"] >>> y[1::2] = x >>> y ['insertme', 1, 'insertme', 2, 'insertme', 3] -- http://mail.python.org/mailman/listinfo/python-list
RE: Insert item before each element of a list
Agon Hajdari wrote: > Sent: Monday, October 08, 2012 3:12 PM > To: [email protected] > Subject: Re: Insert item before each element of a list > > On 10/08/2012 09:45 PM, Chris Kaynor wrote: > > [('insertme', i) for i in x] > > This is not enough, you have to merge it afterwards. Why do you say that? It seems to work just fine for me. >>> x [0, 1, 2, 3, 4] >>> [('insertme', i) for i in x] [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3), ('insertme', 4)] > > y = [item for tup in y for item in tup] > This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On 10/08/2012 11:15 PM, Prasad, Ramit wrote: > Agon Hajdari wrote: >> Sent: Monday, October 08, 2012 3:12 PM >> To: [email protected] >> Subject: Re: Insert item before each element of a list >> >> On 10/08/2012 09:45 PM, Chris Kaynor wrote: >>> [('insertme', i) for i in x] >> >> This is not enough, you have to merge it afterwards. > > Why do you say that? It seems to work just fine for me. > x > [0, 1, 2, 3, 4] [('insertme', i) for i in x] > [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3), > ('insertme', 4)] > >> >> y = [item for tup in y for item in tup] >> > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > I think he wanted to have a 'plain' list a = [0, 1, 0, 2, 0, 3] and not a = [(0, 1), (0, 2), (0, 3)] -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpaking Tuple
Hi there, On Sat, Oct 06, 2012 at 03:08:38PM +, Steven D'Aprano wrote: > > my_tuple = my_tuple[:4] > a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4] > Are you sure this works as you expect? I just stumbled over the following: $ python Python 3.2.3 (default, Jun 25 2012, 23:10:56) [GCC 4.7.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> split = ['foo', 'bar'] >>> head, tail = split if len(split) == 2 else split[0], None >>> head ['foo', 'bar'] >>> tail >>> I don't get it! Could someone help me, please? Why is head not 'foo' and tail not 'bar'? Regards, Thomas -- http://mail.python.org/mailman/listinfo/python-list
RE: Insert item before each element of a list
Agon Hajdari wrote:
> On 10/08/2012 11:15 PM, Prasad, Ramit wrote:
> > Agon Hajdari wrote:
> >>
> >> On 10/08/2012 09:45 PM, Chris Kaynor wrote:
> >>> [('insertme', i) for i in x]
> >>
> >> This is not enough, you have to merge it afterwards.
> >
> > Why do you say that? It seems to work just fine for me.
> >
> x
> > [0, 1, 2, 3, 4]
> [('insertme', i) for i in x]
> > [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3),
> > ('insertme', 4)]
> >
> >>
> >> y = [item for tup in y for item in tup]
>
> I think he wanted to have a 'plain' list
> a = [0, 1, 0, 2, 0, 3]
> and not
> a = [(0, 1), (0, 2), (0, 3)]
You are absolutely correct. I missed that when I tried it.
Instead of the nested list comprehension, I might have used
map instead.
>>> y = [('insertme', i) for i in x]
>>> z = []
>>> _ = map( z.extend, y )
>>> z
['insertme', 0, 'insertme', 1, 'insertme', 2, 'insertme', 3, 'insertme', 4]
I am not sure which is more Pythonic, but to me map + list.extend tells
me more explicitly that I am dealing with an iterable of iterables.
It might make more sense to only to me though.
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
[email protected] writes: x = [1, 2, 3] .. y > ['insertme', 1, 'insertme', 2, 'insertme', 3] def ix(prefix, x): for a in x: yield prefix yield a y = list(ix('insertme', x)) from itertools import * y = list(chain.from_iterable(izip(repeat('insertme'), x))) etc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpaking Tuple
On 8 October 2012 22:45, Thomas Bach wrote:
> Hi there,
>
> On Sat, Oct 06, 2012 at 03:08:38PM +, Steven D'Aprano wrote:
> >
> > my_tuple = my_tuple[:4]
> > a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4]
> >
>
> Are you sure this works as you expect? I just stumbled over the following:
>
> $ python
> Python 3.2.3 (default, Jun 25 2012, 23:10:56)
> [GCC 4.7.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> split = ['foo', 'bar']
> >>> head, tail = split if len(split) == 2 else split[0], None
> >>> head
> ['foo', 'bar']
> >>> tail
> >>>
>
> I don't get it! Could someone help me, please? Why is head not 'foo'
> and tail not 'bar'?
>
Here's a hint:
>>> split = "ab"
>>> head, tail = split if len(split) == 2 else split[0], None
>>> head, tail
('ab', None)
>>> head, tail = (split if len(split) == 2 else split[0]), None
>>> head, tail
('ab', None)
>>> head, tail = split if len(split) == 2 else (split[0], None)
>>> head, tail
('a', 'b')
Regards,
> Thomas
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Mon, 08 Oct 2012 12:28:43 -0700, mooremathewl wrote:
import itertools
x = [1, 2, 3]
y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
range(len(x y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]
>>> [i for j in [1,2,3] for i in ('insertme', j)]
['insertme', 1, 'insertme', 2, 'insertme', 3]
--
http://mail.python.org/mailman/listinfo/python-list
RE: Unpaking Tuple
Thomas Bach wrote: > Hi there, > > On Sat, Oct 06, 2012 at 03:08:38PM +, Steven D'Aprano wrote: > > > > my_tuple = my_tuple[:4] > > a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4] > > > > Are you sure this works as you expect? I just stumbled over the following: > > $ python > Python 3.2.3 (default, Jun 25 2012, 23:10:56) > [GCC 4.7.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> split = ['foo', 'bar'] > >>> head, tail = split if len(split) == 2 else split[0], None > >>> head > ['foo', 'bar'] > >>> tail > >>> > > I don't get it! Could someone help me, please? Why is head not 'foo' > and tail not 'bar'? > > Regards, > Thomas > -- I think you just need to wrap the else in parenthesis so the else clause is treated as a tuple. Without the parenthesis I believe it is grouping the code like this. head, tail = (split if len(split) == 2 else split[0] ), None You want: head, tail = split if len(split) == 2 else (split[0], None ) Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
Re: Plumbum (Shell Combinators) hits v1.0
2012/10/6 Tomer Filiba > http://plumbum.readthedocs.org/en/latest/index.html > > Ever wished the wrist-handiness of shell scripts be put into a real > programming language? Say hello to Plumbum Shell Combinators. Plumbum > (Latin for lead, which was used to create pipes back in the day) is a small > yet feature-rich library for shell script-like programs in Python. The > motto of the library is “Never write shell scripts again”, and thus it > attempts to mimic the shell syntax (shell combinators) where it makes > sense, while keeping it all Pythonic and cross-platform. > > Apart from shell-like syntax and handy shortcuts, the library provides > local and remote command execution (over SSH), local and remote file-system > paths, easy working-directory and environment manipulation, and a > programmatic Command-Line Interface (CLI) application toolkit. Awesome, can it be used in combination with CJSH http://jonathanscorner.com/cjsh/ ? Regards, Amirouche -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
[email protected] wrote: > What's the best way to accomplish this? Am I over-complicating it? > My gut feeling is there is a better way than the following: > > >>> import itertools > >>> x = [1, 2, 3] > >>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i > in range(len(x >>> y > ['insertme', 1, 'insertme', 2, 'insertme', 3] > > I appreciate any and all feedback. > > --Matt Just like the Zen of Python (http://www.python.org/dev/peps/pep-0020/) says . . . "There should be at least ten-- and preferably more --clever and obscure ways to do it." -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On 10/8/2012 11:13 AM, Dave Angel wrote:
Isn't it true, though, that Python 3.3 has a completely new
implementation of decimal that largely removes this disadvantage?
I wouldn't know, I'm on 3.2. However, I sincerely doubt if it's within
a factor of 100 of the speed of the binary float, at least on
>>> import timeit as tt
>>> tt.repeat("float('1.0')-float('0.99')")
[0.6856039948871151, 0.669049830953858, 0.668688006423692]
>>> tt.repeat("Decimal('1.0')-Decimal('0.99')", "from decimal
import Decimal")
[1.3204655578092428, 1.286977575486688, 1.2893188292009938]
>>> tt.repeat("a-b", "a = 1.0; b=0.99")
[0.06100386171601713, 0.04453853592786, 0.04451548406098027]
>>> tt.repeat("a-b", "from decimal import Decimal as D; a = D('1.0'); b
= D('0.99')")
[0.14685526219517442, 0.12909696344064514, 0.12646059371189722]
A factor of 3, as S. Krah, the cdecimal author, claimed
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On 10/8/2012 3:28 PM, [email protected] wrote: What's the best way to accomplish this? Am I over-complicating it? My gut feeling is there is a better way than the following: import itertools x = [1, 2, 3] y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in range(len(x y ['insertme', 1, 'insertme', 2, 'insertme', 3] The straightforward, crystal-clear, old-fashioned way >>> lst = [] >>> for item in [1,2,3]: lst.append('insert me') lst.append(item) >>> lst ['insert me', 1, 'insert me', 2, 'insert me', 3] Paul Rubin's list(gfunc(prefix, lst)) is similar in execution. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
In article , Terry Reedy wrote: > On 10/8/2012 3:28 PM, [email protected] wrote: > > What's the best way to accomplish this? Am I over-complicating it? My gut > > feeling is there is a better way than the following: > > > import itertools > x = [1, 2, 3] > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in > range(len(x > y > > ['insertme', 1, 'insertme', 2, 'insertme', 3] > > The straightforward, crystal-clear, old-fashioned way > > >>> lst = [] > >>> for item in [1,2,3]: > lst.append('insert me') > lst.append(item) I'm going to go with this one. I think people tend to over-abuse list comprehensions. They're a great shorthand for many of the most common use cases, but once you stray from the simple examples, you quickly end up with something totally obscure. > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in > range(len(x A statement ending in four close parens is usually going to be pretty difficult to figure out. This is one where I had to pull out my pencil and start pairing them off manually to figure out how to parse it. -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
On 10/08/2012 09:45 PM, Terry Reedy wrote:
> On 10/8/2012 11:13 AM, Dave Angel wrote:
>
>>> Isn't it true, though, that Python 3.3 has a completely new
>>> implementation of decimal that largely removes this disadvantage?
>
>> I wouldn't know, I'm on 3.2. However, I sincerely doubt if it's within
>> a factor of 100 of the speed of the binary float, at least on
>
> >>> import timeit as tt
> >>> tt.repeat("float('1.0')-float('0.99')")
> [0.6856039948871151, 0.669049830953858, 0.668688006423692]
> >>> tt.repeat("Decimal('1.0')-Decimal('0.99')", "from decimal
> import Decimal")
> [1.3204655578092428, 1.286977575486688, 1.2893188292009938]
>
> >>> tt.repeat("a-b", "a = 1.0; b=0.99")
> [0.06100386171601713, 0.04453853592786, 0.04451548406098027]
> >>> tt.repeat("a-b", "from decimal import Decimal as D; a = D('1.0');
> b = D('0.99')")
> [0.14685526219517442, 0.12909696344064514, 0.12646059371189722]
>
> A factor of 3, as S. Krah, the cdecimal author, claimed
I concede the point. But I was "sincere" in my doubt.
What I'm curious about now is 1) how much the various operators vary in
that 3:1 ratio and 2) how much the overhead portions are using of that
time.
I have to assume that timeit.repeat doesn't count the time spent in its
second argument, right? Because converting a string to a Decimal should
be much faster than converting one to float. But what about the
overhead of eval(), or whatever it uses? Is the "a-b" converted to byte
code just once? Or is it recompiled each time through tje loop?
I have to admit not spending much time in timeit(); I usually end up
timing things with my own loops. So i'd really like to understand how
overhead is figured.
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Oct 9, 7:06 am, Roy Smith wrote: > In article , > Terry Reedy wrote: > > > > > > > > > > > On 10/8/2012 3:28 PM, [email protected] wrote: > > > What's the best way to accomplish this? Am I over-complicating it? My > > > gut > > > feeling is there is a better way than the following: > > > import itertools > > x = [1, 2, 3] > > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in > > range(len(x > > y > > > ['insertme', 1, 'insertme', 2, 'insertme', 3] > > > The straightforward, crystal-clear, old-fashioned way > > > >>> lst = [] > > >>> for item in [1,2,3]: > > lst.append('insert me') > > lst.append(item) > > I'm going to go with this one. I think people tend to over-abuse list > comprehensions. They're a great shorthand for many of the most common > use cases, but once you stray from the simple examples, you quickly end > up with something totally obscure. > > > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in > > range(len(x > > A statement ending in four close parens is usually going to be pretty > difficult to figure out. This is one where I had to pull out my pencil > and start pairing them off manually to figure out how to parse it. How about a 2-paren version? >>> x = [1,2,3] >>> reduce(operator.add, [['insert', a] for a in x]) ['insert', 1, 'insert', 2, 'insert', 3] -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Oct 9, 7:34 am, rusi wrote: > How about a 2-paren version? > > >>> x = [1,2,3] > >>> reduce(operator.add, [['insert', a] for a in x]) > > ['insert', 1, 'insert', 2, 'insert', 3] Or if one prefers the different parens on the other side: >>> reduce(operator.add, (['insert', a] for a in x)) ['insert', 1, 'insert', 2, 'insert', 3] -- http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
yes, thanks all your tips. I did try sorted with itemgetter. However, the
sorted results are same as follows whether I set reverse=True or reverse=
False. Isn't it strange? Thanks.
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> for key in pairs:
list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=True)
[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]
>>> for key in pairs:
list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=False)
[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Oct 9, 12:06 pm, Roy Smith wrote: > I'm going to go with this one. I think people tend to over-abuse list > comprehensions. I weep whenever I find `_ = [...]` in other people's code. -- http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
Dear all, the problem has been solved as follows. Thanks anyway:
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> list_simi=[]
>>> for key in pairs:
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
>>> from operator import itemgetter
>>> sorted(list_simi, key=itemgetter(1), reverse=False)
[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]
>>> sorted(list_simi, key=itemgetter(1), reverse=True)
[('car-automobile', 1.0), ('journey-voyage', 0.25), ('gem-jewel', 0.125)]
>>> sorted(list_simi, key=itemgetter(1))
[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]
--
http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
On Mon, Oct 8, 2012 at 9:13 PM, Token Type wrote: > yes, thanks all your tips. I did try sorted with itemgetter. However, the > sorted results are same as follows whether I set reverse=True or reverse= > False. Isn't it strange? Thanks. First of all, "sorted" does not sort the list in place as you seem to be expecting. It returns a new sorted list. Since your code does not store the return value of the sorted call anywhere, the sorted list is discarded and only the original list is kept. If you want to sort a list in place, use the list.sort method instead. Second, you're not sorting the overall list. On each iteration your code: 1) assigns a new empty list to list_simi; 2) processes one of the pairs; 3) adds the pair to the empty list; and 4) sorts the list. On the next iteration you then start all over again with a new empty list, and so when you get to the sorting step you're only sorting one item each time. You need to accumulate the list instead of wiping it out on each iteration, and only sort it after the loop has completed. -- http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
On Oct 9, 1:13 pm, Token Type wrote:
> yes, thanks all your tips. I did try sorted with itemgetter.
> However, the sorted results are same as follows whether I
> set reverse=True or reverse= False. Isn't it strange? Thanks.
That's because you're sorting each entry individually, not the entire
result. For every key-value pair, you create a new empty list, append
one tuple, and then sort it. The consistent order you're seeing is the
outcome of stepping through the dictionary keys.
This is untested, but it should be closer to what you're after, I
think. First it creates `list_simi` as a generator, then it sorts it.
import nltk
from nltk.corpus import wordnet as wn
from operator import itemgetter
pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
def find_similarity(word1, word2):
as_synset = lambda word: wn.synset( str(word) + '.n.01' )
return as_synset(word1).path_similarity( as_synset(word2) )
similarity_value = itemgetter(1)
list_simi = (
('%s-%s' % (word1, word2), find_similarity(word1, word2) )
for word1, word2 in pairs.iteritems()
)
list_simi = sorted(list_simi, key=similarity_value, reverse=True)
--
http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
Thanks indeed for all your suggestions. When I try my above codes, what puzzles
me is that when the data in the dictionary increase, some data become missing
in the sorted result. Quite odd. In the pairs, we have {'journey':'voyage'} but
in the sorted result no ('journey-voyage',0.25), which did appear in my first
post which was a small scale experiment. I am quite puzzled...
>>> pairs = {'car':'automobile', 'gem':'jewel',
>>> 'journey':'voyage','boy':'lad','coast':'shore', 'asylum':'madhouse',
>>> 'magician':'wizard', 'midday':'noon', 'furnace':'stove', 'food':'fruit',
>>> 'bird':'cock', 'bird':'crane', 'tool':'implement', 'brother':'monk',
>>> 'lad':'brother', 'crane':'implement', 'journey':'car', 'monk':'oracle',
>>> 'cemetery':'woodland', 'food':'rooster', 'coast':'hill',
>>> 'forest':'graveyard', 'shore':'woodland', 'monk':'slave',
>>> 'coast':'forest','lad':'wizard', 'chord':'smile', 'glass':'magician',
>>> 'rooster':'voyage', 'noon':'string'}
>>> list_simi=[]
>>> for key in pairs:
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
>>> from operator import itemgetter
>>> sorted(list_simi, key=itemgetter(1), reverse=True)
[('midday-noon', 1.0), ('car-automobile', 1.0), ('tool-implement', 0.5),
('boy-lad', 0.), ('lad-wizard', 0.2), ('monk-slave', 0.2),
('shore-woodland', 0.2), ('magician-wizard', 0.1),
('brother-monk', 0.125), ('asylum-madhouse', 0.125), ('gem-jewel', 0.125),
('cemetery-woodland', 0.), ('bird-crane', 0.),
('glass-magician', 0.), ('crane-implement', 0.1),
('chord-smile', 0.09090909090909091), ('coast-forest', 0.09090909090909091),
('furnace-stove', 0.07692307692307693), ('forest-graveyard',
0.07142857142857142), ('food-rooster', 0.0625), ('noon-string',
0.058823529411764705), ('journey-car', 0.05), ('rooster-voyage',
0.041664)]
--
http://mail.python.org/mailman/listinfo/python-list
Re: wordnet semantic similarity: how to refer to elements of a pair in a list? can we sort dictionary according to the value?
On Oct 9, 2:16 pm, Token Type wrote:
> When I try my above codes, what puzzles me is that when
> the data in the dictionary increase, some data become
> missing in the sorted result. Quite odd. In the pairs,
> we have {'journey':'voyage'} but in the sorted result no (
> 'journey-voyage',0.25)
>
> >>> pairs = {'car':'automobile', 'gem':'jewel',
> >>> 'journey':'voyage','boy':'lad','coast':'shore', 'asylum':'madhouse',
> >>> 'magician':'wizard', 'midday':'noon', 'furnace':'stove', 'food':'fruit',
> >>> 'bird':'cock', 'bird':'crane', 'tool':'implement', 'brother':'monk',
> >>> 'lad':'brother', 'crane':'implement', 'journey':'car', 'monk':'oracle',
> >>> 'cemetery':'woodland', 'food':'rooster', 'coast':'hill',
> >>> 'forest':'graveyard', 'shore':'woodland', 'monk':'slave',
> >>> 'coast':'forest','lad':'wizard', 'chord':'smile', 'glass':'magician',
> >>> 'rooster':'voyage', 'noon':'string'}
Keys are unique in dictionaries. You have two uses of 'journey'; the
second will overwrite the first.
Do you _need_ these items to be a dictionary? Are you doing any look
up? If not, just make it a list of tuples:
pairs = [ ('car', 'automobile'), ('gem', 'jewel') ...]
Then make your main loop:
for word1, word2 in pairs:
If you do need a dictionary for other reasons, you might want to try a
dictionary of lists:
pairs = {
'car': ['automobile', 'vehicle'],
'gem': ['jewel'],
}
for word1, synonyms in pairs:
for word2 in synonyms:
...
--
http://mail.python.org/mailman/listinfo/python-list
Re: RE: Unpaking Tuple
in 682592 20121008 232126 "Prasad, Ramit" wrote: >Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at = >03:08:38PM +, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_= >tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_= >tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >ar'?=0D=0A> =0D=0A> Regards,=0D=0A>Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >mail is confidential and subject to important disclaimers and=0D=0Aconditio= >ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri= >vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg= >an=2Ecom/pages/disclosures/email=2E How does one unpack this post? ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpaking Tuple
On 10/09/2012 02:07 AM, Bob Martin wrote: > in 682592 20121008 232126 "Prasad, Ramit" wrote: >> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at = >> 03:08:38PM +, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_= >> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_= >> >> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri= >> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg= >> an=2Ecom/pages/disclosures/email=2E > 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 email via python.org? or are you using google-groups or some other indirection? In any reasonable mail program, you can see the source of a message. Most of the troubles i've seen here have been caused by people trying to send html mail to a text-based mailing list. But in the case you quote, the original message came here as text/plain, and well formatted. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
