Paul Rubin wrote:
if any(x[0]==element[0] for x in a):
How come this list comprehension isn't in [] brackets?
--
http://mail.python.org/mailman/listinfo/python-list
Boris Ozegovic wrote:
Paul Rubin wrote:
if any(x[0]==element[0] for x in a):
How come this list comprehension isn't in [] brackets?
It isn't list comprehension, it is generator expression
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Generator_expressions
--
Tero
--
TeroV wrote:
It isn't list comprehension, it is generator expression
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Generator_expressions
Nice. :)
--
http://mail.python.org/mailman/listinfo/python-list
On Feb 22, 7:01 pm, Paul McGuire [EMAIL PROTECTED] wrote:
On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
Paul Rubin http://[EMAIL PROTECTED] writes:
if any(x==element[0] for x in a):
a.append(element)
Should say:
if any(x[0]==element[0] for x in a):
On Feb 23, 6:18 pm, Paul Hankin [EMAIL PROTECTED] wrote:
On Feb 22, 7:01 pm, Paul McGuire [EMAIL PROTECTED] wrote:
On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
Paul Rubin http://[EMAIL PROTECTED] writes:
if any(x==element[0] for x in a):
thebjorn [EMAIL PROTECTED] writes:
If the lists are long enough to care, either rewrite use a set-based
solution if the items are hashable, or keep the elements sorted and
rewrite to use the bisect module if the elements can be ordered.
Well, you want a tree data structure to have fast
On Feb 24, 2:24 am, Paul Rubin http://[EMAIL PROTECTED] wrote:
thebjorn [EMAIL PROTECTED] writes:
If the lists are long enough to care, either rewrite use a set-based
solution if the items are hashable, or keep the elements sorted and
rewrite to use the bisect module if the elements can be
On Sat, 23 Feb 2008 17:19:47 -0800, thebjorn wrote:
On Feb 23, 6:18 pm, Paul Hankin [EMAIL PROTECTED] wrote:
IMO Jason's solution of testing containment in a generator is better
(more readable).
if element[0] not in (x[0] for x in a):
a.append(element)
It may be more readable
Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060))
element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
'/smsc/chp/aztec/padlib',
On Feb 22, 11:20 am, rh0dium [EMAIL PROTECTED] wrote:
found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
Thanks
On Feb 22, 11:20 am, rh0dium [EMAIL PROTECTED] wrote:
Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060))
element1 =
On Feb 22, 10:20 am, rh0dium [EMAIL PROTECTED] wrote:
Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = ('/smsc/chp/aztec/padlib/5VT.Cat',
'/smsc/chp/aztec/padlib',
'5VT.Cat', (33060))
element1 =
rh0dium [EMAIL PROTECTED] writes:
found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
Untested and I'm not sure I
Paul Rubin http://[EMAIL PROTECTED] writes:
if any(x==element[0] for x in a):
a.append(element)
Should say:
if any(x[0]==element[0] for x in a):
a.append(element)
--
http://mail.python.org/mailman/listinfo/python-list
On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
Paul Rubin http://[EMAIL PROTECTED] writes:
if any(x==element[0] for x in a):
a.append(element)
Should say:
if any(x[0]==element[0] for x in a):
a.append(element)
I think you have this backwards.
Paul McGuire [EMAIL PROTECTED] writes:
I think you have this backwards. Should be:
if not any(x[0]==element[0] for x in a):
a.append(element)
I think you are right, it was too early for me to be reading code when
I posted that ;-)
--
On Feb 22, 3:38 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
Paul McGuire [EMAIL PROTECTED] writes:
I think you have this backwards. Should be:
if not any(x[0]==element[0] for x in a):
a.append(element)
I think you are right, it was too early for me to be reading code
Paul McGuire [EMAIL PROTECTED] writes:
I'm still getting used to 'any' and 'all' as new Python built-ins -
but they'll do the short-circuiting as well as a for-loop-with-break.
But I think a set- or dict-based solution will still surpass a list-
based one for the OP.
I guess I don't
18 matches
Mail list logo