ods, both set() and frozenset() share the same
underlying code and data structure. In this respect, they differ from list()
and tuple() which have no shared code and has two substantially different
data structures.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
ribly opposed to the idea. I just
find the case for it to be somewhat weak. Also, I'm not sure it warrants the
effort, the code clutter, or introducing issues like having a semantic
difference between the result of frozenset() and the result of frozenset([]).
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
for elem in s:
bag[elem] = bag.get(elem, 0) + 1
print [elem for elem, count in bag.iteritems() if count>1]
Raymond Hettinger
P.S. Extra credit problem: make the itertools solution output values in the
order seen.
--
http://mail.python.org/mailman/listinfo/python-list
end up with [0,1,2,3,2,4,5].
>>> import itertools
>>> [k for k, v in itertools.groupby(lst)]
[0, 1, 2, 3, 2, 4, 5]
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
return True in s or s == set([None]) and None
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
True in lst:
> return True
> elif False in lst:
> return False
> else:
> return None
>
> This has a light code smell for me though -- can anyone see a simpler
> way of writing this?
return max(lst)
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
(there is probably a much greater demand for
combinatorics, numeric/numarray, or financial modules). If the appeal is not
broad, it has little chance of making it into the standard library.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
d in lib/test/test_builtins.py
The key= and reverse= parameters were introduced to list.sort() in Py2.4.
Consequently, the above code won't provide the desired functionality in Py2.3
and prior.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
l and note that stability is not
preserved:
>>> data = [('a', 1), ('a', 2), ('b', 3)]
>>> data.sort(key=lambda record: record[0])
>>> data.reverse()
>>> data
[('b', 3), ('a', 2), ('a', 1)]
Here's another way of accomplishing the original sort and preserving stability:
>>> data = [('a', 1), ('a', 2), ('b', 3)]
>>> sorted(data, cmp = lambda x,y: cmp(y[0], x[0]))
[('b', 3), ('a', 1), ('a', 2)]
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
thing but is implemented a bit differently. A
custom key wrapper is applied to each object so that only the key value gets
compared (no need for a full tuple with a tie breaker value).
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
[Steve Holden]
> Since it doesn't yet optimize 2+5 to a constant-folded 7 you should
> realize that you are suggesting a large increase in the compiler's
> analytical powers.
FWIW, limited constant folding is already in CVS for Py2.5.
Raymond Hettinger
--
http://mail
docstring for Lib/test/test_iterlen.py .
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
[Raymond Hettinger]
> >List slice assignment is an example of a tool with a special case
optimization
> >for inputs that know their own length -- that enables the tool to
pre-allocate
> >its result rather than growing and resizing in spurts. Other such tools
include
> &g
tor. It would make me examine how 'a' is being used and check
whether the surrounding code can use the iterator or an new object.
> Comments, clues, ... please.
As a point of interest, note that Py2.4 improved some of its built-in iterators
to report their length if req
__().
Still, if __iter__() is provided, UserDict.DictMixin will take advantage of it.
The same is also true for __contains__(), and iteritems().
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
e real issue with PEP 288's idea for generator attributes is that the current
C implementation doesn't readily accommodate this change. Major surgery would
be required :-(
The more important part of the PEP is the idea for generator exceptions. The
need arises in the context of flushin
le)
... rows = range(len(data))
... for col in xrange(len(data[0])):
... args = [data[row][col] for rows in rows]
... yield f(*args)
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
posting present only toy
examples -- real use cases have not yet emerged. If some do emerge, I
suspect that each problem will have a better solution (using existing
tools) than the one being proposed. If so, then adopting the proposal
will have the negative effect of leading folks away from the corre
a,b))
1.62 sec:sum(x*y for x,y in zip(a,b))
0.75 sec:sum(imap(mul, a, b))
1.04 sec:sum(map(mul, a, b))
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
leton instance of None. That will
occur even if you've mucked with None entry in the globals dictionary.
Bytecode for eval() doesn't go through the bytecode optimizer so its dictionary
lookup is retained (producing the effect in your example).
To have made None a literal constant would
all in comparison.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
,y in zip(a,b))
0.635497577901 sum(imap(mul, a, b))
0.85894416601 sum(map(mul, a, b))
C:\pydev>python timedot.py 10
2.19490353509 sum(a[i]*b[i] for i in xrange(len(a)))
2.01773998894 sum(x*y for x,y in izip(a,b))
2.44932533231 sum(x*y for x,y in zip(a,b))
1.24698871922 sum(imap(mul, a, b))
1.
;None' only just became a constant in 2.4.
>
> I don't know if 'True' and 'False' are in line for similar treatment (there
are
> obvious backwards compatibility issues in doing so).
It is unlike to before Py3.0. Making them constants would break the reams
ist comprehensions got a nice 60% boost on my
machine:
C:\py24\Lib>\python23\python timeit.py -r9 "[i for i in xrange(1000)]"
100 loops, best of 9: 1.11 msec per loop
C:\py24\Lib>\python24\python timeit.py -r9 "[i for i in xrange(1000)]"
1000 loops, best of 9: 417
omps and genexps.
If you want to go the extra distance, itertools.izip() can offer a performance
boost and better memory utilization than zip(). It can be used almost anywhere
as long as the app doesn't demand a real list.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
double quotes? For making changes to Python code, I
> would also like to avoid changing text in comments, either the '#' or
> '""" ... """' kind.
The source for the tokenize module covers all these bases.
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
nt?
Yes:
>>> set((x,round(y)) for x,y in a) & set((x,round(y)) for x,y in b)
set([(123, 8.0), (123, 2.0), (123, 1.0)])
> I'm using python 2.3.
>>> from sets import Set as set
>>> set([(x,round(y)) for x,y in a]) & set([(x,round(y)) for x,y in b])
set([(123, 8.0), (123, 2.0), (123, 1.0)])
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
((0, (1, 0, 1), 1)))
> '1.01E+3'
> >>>
>
> how do you make the 2nd example print 1010?
The quantize method will convert to any desired exponent (zero in your example):
>>> d = (Decimal((0, (1, 0, 1), 1)))
>>> d
Decimal("1.01E+3")
>>> d.quantize(Decimal(1))
Decimal("1010")
Raymond Hettinger
--
http://mail.python.org/mailman/listinfo/python-list
701 - 728 of 728 matches
Mail list logo