473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is there a better way to check an array?

I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')

Sep 1 '05 #1
9 1366
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')


if product[k] == '' and k in fieldIsRequired:
raise GMError(k + ' is required')

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Sep 1 '05 #2
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass


fieldIsRequired = not (k in notRequiredAry)

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 1 '05 #3
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')


if product[k] == '' and k in notRequiredAry:
raise GMError(k + ' is required')
Sep 1 '05 #4
You can check for membership in a list at least these ways:

my_list.index(candidate)
-returns the index into my_list of the first occurrence of candidate.
Raises ValueError if candidate doesn't occur in my_list.

my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.

candidate in my_list
-returns True if candidate occurs in my_list
The last is nicest (most pythonic?) if you don't actually need the
index, which it seems you don't. That being said, you should consider
the, what I believe is called, Complexity of your algorithm. In
particular, you should recall that checking for membership in a list
takes an amount of time proportional to the length of the list, while
checking for membership in a set (or dictionary) takes a constant
amount of time regardless of the size of the set. From what I see, it
appears that you don't necessarily need to keep the notRequiredAry
things in order, but that you really just need to kknow the set of
things that are not required. If you are making this check a lot, for
instance looping over a big list and checking for each member whether
it is not required, you should consider using a set instead of a list.
It might have the benefit of making your code more readable too.

Sep 1 '05 #5
Thanks for all the help everyone.

Steve, sets are perfect. I didn't even realize they existed. Somehow I
completely missed that part of the tutorial. Thanks :)

Sep 1 '05 #6
"Steve M" <sj******@gmail.com> writes:
You can check for membership in a list at least these ways:
Two ways - idnex and the in keyword.
my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.


Lists don't have a find method. strings do. Why is a good question.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 2 '05 #7
Mike Meyer wrote:
"Steve M" <sj******@gmail.com> writes:
my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.


Lists don't have a find method. strings do. Why is a good question.


Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it, I suspect
nobody also suggested that implementing find() was a good idea so it
wasn't done.

And, given both that index() exists and the recent discussion about the
potential problems caused by find() returning a valid slice index when
it fails, maybe it's _not_ a good idea...

-Peter
Sep 2 '05 #8
Peter Hansen wrote:
Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it


the list "index" method has been in Python since, like, forever (early 1991,
according to CVS, which means that it was added about two years before
the first 1.0 alpha was made public...)

</F>

Sep 2 '05 #9
Fredrik Lundh wrote:
Peter Hansen wrote:
Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it


the list "index" method has been in Python since, like, forever (early 1991,
according to CVS, which means that it was added about two years before
the first 1.0 alpha was made public...)


D'oh! My apologies: I was misremembering the addition of the optional
"start" and "stop" arguments to the existing list() method, which
happened in June 2003 (SF #754014 at
http://sourceforge.net/tracker/index...70&atid=355470
).

-Peter
Sep 2 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Google Mike | last post by:
{NOTE: I have PHP 4.2.2 for RH9 Linux.} Anyone have a better file_exists() out there? Even if you use shell out tricks with Linux using the `command` trick, I'd be interested to see what you...
9
by: Randell D. | last post by:
Folks, I have a large amount of values to store (we're talking tens, if not hundreds of bytes). I need this for a client side application - ignore the security consequences for the moment -...
0
by: 2trax | last post by:
Hi all, I am trying to mimic the unix security model (users and groups) in a web app. I have thought of two ways of implementing this, but am unsure which would be better. The first design...
10
by: robspyke | last post by:
Dear All, I am learning c at the moment - for recreational purposes really more than anything else - and I was just wondering if I could pick your collective wisdom on how best to code a...
52
by: Darklight | last post by:
Question: Write a function named sumarrays() that accepts two arrays that are the same size. The function should add each element in the array together and place the values in the thrid array. ...
24
by: Sid | last post by:
Hi, I am writing an application where I look for a white pixel by testing if all the R,G,B values are 255 i.e. I use if(RGB == 255 && RGB == 255 && RGB == 255) (assuming RGB is a pointer to...
47
by: Dickson Woo | last post by:
Just that.
1
by: Nip | last post by:
Hello eyerybody I have got an array of objects of the same class. These objects might be for example visible or not. Because of the performance I donot want to check these properties with...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.