Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41610 > unrolled thread
| Started by | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| First post | 2013-03-20 11:15 -0700 |
| Last post | 2013-03-20 20:39 +0000 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-03-20 11:15 -0700 |
| Subject | Test a list |
| Message-ID | <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> |
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?
Thank you all
[toc] | [next] | [standalone]
| From | timothy crosley <timothy.crosley@gmail.com> |
|---|---|
| Date | 2013-03-20 11:24 -0700 |
| Message-ID | <a63f3127-fbf7-4bb8-9152-41c6820d5937@googlegroups.com> |
| In reply to | #41610 |
Hi Ana, if I understand your question correctly, all you have to do to test this is to write: if i in t: print "Test1" else: print "Test2" On Wednesday, March 20, 2013 2:15:27 PM UTC-4, 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? > > > > Thank you all
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-03-20 14:26 -0400 |
| Message-ID | <mailman.3572.1363804006.2939.python-list@python.org> |
| In reply to | #41610 |
[Multipart message — attachments visible in raw view] — view raw
On Wed, Mar 20, 2013 at 2:15 PM, Ana Dionísio <anadionisio257@gmail.com>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: > You test i, but you don't set i to anything, or change it in your loop you may want to try for i in range(25) (or perhaps range(1,26) i'm guessing. Be more clear > > if i==t[]: > This isn't legal syntax. Try googling about for loops and sets. Try again and come back > > print "Test1" > > else: > > print "Test2" > > What is missing here for this script work? > > Thank you all > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-03-20 13:36 -0500 |
| Message-ID | <mailman.3573.1363804459.2939.python-list@python.org> |
| In reply to | #41610 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-20 18:40 +0000 |
| Message-ID | <514a02a4$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41610 |
On Wed, 20 Mar 2013 11:15:27 -0700, 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:
Wouldn't it make more sense to print "Equal" and "Not equal"?
If all you want to do is check whether some value i can be found in the
list, you can do this:
if i in t:
print "Found"
else:
print "Not found"
If you want to find the index of where a value is found, use the index
method:
x = 10
t.index(x) # returns 4
> while i<=25:
> if i==t[]:
> print "Test1"
> else:
> print "Test2"
>
> What is missing here for this script work?
Lots of things. What's i? Does it ever change, or is it a constant? What
are you comparing it to?
Taking a wild guess at what you mean, you could try this:
for i in range(25):
print i, "found" if i in t else "not found"
Does this help?
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-03-20 20:39 +0000 |
| Message-ID | <kid6pj$aua$1@reader2.panix.com> |
| In reply to | #41610 |
In <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> =?ISO-8859-1?Q?Ana_Dion=EDsio?= <anadionisio257@gmail.com> writes:
> 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?
1. You're missing an initial value for i.
2. You never increment i, so the while loop will never exit.
3. The syntax 'if i==t[]' is wrong.
4. The way you have this code set up, you would need two loops: an outer
loop for i from 1 to 25, and an inner loop for the items in t.
5. As others have said, this is a poor way to go about it. You want to
use "if i in t".
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web