Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41613

Re: Test a list

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.03; 'syntax': 0.03; 'correct,': 0.09; 'iterate': 0.09; 'cc:addr :python-list': 0.10; 'index': 0.13; '-tkc': 0.16; 'accordingly': 0.16; 'comparison.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'offset,': 0.16; 'pythonic': 0.16; 'value:': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'element': 0.17; '(or': 0.18; 'math': 0.20; 'otherwise,': 0.20; 'bit': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'subject:list': 0.28; 'initial': 0.28; 'index,': 0.29; 'value)': 0.29; 'code': 0.31; 'running': 0.32; 'print': 0.32; 'equal': 0.33; 'likely': 0.33; 'list': 0.35; 'but': 0.36; 'compare': 0.36; 'test': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'your': 0.60; 'here': 0.65; 'sounds': 0.71; 'everything.': 0.84; 'received:50.22': 0.84; 'subject:Test': 0.84; 'ana': 0.91
Date Wed, 20 Mar 2013 13:36:01 -0500
From Tim Chase <python.list@tim.thechases.com>
To Ana Dionísio <anadionisio257@gmail.com>
Subject Re: Test a list
In-Reply-To <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com>
References <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com>
X-Mailer Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3573.1363804459.2939.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363804459 news.xs4all.nl 6990 [2001:888:2000:d::a6]:44183
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41613

Show key headers only | View raw


On 2013-03-20 11:15, Ana Dionísio wrote:
> t= [3,5,6,7,10,14,17,21]
> 
> Basically I want to print Test 1 when i is equal to an element of
> the list "t" and print Test 2 when i is not equal:
> 
> while i<=25:
>     if i==t[]:
>        print "Test1"
>     else:
>        print "Test2"
> 
> What is missing here for this script work?

Well, your code never increments "i", so it will loop forever; you
also don't subscript "t" with anything, so you have invalid syntax
there; you also don't have any values in the list that actually match
their 0-indexed offset, so even if your code was correct, it would
still (correctly) return Test2 for everything.

This sounds a bit like homework, but the Pythonic way would likely
iterate over the data and its enumeration:

  for index, value in enumerate(t):
    if index == value: # compare index & value accordingly
     ...

If you need to have the index start at 1 (or some other value)
instead of 0, and you're running Python2.6+, you can pass the initial
index to enumerate().  Otherwise, you have to do the math in your
comparison.

-tkc



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Test a list Ana Dionísio <anadionisio257@gmail.com> - 2013-03-20 11:15 -0700
  Re: Test a list timothy crosley <timothy.crosley@gmail.com> - 2013-03-20 11:24 -0700
  Re: Test a list Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-20 14:26 -0400
  Re: Test a list Tim Chase <python.list@tim.thechases.com> - 2013-03-20 13:36 -0500
  Re: Test a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-20 18:40 +0000
  Re: Test a list John Gordon <gordon@panix.com> - 2013-03-20 20:39 +0000

csiph-web