Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List Question

Reply
Thread Tools

List Question

 
 
brad
Guest
Posts: n/a
 
      10-02-2007
How is this expressed in Python?

If x is in y more than three times:
print x

y is a Python list.
 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:
> How is this expressed in Python?
>
> If x is in y more than three times:
> print x
>
> y is a Python list.


Simple and readable:
if len([a for a in y if x == a]) > 3:
print x

Or the slightly-too-flashy version:
if sum(1 for a in y if x == a) > 3:
print x

--
Paul Hankin

 
Reply With Quote
 
 
 
 
Michael Bentley
Guest
Posts: n/a
 
      10-02-2007

On Oct 2, 2007, at 2:06 PM, brad wrote:

> How is this expressed in Python?
>
> If x is in y more than three times:
> print x
>
> y is a Python list.


# Try using help -- help(list) or help(list.count) for instance...
if y.count(x) > 3:
print x

 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 10:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:
>
> > How is this expressed in Python?

>
> > If x is in y more than three times:
> > print x

>
> > y is a Python list.

>
> Simple and readable:
> if len([a for a in y if x == a]) > 3:
> print x
>
> Or the slightly-too-flashy version:
> if sum(1 for a in y if x == a) > 3:
> print x


Or the embarrassingly simple:

if y.count(x) > 3:
print x

--
Paul Hankin

 
Reply With Quote
 
Pablo Ziliani
Guest
Posts: n/a
 
      10-02-2007
Paul Hankin wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:
>
>> How is this expressed in Python?
>>
>> If x is in y more than three times:
>> print x
>>
>> y is a Python list.
>>

>
> Simple and readable:
> if len([a for a in y if x == a]) > 3:
> print x
>
> Or the slightly-too-flashy version:
> if sum(1 for a in y if x == a) > 3:
> print x


<joke>

I always use this full-featured, all-inclusive, rock-solid version (see
the try/except block):

count = i = 0
x = 1
y = [1,2,3,4,5,1,2,3,4,1,2,1]
try:
while count < 3:
if y[i] == x:
count += 1
i += 1
except RuntimeError:
pass
except IndexError:
pass
else:
print x

</joke>


Sorry, couldn't resist...
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 4:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:
>
> > How is this expressed in Python?

>
> > If x is in y more than three times:
> > print x

>
> > y is a Python list.

>
> Simple and readable:
> if len([a for a in y if x == a]) > 3:
> print x
>
> Or the slightly-too-flashy version:
> if sum(1 for a in y if x == a) > 3:
> print x
>
> --
> Paul Hankin


As long as you are eschewing count for sum, don't forget that true is
1 and false is 0:

if sum(x==a for a in y) > 3:
print x

-- Paul

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 4:58 pm, Pablo Ziliani <(E-Mail Removed)> wrote:
> Paul Hankin wrote:
> > On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:

>
> >> How is this expressed in Python?

>
> >> If x is in y more than three times:
> >> print x

>
> >> y is a Python list.

>
> > Simple and readable:
> > if len([a for a in y if x == a]) > 3:
> > print x

>
> > Or the slightly-too-flashy version:
> > if sum(1 for a in y if x == a) > 3:
> > print x

>
> <joke>
>
> I always use this full-featured, all-inclusive, rock-solid version (see
> the try/except block):
>
> count = i = 0
> x = 1
> y = [1,2,3,4,5,1,2,3,4,1,2,1]
> try:
> while count < 3:
> if y[i] == x:
> count += 1
> i += 1
> except RuntimeError:
> pass
> except IndexError:
> pass
> else:
> print x
>
> </joke>
>
> Sorry, couldn't resist...- Hide quoted text -
>
> - Show quoted text -


Well, there is an advantage to your method/madness, in that it does
short-circuiting once the magic count of 3 is found. If the list
contained *many* entries, or if the predicate were expensive to
evaluate, or if the count were likely to be satisfied within the first
few list elements, your approach beats the other count or sum
suggestions (since they evaluate all list entries).

Here's a version of your code using itertools.takewhile:

count = 0
for a in itertools.takewhile(lambda _:count<3,y):
count += (x==a)
if count==3:
print x

-- Paul


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-03-2007
brad a écrit :
> How is this expressed in Python?
>
> If x is in y more than three times:
> print x
>
> y is a Python list.



if y.count(x) > 3:
print x
 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-03-2007
On Oct 2, 11:09 pm, Paul McGuire <(E-Mail Removed)> wrote:
> On Oct 2, 4:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> > On Oct 2, 10:06 pm, brad <(E-Mail Removed)> wrote:

>
> > > How is this expressed in Python?

>
> > > If x is in y more than three times:
> > > print x

>
> > > y is a Python list.

>
> > Or the slightly-too-flashy version:
> > if sum(1 for a in y if x == a) > 3:
> > print x

>
> As long as you are eschewing count for sum, don't forget that true is
> 1 and false is 0:
>
> if sum(x==a for a in y) > 3:
> print x


I like it!

--
Paul Hankin

 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      10-03-2007
Pablo Ziliani wrote:

> <joke>
>
> I always use this full-featured, all-inclusive, rock-solid version
> (see the try/except block):
>
> count = i = 0
> x = 1
> y = [1,2,3,4,5,1,2,3,4,1,2,1]
> try:
> while count < 3:
> if y[i] == x:
> count += 1
> i += 1
> except RuntimeError:
> pass
> except IndexError:
> pass
> else:
> print x
>
> </joke>


Wrong, this must be just

except:
pass

Regards&CNRE,


Björn

--
BOFH excuse #141:

disks spinning backwards - toggle the hemisphere jumper.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>' asil klin Java 28 03-05-2011 01:59 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments