Newsgroups: comp.lang.perl.misc
From: "Veli-Pekka Tätilä"
Date: Sat, 5 Nov 2005 17:25:35 +0200
Subject: Re: Python doc problem example: gzip module (reprise)
Xah Lee wrote:
> Today i need to use Python to compress/decompress gzip files.
> However, scanning the doc after 20 se
Recommended:
Sophie's World, by Jostein Gaarder, 1995.
http://en.wikipedia.org/wiki/Sophie%27s_World
http://www.amazon.com/gp/product/0425152251/
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
--
http://mail.python.org/mailman/listinfo/python-list
i had the pleasure to read the PHP's manual today.
http://www.php.net/manual/en/
although Pretty Home Page is another criminal hack of the unix lineage,
but if we are here to judge the quality of its documentation, it is a
impeccability.
it has or possesses properties of:
• To the point and use
Post-modernism, Academia, and the Tech Geeking fuckheads
• the Sokal Affair
http://en.wikipedia.org/wiki/Sokal_Affair
• SCIGen and World Multi-Conference on Systemics, Cybernetics and
Informatics
http://pdos.csail.mit.edu/scigen/
• What are OOP's Jargons and Complexities, Xah Lee
recently i got a project that involves the use of php. In 2 days, i
read almost the entirety of the php doc. Finding it a breeze because it
is roughly based on Perl, of which i have mastery.
i felt a sensation of neatness, as if php = Perl Improved, for a
dedicated job of server-side scripting. Ev
# in Python, list can be done this way:
a = [0, 1, 2, 'more',4,5,6]
print a
# list can be joined with plus sign
b = a + [4,5,6]
print b
# list can be extracted by appending a square bracket with index
# negative index counts from right.
print b[2]
print b[-2]
# sublist extraction
print 'element
# here's a while statement in python.
a,b = 0,1
while b < 20:
print b
a,b = b,a+b
---
# here's the same code in perl
($a,$b)=(0,1);
while ($b<20) {
print $b, "\n";
($a,$b)= ($b, $a+$b);
}
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/m
i'm using groups-beta.google.com to post python code.
Is there a way to stop google from messing with my format? it seems to
have eaten the spaces in my python code.
thanks.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
gmane is great! its renaming of newsgroups is quite a headache.
i found that comp.lang.python corresponds to gmane.comp.python.general.
do you know which one corresponds to comp.lang.perl.misc?
there's no .misc or .general...
--
i thought there a strick like preceding a line by -- or something tha
while programing in Python, one can lookup syntax or info for keywords
or modules within Python.
In the command line, type
python
to get into the python interactive program. then type
help()
>From there one can type any keyword or module name to find out the
syntax or info. Everything is self-co
. # here's an example of if statement in python.
.
. x=-1
. if x<0:
. print 'neg'
. elif x==0:
. print 'zero'
. elif x==1:
. print 'one'
. else:
. print 'other'
.
. # the elif can be omitted.
. --
. # here's an example of if statement in perl
.
. $x=3
© # this is an example of for statement
© # the % symbol calculates the remainder
© # of division.
© # the range(m,n) function
© # gives a list from m to n-1.
©
© a = range(1,51)
© for x in a:
© if x % 2 == 0:
© print x, 'even'
©
© # note that in this example, for goes over a list.
© # e
does anyone know why the Python mode in emacs uses spaces for first
level indentation but one tab for second level?
i'm using emacs 21.3.50.1.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
© ok, here's the ordeal.
©
© for i in range(5):
© print i
© for i in range(2):
© print i, 'tt'
© for i in [3]:
© print i
© for i in [32]:
© print i
©
© # 1 level, 4 space
© # 2 level, 1 tab
© # 3 level, 1 tab, 4 spaces
© # 4 level, 2 tabs.
©
© w
© # the following is a example of defining
© # a function in Python.
©
© def fib(n):
© """This prints n terms of a sequence
© where each term is the sum of previous two,
© starting with terms 1 and 1."""
© result=[];a=1;b=1
© for i in range(n):
© result.append(b)
©
© # -*- coding: utf-8 -*-
© # Python
©
© # the "filter" function can be used to
© # reduce a list such that unwanted
© # elements are removed.
© # example:
©
© def even(n): return n % 2 == 0
© print filter( even, range(11))
©
©
© # the "map" function applies a function
© # to all elements of a list
errata:
* the variables in the perl section should be declared inside the
subroutine.
* the @_[0] should've been $_[0]
thanks for Dave Cross for pointing them out.
* the Mathematica Apply should be Select...
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.
erratum:
the Mathematica Apply should've been Select.
...
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
erratum:
the Mathematica Apply should've been Select.
...
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
© # -*- coding: utf-8 -*-
©
© # in Python, there's a special type of
© # data structure called keyed list. it
© # is a unordered list of pairs, each
© # consists of a key and a value. It is
© # also known as dictionary.
©
© # define a keyed list
© aa = {'john':3, 'mary':4, 'jane':5, 'vicky':7}
© p
© # -*- coding: utf-8 -*-
© # Python
©
© # one can write functions,
© # save it in a file
© # and later on load the file
© # and use these functions.
©
© # For example, save the following line in
© # a file and name it module1.py
© # def f1(n): returns range(n)
©
© # to load the file, use import
©
Python has iteritems() and enumerate() to be used in for loops.
can anyone tell me what these are by themselves, if anything?
are they just for idiom?
thanks.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
© # -*- coding: utf-8 -*-
© # Python
©
© # once a module is loaded
© # import mymodule
© # one can find all the names it
© # export with dir()
©
© import sys
© print dir(sys)
©
© # without argument it gives the names
© # you've defined
© print dir()
©
© # to find a list of built-in names
© # import
i've started to read python tutorial recently.
http://python.org/doc/2.3.4/tut/tut.html
Here are some quick critique:
quick example:
If the input string is too long, they don't truncate it, but return it
unchanged; this will mess up your column lay-out but that's usually
better than the alternati
# -*- coding: utf-8 -*-
# Python
# to open a file and write to file
# do
f=open('xfile.txt','w')
# this creates a file "object" and name it f.
# the second argument of open can be
# 'w' for write (overwrite exsiting file)
# 'a' for append (ditto)
# 'r' or read only
# to actually print to file
adding to my previosu comment...
In the Python tutorial:
http://python.org/doc/2.3.4/tut/node11.html
the beginning two paragraphs should be deleted. Nobody gives a shit
except a few smug academicians where the author wrote it for pleasing
himself. For 99% of readers, it is incomprehensible and irr
© # -*- coding: utf-8 -*-
© # Python
©
© # in Python, one can define a boxed set
© # of data and functions, which are
© # traditionally known as "class".
©
© # in the following, we define a set of data
© # and functions as a class, and name it xxx
© class xxx:
© "a class extempore! (^_^)"
©
# -*- coding: utf-8 -*-
# Python
# some venture into standard modules
import os
# print all names exported by the module
print dir(os)
# print the module's online manual
print help(os)
# the above is also available in
# interactive mode in Python terminal
# example of using a function
print 'c
in my previous two messages, i've criticized the inanity of vast
majority of language documentations and tutorials in the industry. I've
used the Python tutorial's chapter on class as an example. I've
indicated that proper tutorial should be simple, covering just common
cases, be self-contained, an
© # -*- coding: utf-8 -*-
© # Python
©
© import sys
©
© nn = len(sys.argv)
©
© if not nn==5:
© print "error: %s search_text replace_text in_file out_file" %
sys.argv[0]
© else:
© stext = sys.argv[1]
© rtext = sys.argv[2]
© input = open(sys.argv[3])
© output = open(sys.argv[4],'w
is there a syntax to comment out a block of code? i.e. like html's
or perhaps put a marker so that all lines from there on are ignored?
thanks.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
# -*- coding: utf-8 -*-
# Python
suppose you want to walk into a directory, say, to apply a string
replacement to all html files. The os.path.walk() rises for the
occasion.
© import os
© mydir= '/Users/t/Documents/unix_cilre/python'
© def myfun(s1, s2, s3):
© print s2 # current dir
© pr
for those interested, i've created a webpage for these perl-python
daily tips. The url is:
http://xahlee.org/perl-python/python.html
Thanks to those who have made useful comments. They will be
assimilated.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
> Hey all, I have seen no
in computer languages, often a function definition looks like this:
subroutine f (x1, x2, ...) {
variables ...
do this or that
}
in advanced languages such as LISP family, it is not uncommon to define
functions inside a function. For example:
subroutine f (x1, x2, ...) {
variables...
subroutine
# -*- coding: utf-8 -*-
# Python
# Suppose you want to spam your friend, and you have lots of
# friends. The solution is to write a program to do it. After a gander
# at python docs, one easily found the module for the job.
# see http://python.org/doc/2.3.4/lib/SMTP-example.html
# the code is a b
in Python, is there a way to quote a string as to avoid escaping ' or "
inside the string?
i.e. like Perl's q or qq.
thanks.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
suppose you want to do find & replace of string of all files in a
directory.
here's the code:
©# -*- coding: utf-8 -*-
©# Python
©
©import os,sys
©
©mydir= '/Users/t/web'
©
©findStr=''
©repStr=''
©
©def replaceStringInFile(findStr,repStr,filePath):
©"replaces all findStr by repStr in file file
# -*- coding: utf-8 -*-
# Python
# Matching string patterns
#
# Sometimes you want to know if a string is of
# particular pattern. Let's say in your website
# you have converted all images files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, ess
in the doc for re module
http://python.org/doc/lib/module-re.html
4.2.2 on Matching vs Searching
http://python.org/doc/lib/matching-searching.html
Its mentioning of Perl is irrelevant, since the majority reading that
page will not have expertise with Perl regex. The whole section should
be delete
i've noticed that in Python official doc
http://python.org/doc/lib/module-re.html
and also How-To doc
http://www.amk.ca/python/howto/regex/
both mentions the book "Mastering Regular Expressions" by Jeffrey
Friedl.
I suggest it be dropped in both places. The mentioning of this book in
the Perl/Pyt
# -*- coding: utf-8 -*-
# Python
# suppose you want to fetch a webpage.
from urllib import urlopen
print
urlopen('http://xahlee.org/Periodic_dosage_dir/_p2/russell-lecture.html').read()
# note the line
# from import
# it reads the library and import the function name
# to see available function
oops... it is in the tutorial... sorry.
though, where would one find it in the python reference?
i.e. the function def with variable/default parameters.
This is not a rhetorical question, but where would one start to look
for it in the python ref?
a language is used by programers. Subroutine def
in coding Python yesterday, i was quite stung by the fact that lists
appened to another list goes by as some so-called "reference". e.g.
t=range(5)
n=range(3)
n[0]='m'
t.append(n)
n[0]='h'
t.append(n)
print t
in the following code, after some 1 hour, finally i found the solution
of h[:]. (and th
Dear Chinook Lee,
Thank you very much. That seems a godsend. I'd like to also thank its
author Richard Gruet.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Chinook wrote:
> ...
> I don't get to the reference docs much. Mostly I use the quick reference
> guide and it's noted there in an easy to f
Dear Andrea Griffini,
Thanks for explaning this tricky underneath stuff.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Andrea Griffini wrote:
> On Sun, 19 Jun 2005 22:25:13 -0500, Terry Hancock
> <[EMAIL PROTECTED]> wrote:
>
> >> PS is there any difference between
> >> t=t+[li]
> >> t.append(li)
here's the Python spec for the Table function:
'''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the
range range(iStart,iEnd,iStep).
Example: Table(f,[3,10,2]) returns [f(3),f(5),f(7),f(9)]
Table(f,[iStart,iEnd,iStep], [jStart,jEnd,jStep], ...) returns a nested
list of f(i,j,...)
is it possible to eval a string like the following?
m='''
i0=[0,1]
i1=[2,3]
i2=[4,'a']
h0=[]
for j0 in i0:
h1=[]
for j1 in i1:
h2=[]
for j2 in i2:
h2.append(f(j0,j1,j2))
h1.append( h2[:] )
h0.append( h1[:] )
return h0'''
perhaps i'm tired, but why can't i run:
t='m=3'
pr
the example in the spec of previous post is wrong. Here's corrected
version:
here's the Python spec for the Table function:
'''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the
range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns
[f(3),f(5),f(7),f(9)] Table(f,[iStart,
ge, so they
share syntax form.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Duncan Booth wrote:
> Xah Lee wrote:
>
> > '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the
> > range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns
&g
der if i can even do it in a few days.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
David Van Horn wrote:
> Xah Lee wrote:
> > here's the Python spec for the Table function:
> ...
> > References:
> >
> > • for a context of this message, see: http://xahlee.org/tre
i have a large number of lines i want to turn into a list.
In perl, i can do
@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);
use Data::Dumper;
print Dumper([EMAIL PROTECTED]);
--
is there some shortcut to turn lines into list in Python?
Xah
[EMAIL PROTE
What is Expresiveness in a Computer Language
20050207, Xah Lee.
In languages human or computer, there's a notion of expressiveness.
English for example, is very expressive in manifestation, witness all
the poetry and implications and allusions and connotations and
dictions. There are a m
Most participants in the computering industry should benefit in reading
this essay:
George Orwell's “Politics and the English Language”, 1946.
Annotated:
http://xahlee.org/p/george_orwell_english.html
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
--
http://mail.python.org/mailman/listinfo/pyth
oriented communist/socialist
nations or sovereignly ruled kingdoms & queendoms.
this post is archived at:
http://xahlee.org/UnixResource_dir/writ/mshatred155.html
© copyright 2003 by Xah Lee.
Xah
[EMAIL PROTECTED]
â http://xahlee.org/
--
http://mail.python.org/mailman/listinfo/python-list
Jargons of Info Tech industry
(A Love of Jargons)
Xah Lee, 2002 Feb
People in the computing field like to spur the use of spurious jargons.
The less educated they are, the more they like extraneous jargons, such
as in the Unix & Perl community. Unlike mathematicians, where in
mathematics t
Unix, RFC, and Line Truncation
[Note: unix tradition requires that a return be inserted at every 70
characters in email messages or so so that each line are less than 80
characters. Unixers made this as a requirement into an RFC document.]
Xah Lee, 20020511
This truncation of lines business is
previously i've made serious criticisms on Python's documentations
problems.
(see http://xahlee.org/perl-python/re-write_notes.html )
I have indicated that a exemplary documentation is Wolfram Research
Incorporated's Mathematica language. (available online at
http://documents.wolfram.com/mathemati
today i need to use Python to decompress gzip files.
since i'm familiar with Python doc and have 10 years of computing
experience with 4 years in unix admin and perl, i have quickly located
the official doc:
http://python.org/doc/2.4.1/lib/module-gzip.html
but after a minute of scanning, please
Today i need to use Python to compress/decompress gzip files.
I quickly found the official doc:
http://python.org/doc/2.4.1/lib/module-gzip.html
I'd imagine it being a function something like
GzipFile(filePath, comprress/decompress, outputPath)
however, scanning the doc after 20 seconds there's
Apache by default uses the following format for date:
30/Aug/2005
is there a module that turn this directly into mmdd?
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
--
http://mail.python.org/mailman/listinfo/python-list
By the way, i have sent my criticisms to the proper python doc
maintainer or mailing list several months ago.
-
i'm very sorry to say, that the Python doc is one of the worst possible
in the industry. I'm very sick of Perl and its intentional obfuscation
and juvenile drivel style of i
On Python's Documentation
Xah Lee, 20050831
I'm very sorry to say, that the Python doc is one of the worst possible
in the industry. I'm very sick of Perl and its intentional obfuscation
and juvenile drivel style of its docs. I always wanted to learn Python
as a replacement of
nothing personal my friend. But just in case you are interested about
getting it:
the question here is about quality of documentation, not about whether
you got it.
http://xahlee.org/UnixResource_dir/writ/python_doc.html
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Sybren Stuvel wrote:
>
i noticed that Python uses various logos:
http://python.org/pics/pythonHi.gif
http://python.org/pics/PyBanner038.gif
http://python.org/pics/PyBanner037.gif
http://python.org/pics/PythonPoweredSmall.gif
http://wiki.python.org/pics/PyBanner057.gif
is this some decision that python should use vario
identity and as well as advertisement.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Steve Holden wrote:
> Xah Lee wrote:
> > i noticed that Python uses various logos:
> >
> > http://python.org/pics/pythonHi.gif
> > http://python.org/pics/PyBanner038.gif
> > http://
does anyone know why the folllowing prints to the screen?
# python
import os
os.system(r"ls")
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
--
http://mail.python.org/mailman/listinfo/python-list
Steve Holden wrote:
> This is all pretty basic stuff. Perhaps you should stop your verbal
> assault on the computer science community and start to learn the
> principles of what you are doing.
is this a supressed behavior that a human animal can finally
instinctively and justifiably release at ano
do you know what the Muses do when a mortal challenged them?
And, please tell me exactly what capacity you hold under the official
Python organization so that i can calculate to what degree i can kiss
your ass or feign mum of your ignorance.
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Steve Hol
Python Doc Problem Example: os.system
Xah Lee, 2005-09
today i'm trying to use Python to call shell commands. e.g. in Perl
something like
output=qx(ls)
in Python i quickly located the the function due to its
well-named-ness:
import os
os.system("ls")
however, according
Xah Lee wrote:
> does anyone know why the folllowing prints to the screen?
> # python
> import os
> os.system(r"ls")
Steve Holden wrote:
> It only prints to the screen when standard output of the invoking
> process is the screen. The sub-process forked by os.system
suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the last line. How can i determine when the first
process is done?
Example:
subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);
last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=sub
Thanks all.
I found the answer, rather easily.
To make a system call and wait for it, do:
subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]).wait();
--
this post is archived at:
http://xahlee.org/perl-python/system_calls.html
Xah
[EMAIL PROTECTED]
Martin Franklin wrote:
> import gzip
> log_file = gzip.open("access_log.4.gz")
> last_line = log_file.readlines()[-1]
> log_file.close()
does the
log_file.readlines()[-1]
actually read all the lines first?
i switched to system call with tail because originally i was using a
pure Python solution
isn't there a way to implement tail in python with the same class of
performance?
how's tail implemented?
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Fredrik Lundh wrote:
> Fredrik Lundh wrote:
>
> > zcat|tail is a LOT faster.
>
> and here's the "right way" to use that:
>
> from subprocess
in the regex module re:
Note: Match() is not exactly equivalent to Search() with "^". For
example:
re.search(r'^B', 'A\nB',re.M) # succeeds
re.match(r'B', 'A\nB',re.M) # fails
if without the re.M, would re.search and re.match be equivalent?
i wish to spruce up the rewritten re
Python Doc Problem Example
Quote from:
http://docs.python.org/lib/module-os.path.html
--
split( path)
Split the pathname path into a pair, (head, tail) where tail is the
last pathname component and head is everything leading up to that. The
tail part will never contain a slash
left out.
---
This post is archived at:
http://xahlee.org/perl-python/python_doc_os_path_split.html
Xah
[EMAIL PROTECTED]
∑ http://xahlee.org/
Xah Lee wrote:
> Python Doc Problem Example
>
> Quote from:
> http://docs.python.org/lib/module-os.path.html
> --
> s
Perl's documentation has come of age: http://perldoc.perl.org/
Python morons really need to learn:
• ample example codes.
• example codes are linked to the appropriate doc location for each
code word in the example.
• written in a task-oriented style, or manifest-functionality style.
That is, i
20050207 text pattern matching
# -*- coding: utf-8 -*-
# Python
# suppose you want to replace all strings of the form
#
# to
#
# in your html files.
# you can use the "re" module.
import re
text = r'''
blab blab
look at this pict
and this one , both are
beautiful, but also look: , and sequ
is it possible to write python code without any indentation?
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
i thought it is trivial for the Python parser to spit out a version
with matching brackets. Similarly, perhaps some opensourcing student
has modified a parser to read in a matching brackets delimited version
of Python.
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://ma
a year ago i wrote this perl program as part of a larger program.
as a exercise of fun, let's do a python version. I'll post my version
later today.
=pod
combo(n) returns a collection with elements of pairs that is all
possible combinations of 2 things from n. For example, combo(4)
returns {'3,4
David Eppstein's code is very nice.
Here's the python version of the perl code:
©# -*- coding: utf-8 -*-
©# Python
©
©def combo (n):
©'''returns all possible (unordered) pairs out of n numbers 1 to
n.
©
©Returns a dictionary. The keys are of the form "n,m",
©and their values are tuple
# -*- coding: utf-8 -*-
# Python
# David Eppstein of the Geometry Junkyard fame gave this elegant
# version for returing all possible pairs from a range of n numbers.
def combo2(n):
return dict([('%d,%d'%(i+1,j+1),(i+1,j+1)) for j in range(n) for i
in range(j)])
print combo2(5)
# this constr
here's a interesting real-world algoritm to have fun with.
attached below is the Perl documentation that i wrote for a function
called "reduce", which is really the heart of a larger software.
The implementation is really simple, but the key is to understand what
the function should be. I'll post
here are the answers:
Perl code:
sub reduce ($$) {
my %hh= %{$_[0]}; # e.g. {'1,2'=>[1,2],'5,6'=>[5,6],...}
my ($j1,$j2)=($_[1]->[0],$_[1]->[1]); # e.g. [3,4]
delete $hh{"$j1,$j2"};
foreach my $k (keys %hh) {
$k=~m/^(\d+),(\d+)$/;
my ($k1,$k2)=($1,$2);
if ($k1==$j1) {
my Python coding is not experienced. In this case, is
ps.pop("%d,%d"%(j[1],k[1]),0) out of ordinary?
if i have long paragraphs of documentation for a function, do i still
just attach it below the fun def?
Xah
Xah Lee wrote:
> here are the answers:
>
> ©Python code.
> ©
ps.pop("%d,%d"%(j[1],k[0]),0)
©return ps
©
©is reduce2 more efficient? It works entirely differently. I'll have
to think about it... besides algorithmic... onto the minute academic
diddling, i wonder if it is faster to delete entries in dict or add
entries...
Xah
[EMAIL
Xah Lee wrote:
> In imperative languages such as Perl and Python and Java, in general
it
> is not safe to delete elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a copy first, and work
with
> the copy.
Correction:
When looping thru a list
Xah Lee wrote:
> In imperative languages such as Perl and Python
> and Java, in general it is not safe to delete
> elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a
> copy first, and work with the copy.
Correction:
When looping thru a l
Xah Lee wrote:
> In imperative languages such as Perl and Python
> and Java, in general it is not safe to delete
> elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a
> copy first, and work with the copy.
Correction:
When looping thru a l
here's another interesting algorithmic exercise, again from part of a
larger program in the previous series.
Here's the original Perl documentation:
=pod
merge($pairings) takes a list of pairs, each pair indicates the
sameness
of the two indexes. Returns a partitioned list of same indexes.
For
here's the answer to the partition by equivalence exercise.
---
# Perl code
sub merge($) {
my @pairings = @{$_[0]};
my @interm; # array of hashs
# chop the first value of @pairings into @interm
$interm[0]={$pairings[0][0]=>'x'}; ${interm[0]}{$pairings[0]
The GOTO statement from Perl has been messed up.
This block:
©for group in interm:
©for newcoup in fin:
©for k in group.keys():
©if newcoup.has_key(k):
©for kk in group.keys(): newcoup[kk]='x';
©break
©break
©
an interesting problem so developed now is to write a function that
generate test cases for the purpose of testing performance. (just for
fun)
the design of this function could be interesting. We want to be able to
give parameters in this function so as to spit out all possible screw
test cases. F
when i try to run the following program, Python complains about some
global name frozenset is not defined. Is set some new facility in
Python 2.4?
©# from Reinhold Birkenfeld
©def merge(pairings):
©sets = {}
©for x1, x2 in pairings:
©newset = (sets.get(x1, frozenset([x1]))
©
I started to collect i believe the 4 or so solutions by different
people... but seems it's gonna take some an hour or more... So far the
only other one i've run and find alright is Reinhold Birkenfeld's
original. Others worth noting i'm aware of is David Epsteinn, improved
versions from Reinhold Bi
another functional exercise with lists.
Here's the perl documentation. I'll post a perl and the translated
python version in 48 hours.
=pod
parti(aList, equalFunc)
given a list aList of n elements, we want to return a list that is a
range of numbers from 1 to n, partition by the predicate funct
# the following solution is submitted by
# Sean Gugler and David Eppstein independently
# 20050224.
@def parti(aList, equalFunc):
@result = []
@for i in range(len(aList)):
@for s in result:
@if equalFunc( aList[i], aList[s[0]] ):
@s.append(i)
@
is there a way to write a expression of a function with more than 1
argument?
e.g., i want a expression that's equivalent to
def f(x,y)
return x+y
Xah
[EMAIL PROTECTED]
http://xahlee.org/PageTwo_dir/more.html
--
http://mail.python.org/mailman/listinfo/python-list
101 - 200 of 525 matches
Mail list logo