Re: Question on for loop

2013-03-05 Thread Bryan Devaney
On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote:
 On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney bryan.deva...@gmail.com wrote:
 
  if character not in lettersGuessed:
 
 
 
  return True
 
 
 
  return False
 
 
 
  assuming a function is being used to pass each letter of the letters 
  guessed inside a loop itself that only continues checking if true is 
  returned, then that could work.
 
 
 
  It is however more work than is needed.
 
 
 
  If you made secretword a list,you could just
 
 
 
  set(secretword)set(lettersguessed)
 
 
 
  and check the result is equal to secretword.
 
 
 
 Check the result is equal to set(secretword), I think you mean.
 
 
 
 set(secretword).issubset(set(lettersguessed))
 
 
 
 might be slightly more efficient, since it would not need to build and
 
 return an intersection set.
 
 
 
 One might also just do:
 
 
 
 all(letter in lettersguessed for letter in secretword)
 
 
 
 Which will be efficient if lettersguessed is already a set.

You are correct. sorry for the misleading answer, was digging through old shell 
scripts all day yesterday and brain was obviously not not the better for it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Question on for loop

2013-03-04 Thread newtopython
Hi all,

I'm super new to python, just fyi.

In the piece of code below, secretWord is a string and lettersGuessed is a 
list. I'm trying to find out if ALL the characters of secretWord are included 
in lettersGuessed, even if there are additional values in the lettersGuessed 
list that aren't in secretWord. 

What this code is doing is only checking the first character of secretWord and 
then returning True or False. How do I get it to iterate through ALL of the 
characters of secretWord?

for character in secretWord:
if character not in lettersGuessed:
return True
return False

Thanks!

Ro
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread leo kirotawa
In fact this code is already doing what you want, but if the second
character, by example, is not in secrectWord it'll jump out of the for and
return. If you want that interact through the all characters and  maybe
count how many them are in the secrectWord, just take of the return there
or do some list comprehension like: [ r for r in secrecWord if r in
lettersGuessed] .



[]'s



On Mon, Mar 4, 2013 at 9:18 AM, newtopython roshen.set...@gmail.com wrote:

 Hi all,

 I'm super new to python, just fyi.

 In the piece of code below, secretWord is a string and lettersGuessed is a
 list. I'm trying to find out if ALL the characters of secretWord are
 included in lettersGuessed, even if there are additional values in the
 lettersGuessed list that aren't in secretWord.

 What this code is doing is only checking the first character of secretWord
 and then returning True or False. How do I get it to iterate through ALL of
 the characters of secretWord?

 for character in secretWord:
 if character not in lettersGuessed:
 return True
 return False

 Thanks!

 Ro
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Leônidas S. Barbosa (Kirotawa)

Engenheiro de Software - IBM (LTC - Linux Technology Center)
MsC Sistemas e Computação
Bacharel em Ciências da Computação.

blog nerd: corecode.wordpress.com/

http://corecode.wordpress.com/User linux : #480879

Mais sábio é aquele que sabe que não sabe (Sócrates)
smile and wave - =D + o/ (Penguins of Madagascar)

日本語の学生です。
コンピュータサイエンスの学位.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Joel Goldstick
On Mon, Mar 4, 2013 at 7:18 AM, newtopython roshen.set...@gmail.com wrote:

 Hi all,

 I'm super new to python, just fyi.


Welcome.  Next time write a better subject line, and be sure the code you
post is actually the code you are running.  Provide the results you want
and what you get.  Provide the traceback if there is one


 In the piece of code below, secretWord is a string and lettersGuessed is a
 list. I'm trying to find out if ALL the characters of secretWord are
 included in lettersGuessed, even if there are additional values in the
 lettersGuessed list that aren't in secretWord.

 What this code is doing is only checking the first character of secretWord
 and then returning True or False. How do I get it to iterate through ALL of
 the characters of secretWord?

 for character in secretWord:
 if character not in lettersGuessed:


I am guessing that the next two lines are actually indented in your script
so I am changing them here


return True
 return False

 The first time your if block is checked it will return True or False.
Since you haven't shown this code in a function, as written it won't run at
all.

Your question makes no sense.  What would it mean to look through each
character and return True or False?  What would make the result True?  All
matches, some matches?

 Thanks!

 Ro
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Dave Angel

On 03/04/2013 07:18 AM, newtopython wrote:

Hi all,

I'm super new to python, just fyi.


Welcome to the Python list.



In the piece of code below, secretWord is a string and lettersGuessed is a 
list. I'm trying to find out if ALL the characters of secretWord are included 
in lettersGuessed, even if there are additional values in the lettersGuessed 
list that aren't in secretWord.

What this code is doing is only checking the first character of secretWord and 
then returning True or False. How do I get it to iterate through ALL of the 
characters of secretWord?

for character in secretWord:
 if character not in lettersGuessed:
 return True
return False



Please post a complete sample when possible, and make sure you 
copy/paste it, not just retype it and hope.  As written, it'll throw an 
exception when return is encountered.  But before that, it'll complain 
about the indentation of the return True.


Perhaps you have something like:

def has_some_behavior(secretWord, lettersGuessed):
for character in secretWord:
if character not in lettersGuessed:
return True
return False

If so, please copy the whole thing from your code, and explain just how 
you call it (what arguments are passed), what it returned, and what's 
wrong with that behavior.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Bryan Devaney
 if character not in lettersGuessed:
 
 return True
 
 return False

assuming a function is being used to pass each letter of the letters guessed 
inside a loop itself that only continues checking if true is returned, then 
that could work.

It is however more work than is needed. 

If you made secretword a list,you could just 

set(secretword)set(lettersguessed) 

and check the result is equal to secretword.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Rick Johnson
On Monday, March 4, 2013 6:18:20 AM UTC-6, newtopython wrote:

[Note: Post has be logically re-arranged for your comprehensive pleasures]

 for character in secretWord:
 if character not in lettersGuessed:
 return True
 return False
 
 What this code is doing is only checking the first
 character of secretWord and then returning True or False.
 How do I get it to iterate through ALL of the characters
 of secretWord?

Your code is a fine example of:  attempting to solve too many problems at the 
same time. If you are having trouble understanding how to iterate over a 
sequence, then why would you complicate that learning experience by injecting 
other unsolved problems into the mix? First, solve the iteration problem. Then 
expand.

 ## START INTERACTIVE SESSION ##
 py s = 'multiplicity'
 py for char in s:
 ... print char 
 m
 u
 l
 t
 i
 p
 l
 i
 c
 i
 t
 y
 ## END INTERACTIVE SESSION ##

Now, we have a simple base from which to build! 

 In the piece of code [ABOVE], secretWord is a string and
 lettersGuessed is a list. I'm trying to find out if ALL
 the characters of secretWord are included in
 lettersGuessed, even if there are additional values in the
 lettersGuessed list that aren't in secretWord.

First, step away from your interpreter! Now, grab a pen and paper and write 
down the steps required to compare two sequences in real life.

1. Create a list of letters guessed and a string representing the secret 
word.

secretWord = 'multiplicity'
lettersGuessed = 'aeiouy'

2. For each letter in secretWord, look in lettersGuessed and see if you can 
find the letter, then make a note of your findings. If the letter is in IN both 
sequences, write [letter]=True, if not, write [letter]=False.

However, this algorithm is rather naive. What happens if one or both list 
contain the same letter numerous times (f.e. multiplicity has 3 i chars)? 
Do we want the user to provide a guess for all three i chars, or will just a 
single guess al la price is right will do the trick? 

Also, do we care about the char order? Or are we merely allowing the user to 
guess all the letters of the word in ANY order (that seems to be your intent 
here!)?

In any event i am not going to just gift wrap and answer for you. There are 
many methods of solving this problem, some are elegant, some or not elegant, 
some use built-in functions, some use list comprehensions, and some could just 
use a for loop and a single built-in function. I would highly suggest that you 
figure this out using the latter. Until you can achieve this, forget about list 
comprehension or any advanced stuff. 

But most importantly: Build your code in small incremental steps and solve ONE 
issue at a time. This is the path of a wise problem solver.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Ian Kelly
On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney bryan.deva...@gmail.com wrote:
 if character not in lettersGuessed:

 return True

 return False

 assuming a function is being used to pass each letter of the letters guessed 
 inside a loop itself that only continues checking if true is returned, then 
 that could work.

 It is however more work than is needed.

 If you made secretword a list,you could just

 set(secretword)set(lettersguessed)

 and check the result is equal to secretword.

Check the result is equal to set(secretword), I think you mean.

set(secretword).issubset(set(lettersguessed))

might be slightly more efficient, since it would not need to build and
return an intersection set.

One might also just do:

all(letter in lettersguessed for letter in secretword)

Which will be efficient if lettersguessed is already a set.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-03-04 Thread Ricardo Aráoz

El 04/03/13 09:18, newtopython escribió:

Hi all,

I'm super new to python, just fyi.

In the piece of code below, secretWord is a string and lettersGuessed is a 
list. I'm trying to find out if ALL the characters of secretWord are included 
in lettersGuessed, even if there are additional values in the lettersGuessed 
list that aren't in secretWord.

What this code is doing is only checking the first character of secretWord and 
then returning True or False. How do I get it to iterate through ALL of the 
characters of secretWord?

for character in secretWord:
 if character not in lettersGuessed:
 return True
return False

Thanks!

Ro


Indent the return True line so that it is inside the if clause.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-15 Thread subhabangalore
On Friday, January 4, 2013 11:18:24 AM UTC+5:30, Steven D'Aprano wrote:
 On Thu, 03 Jan 2013 12:04:03 -0800, subhabangalore wrote:
 
 
 
  Dear Group,
 
  If I take a list like the following:
 
  
 
  fruits = ['banana', 'apple',  'mango'] 
 
  for fruit in fruits:
 
 print 'Current fruit :', fruit
 
  
 
  Now,
 
  if I want variables like var1,var2,var3 be assigned to them, we may
 
  take, var1=banana,
 
  var2=apple,
 
  var3=mango
 
  
 
  but can we do something to assign the variables dynamically
 
 
 
 Easy as falling off a log. You can't write var1, var2 etc. but you 
 
 can write it as var[0], var[1] etc.
 
 
 
 var = ['banana', 'apple',  'mango'] 
 
 print var[0]  # prints 'banana'
 
 print var[1]  # prints 'apple'
 
 print var[2]  # prints 'mango'
 
 
 
 
 
 
 
 Of course var is not a very good variable name. fruit or fruits 
 
 would be better.
 
 
 
 
 
 
 
 
 
 -- 
 
 Steven

Actually in many cases it is easy if you get the variable of list value, I was 
trying something like,
def func1(n):
list1=[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]
blnk=[]
for i in range(len(list1)):
num1=var+str(i)+=+list1[i]
blnk.append(num1)
print blnk
Regards,
Subhabrata. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-04 Thread Alister
On Thu, 03 Jan 2013 12:04:03 -0800, subhabangalore wrote:

 Dear Group,
 If I take a list like the following:
 
 fruits = ['banana', 'apple',  'mango']
 for fruit in fruits:
print 'Current fruit :', fruit
 
 Now,
 if I want variables like var1,var2,var3 be assigned to them, we may
 take, var1=banana,
 var2=apple,
 var3=mango
 
 but can we do something to assign the variables dynamically I was
 thinking of var_series=['var1','var2','var3']
 for var in var_series:
   for fruit in fruits:
print var,fruits
 
 If any one can kindly suggest.
 
 Regards,
 Subhabrata
 
 NB: Apology for some alignment mistakes,etc.

if you really want to do this ( I agree with the other replies that this 
is unlikely to be a good idea) then you could simply unpack the list

var1,var2,var3=fruits

of course if your list is of unknown length then this again becomes 
impractical.

for most programming requirements there is a simple solution, if you find 
your approach is not easily implemented it is probably a good time to re-
asses your approach, more of the than not you have been given a bum steer 
and are heading down the wrong road.

See http://thedailywtf.com for an almost limitless supply of examples of 
programmers continuing down the wrong road ;-) 

-- 
There's nothing worse for your business than extra Santa Clauses
smoking in the men's room.
-- W. Bossert
-- 
http://mail.python.org/mailman/listinfo/python-list


Question on for loop

2013-01-03 Thread subhabangalore
Dear Group,
If I take a list like the following:

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:
   print 'Current fruit :', fruit

Now, 
if I want variables like var1,var2,var3 be assigned to them, we may take,
var1=banana,
var2=apple,
var3=mango

but can we do something to assign the variables dynamically I was thinking
of 
var_series=['var1','var2','var3']
for var in var_series:
  for fruit in fruits:
   print var,fruits

If any one can kindly suggest.

Regards,
Subhabrata

NB: Apology for some alignment mistakes,etc.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread MRAB

On 2013-01-03 20:04, subhabangal...@gmail.com wrote:

Dear Group,
If I take a list like the following:

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:
print 'Current fruit :', fruit

Now,
if I want variables like var1,var2,var3 be assigned to them, we may take,
var1=banana,
var2=apple,
var3=mango

but can we do something to assign the variables dynamically I was thinking
of
var_series=['var1','var2','var3']
for var in var_series:
   for fruit in fruits:
print var,fruits

If any one can kindly suggest.

Regards,
Subhabrata

NB: Apology for some alignment mistakes,etc.


Why would you want to do that? Creating names dynamically like that is
a bad idea. Just keep them in a list, like they are already.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread Peter Otten
subhabangal...@gmail.com wrote:

 Dear Group,
 If I take a list like the following:
 
 fruits = ['banana', 'apple',  'mango']
 for fruit in fruits:
print 'Current fruit :', fruit
 
 Now,
 if I want variables like var1,var2,var3 be assigned to them, we may take,
 var1=banana,
 var2=apple,
 var3=mango
 
 but can we do something to assign the variables dynamically I was thinking
 of
 var_series=['var1','var2','var3']
 for var in var_series:
   for fruit in fruits:
print var,fruits
 
 If any one can kindly suggest.

For that problem you need another data structure -- a dictionary:

 lookup_fruits = {var1: banana, var2: apple, var3: mango}
 var_series = [var1, var2, var3]
 for var in var_series:
... print var, lookup_fruits[var]
... 
var1 banana
var2 apple
var3 mango


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread Matt Jones
Yeah, this seems like a bad idea.  What exactly are you trying to do here?

Maybe using a dictionary is what you want?


d = {
'first' : 'banana',
'second' : 'apple',
'third'  : 'mango'
}

for key, value in d.items():
print key, value


However I'm still not sure why you'd want to do this.

*Matt Jones*


On Thu, Jan 3, 2013 at 2:21 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-01-03 20:04, subhabangal...@gmail.com wrote:

 Dear Group,
 If I take a list like the following:

 fruits = ['banana', 'apple',  'mango']
 for fruit in fruits:
 print 'Current fruit :', fruit

 Now,
 if I want variables like var1,var2,var3 be assigned to them, we may take,
 var1=banana,
 var2=apple,
 var3=mango

 but can we do something to assign the variables dynamically I was thinking
 of
 var_series=['var1','var2','**var3']
 for var in var_series:
for fruit in fruits:
 print var,fruits

 If any one can kindly suggest.

 Regards,
 Subhabrata

 NB: Apology for some alignment mistakes,etc.

  Why would you want to do that? Creating names dynamically like that is
 a bad idea. Just keep them in a list, like they are already.
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread Don Ross
I'm interested to know why you're trying this as well.  Is this something that 
would be helped by creating a class and then dynamically creating instances of 
that class?  Something like...

class Fruit:
def __init__(self, name):
self.name = name

for fruit in ['banana', 'apple', 'mango']:
varName = Fruit(fruit)
# do stuff with varName

On Thursday, January 3, 2013 2:04:03 PM UTC-6, subhaba...@gmail.com wrote:
 Dear Group,
 
 If I take a list like the following:
 
 
 
 fruits = ['banana', 'apple',  'mango']
 
 for fruit in fruits:
 
print 'Current fruit :', fruit
 
 
 
 Now, 
 
 if I want variables like var1,var2,var3 be assigned to them, we may take,
 
 var1=banana,
 
 var2=apple,
 
 var3=mango
 
 
 
 but can we do something to assign the variables dynamically I was thinking
 
 of 
 
 var_series=['var1','var2','var3']
 
 for var in var_series:
 
   for fruit in fruits:
 
print var,fruits
 
 
 
 If any one can kindly suggest.
 
 
 
 Regards,
 
 Subhabrata
 
 
 
 NB: Apology for some alignment mistakes,etc.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread alex23
On Jan 4, 6:04 am, subhabangal...@gmail.com wrote:
 but can we do something to assign the variables dynamically I was thinking
 of
 var_series=['var1','var2','var3']
 for var in var_series:
   for fruit in fruits:
        print var,fruits

Before trying to do this, write the next bit of code where you _use_
such variables. What do you do if there are no fruits? What do you do
if there are 7000?

You don't want variables to be optional, because otherwise you'll need
to guard every usage with something like:

if 'var2893' in locals(): ...

Of course, you can also automate this, but why push values into a
dictionary that exists for one purpose if you're not going to use it
that way?

If you need to deal with an unknown number of objects, use a list. If
those objects have a name by which you can refer to them, use a
dictionary.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on for loop

2013-01-03 Thread Steven D'Aprano
On Thu, 03 Jan 2013 12:04:03 -0800, subhabangalore wrote:

 Dear Group,
 If I take a list like the following:
 
 fruits = ['banana', 'apple',  'mango'] 
 for fruit in fruits:
print 'Current fruit :', fruit
 
 Now,
 if I want variables like var1,var2,var3 be assigned to them, we may
 take, var1=banana,
 var2=apple,
 var3=mango
 
 but can we do something to assign the variables dynamically

Easy as falling off a log. You can't write var1, var2 etc. but you 
can write it as var[0], var[1] etc.

var = ['banana', 'apple',  'mango'] 
print var[0]  # prints 'banana'
print var[1]  # prints 'apple'
print var[2]  # prints 'mango'



Of course var is not a very good variable name. fruit or fruits 
would be better.




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Question about nested loop

2012-12-31 Thread Isaac Won
Hi all,
I am a very novice for Python. Currently, I am trying to read continuous 
columns repeatedly in the form of array. 
my code is like below:

import numpy as np

b = []


c = 4
f = open(text.file, r)


while c  10:
c = c + 1

for  columns in ( raw.strip().split() for raw in f ):


b.append(columns[c])

y = np.array(b, float)
print c, y


I thought that  can get the arrays of the columns[5] to [10], but I only could 
get repetition of same arrays of columns[5].

The result was something like:

5 [1 2 3 4 .., 10 9 8]
6 [1 2 3 4 .., 10 9 8]
7 [1 2 3 4 .., 10 9 8]
8 [1 2 3 4 .., 10 9 8]
9 [1 2 3 4 .., 10 9 8]
10 [1 2 3 4 .., 10 9 8]


What I can't understand is that even though c increased incrementally upto 10, 
y arrays stay same.

Would someone help me to understand this problem more?

I really appreciate any help.

Thank you,

Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about nested loop

2012-12-31 Thread Gisle Vanem

Isaac Won winef...@gmail.com wrote:


while c  10:
   c = c + 1

   for  columns in ( raw.strip().split() for raw in f ):


   b.append(columns[c])

   y = np.array(b, float)
   print c, y


I thought that  can get the arrays of the columns[5] to [10],
but I only could get repetition of same arrays of columns[5].


I don't pretend to know list comprehension very well, but 
'c' isn't incremented in the inner loop ( .. for raw in f). 
Hence you only append to columns[5].


Maybe you could use another 'd' indexer inside the inner-loop?
But there must a more elegant way to solve your issue. (I'm a
PyCommer myself).

--gv
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question about nested loop

2012-12-31 Thread Hans Mulder
On 31/12/12 11:02:56, Isaac Won wrote:
 Hi all,
 I am a very novice for Python. Currently, I am trying to read continuous
 columns repeatedly in the form of array. 
 my code is like below:
 
 import numpy as np
 
 b = [] 
 c = 4
 f = open(text.file, r)
 
 while c  10:
 c = c + 1
 
 for  columns in ( raw.strip().split() for raw in f ):
 b.append(columns[c])
 
 y = np.array(b, float)
 print c, y
 
 
 I thought that  can get the arrays of the columns[5] to [10], but I only
 could get repetition of same arrays of columns[5].
 
 The result was something like:
 
 5 [1 2 3 4 .., 10 9 8]
 6 [1 2 3 4 .., 10 9 8]
 7 [1 2 3 4 .., 10 9 8]
 8 [1 2 3 4 .., 10 9 8]
 9 [1 2 3 4 .., 10 9 8]
 10 [1 2 3 4 .., 10 9 8]
 
 
 What I can't understand is that even though c increased incrementally upto 10,
 y arrays stay same.
 
 Would someone help me to understand this problem more?

That's because the inner loop read from a file until his reaches
the end of the file.  Since you're not resetting the file pointer,
during the second and later runs of the outer loop, the inner loop
starts at the end of the file and terminates without any action.

You'd get more interesting results if you rewind the file:

import numpy as np

b = []
c = 4
f = open(text.file, r)

while c  10:
c = c + 1

f.seek(0,0)
for  columns in ( raw.strip().split() for raw in f ):
b.append(columns[c])

y = np.array(b, float)
print c, y

It's a bit inefficient to read the same file several times.
You might consider reading it just once.  For example:

import numpy as np

b = []

f = open(text.file, r)
data = [ line.strip().split() for line in f ]
f.close()

for c in xrange(5, 11):
for row in data:
b.append(row[c])

y = np.array(b, float)
print c, y


Hope this helps,

-- HansM



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about nested loop

2012-12-31 Thread Isaac Won
On Monday, December 31, 2012 5:25:16 AM UTC-6, Gisle Vanem wrote:
 Isaac Won winef...@gmail.com wrote:
 
 
 
  while c  10:
 
 c = c + 1
 
  
 
 for  columns in ( raw.strip().split() for raw in f ):
 
  
 
  
 
 b.append(columns[c])
 
  
 
 y = np.array(b, float)
 
 print c, y
 
  
 
  
 
  I thought that  can get the arrays of the columns[5] to [10],
 
  but I only could get repetition of same arrays of columns[5].
 
 
 
 I don't pretend to know list comprehension very well, but 
 
 'c' isn't incremented in the inner loop ( .. for raw in f). 
 
 Hence you only append to columns[5].
 
 
 
 Maybe you could use another 'd' indexer inside the inner-loop?
 
 But there must a more elegant way to solve your issue. (I'm a
 
 PyCommer myself).
 
 
 
 --gv

Thank you for your advice.
 I agree with you and tried to increment in inner loop, but still not very 
succesful. Anyway many thanks for you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about nested loop

2012-12-31 Thread Isaac Won
On Monday, December 31, 2012 6:59:34 AM UTC-6, Hans Mulder wrote:
 On 31/12/12 11:02:56, Isaac Won wrote:
 
  Hi all,
 
  I am a very novice for Python. Currently, I am trying to read continuous
 
  columns repeatedly in the form of array. 
 
  my code is like below:
 
  
 
  import numpy as np
 
  
 
  b = [] 
 
  c = 4
 
  f = open(text.file, r)
 
  
 
  while c  10:
 
  c = c + 1
 
  
 
  for  columns in ( raw.strip().split() for raw in f ):
 
  b.append(columns[c])
 
  
 
  y = np.array(b, float)
 
  print c, y
 
  
 
  
 
  I thought that  can get the arrays of the columns[5] to [10], but I only
 
  could get repetition of same arrays of columns[5].
 
  
 
  The result was something like:
 
  
 
  5 [1 2 3 4 .., 10 9 8]
 
  6 [1 2 3 4 .., 10 9 8]
 
  7 [1 2 3 4 .., 10 9 8]
 
  8 [1 2 3 4 .., 10 9 8]
 
  9 [1 2 3 4 .., 10 9 8]
 
  10 [1 2 3 4 .., 10 9 8]
 
  
 
  
 
  What I can't understand is that even though c increased incrementally upto 
  10,
 
  y arrays stay same.
 
  
 
  Would someone help me to understand this problem more?
 
 
 
 That's because the inner loop read from a file until his reaches
 
 the end of the file.  Since you're not resetting the file pointer,
 
 during the second and later runs of the outer loop, the inner loop
 
 starts at the end of the file and terminates without any action.
 
 
 
 You'd get more interesting results if you rewind the file:
 
 
 
 import numpy as np
 
 
 
 b = []
 
 c = 4
 
 f = open(text.file, r)
 
 
 
 while c  10:
 
 c = c + 1
 
 
 
 f.seek(0,0)
 
 for  columns in ( raw.strip().split() for raw in f ):
 
 b.append(columns[c])
 
 
 
 y = np.array(b, float)
 
 print c, y
 
 
 
 It's a bit inefficient to read the same file several times.
 
 You might consider reading it just once.  For example:
 
 
 
 import numpy as np
 
 
 
 b = []
 
 
 
 f = open(text.file, r)
 
 data = [ line.strip().split() for line in f ]
 
 f.close()
 
 
 
 for c in xrange(5, 11):
 
 for row in data:
 
 b.append(row[c])
 
 
 
 y = np.array(b, float)
 
 print c, y
 
 
 
 
 
 Hope this helps,
 
 
 
 -- HansM

Hi Hans,

I appreciate your advice and kind tips.

The both codes which you gave seem pretty interesting.

Both look working for incrementing inner loop number, but the results of y are 
added repeatedly such as [1,2,3],[1,2,3,4,5,6], [1,2,3,4,5,6,7,8,9]. Anyhow, 
really thank you for your help and I will look at this problem more in detail.

Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-12 Thread Jason Stokes

jmDesktop [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

 So what is n and x in the first iteration?  Sorry.  I'm trying.

Remember how Python's range operator works.  range(n, x) constructs a list 
that consists of all elements starting with n and up to, but /not 
including/, x.  For example, range(3, 7) constructs the list [3, 4, 5, 6].

So what happens if you try to construct the list range(2, 2)?  In this case, 
Python will construct an empty list -- this is its interpretation of up to, 
but not including n for an argument like range(n, n).  This is precisely 
what is happening in the first iteration of the first enclosing for loop. 
Trace through the code; see that 2 is bound to n in the first iteration; see 
that the inner loop then tries to construct the list range(2, n), which is 
range(2, 2), which is an empty list.  And a for x in [] statement will not 
execute even once.

As it happens, your loop is perfectly correct.  You are testing for divisors 
for a number excluding 1 and the number itself.  For the number 2, the 
number of possible divisors satisfying this condition is an empty set.  So 2 
is, quite correctly, adjudged to be prime. 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-12 Thread Steve Holden
jmDesktop wrote:
[...]
 So what is n and x in the first iteration?  Sorry.  I'm trying.

Somewhat feebly, if you don't mind my saying so, but don't worry.

The usual way to proceed in the face of such ignorance is to insert some 
form of output that will tell you the answer to your question.

So:

  for n in range(2, 20):
... print range(2, n)
... for x in range(2, n):
... if n % x == 0:
...print n, 'equals', x, '*', n/x
...break
... else:
... print n, is prime
...
[]
2 is prime
[2]
3 is prime
[2, 3]
4 equals 2 * 2
[2, 3, 4]
5 is prime
[2, 3, 4, 5]
6 equals 2 * 3
[2, 3, 4, 5, 6]
7 is prime
[2, 3, 4, 5, 6, 7]
8 equals 2 * 4
[2, 3, 4, 5, 6, 7, 8]
9 equals 3 * 3

and so on! This is the value of the interactive interpreter.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


basic python question about for loop

2008-04-09 Thread jmDesktop
From the Python.org tutorial:

 for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

http://www.python.org/doc/current/tut/node6.html#SECTION00630

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread Diez B. Roggisch
jmDesktop schrieb:
 From the Python.org tutorial:
 
 for n in range(2, 10):
 ... for x in range(2, n):
 ... if n % x == 0:
 ... print n, 'equals', x, '*', n/x
 ... break
 ... else:
 ... # loop fell through without finding a factor
 ... print n, 'is a prime number'
 ...
 2 is a prime number
 3 is a prime number
 4 equals 2 * 2
 5 is a prime number
 6 equals 2 * 3
 7 is a prime number
 8 equals 2 * 4
 9 equals 3 * 3
 
 
 
 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
 Why did it fall through?

print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.


Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: basic python question about for loop

2008-04-09 Thread Reedick, Andrew


 -Original Message-
 From: [EMAIL PROTECTED] [mailto:python-
 [EMAIL PROTECTED] On Behalf Of jmDesktop
 Sent: Wednesday, April 09, 2008 4:51 PM
 To: python-list@python.org
 Subject: basic python question about for loop
 
 From the Python.org tutorial:
 
  for n in range(2, 10):
 ... for x in range(2, n):
 ... if n % x == 0:
 ... print n, 'equals', x, '*', n/x
 ... break
 ... else:
 ... # loop fell through without finding a factor
 ... print n, 'is a prime number'
 ...
 2 is a prime number
 3 is a prime number
 4 equals 2 * 2
 5 is a prime number
 6 equals 2 * 3
 7 is a prime number
 8 equals 2 * 4
 9 equals 3 * 3
 
 
 
 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
 Why did it fall through?
 

a) 2 is prime, so nothing is wrong.

b) Range isn't doing what you think it's doing:

 print range(2,2)
[]
 print range(2,3)
[2]
 print range(2,4)
[2, 3]
 print range(2,5)
[2, 3, 4]

 print range(1,1)
[]
 print range(1,2)
[1]
 print range(1,3)
[1, 2]




*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread Steve Holden
jmDesktop wrote:
From the Python.org tutorial:
 
 for n in range(2, 10):
 ... for x in range(2, n):
 ... if n % x == 0:
 ... print n, 'equals', x, '*', n/x
 ... break
 ... else:
 ... # loop fell through without finding a factor
 ... print n, 'is a prime number'
 ...
 2 is a prime number
 3 is a prime number
 4 equals 2 * 2
 5 is a prime number
 6 equals 2 * 3
 7 is a prime number
 8 equals 2 * 4
 9 equals 3 * 3
 
 
 
 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
 Why did it fall through?
 
  range(2, 2)
[]
 

The loop body executes zero times.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread jmDesktop
On Apr 9, 4:58 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 jmDesktop schrieb:





  From the Python.org tutorial:

  for n in range(2, 10):
  ...     for x in range(2, n):
  ...         if n % x == 0:
  ...             print n, 'equals', x, '*', n/x
  ...             break
  ...     else:
  ...         # loop fell through without finding a factor
  ...         print n, 'is a prime number'
  ...
  2 is a prime number
  3 is a prime number
  4 equals 2 * 2
  5 is a prime number
  6 equals 2 * 3
  7 is a prime number
  8 equals 2 * 4
  9 equals 3 * 3

  first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
  Why did it fall through?

 print out what range(2, n) for n == 2 is.

 And if you didn't know - 2 *IS* a prime.

 Diez- Hide quoted text -

 - Show quoted text -

I do not understand.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread jmDesktop
On Apr 9, 4:59 pm, Reedick, Andrew [EMAIL PROTECTED] wrote:
  -Original Message-
  From: [EMAIL PROTECTED] [mailto:python-
  [EMAIL PROTECTED] On Behalf Of jmDesktop
  Sent: Wednesday, April 09, 2008 4:51 PM
  To: [EMAIL PROTECTED]
  Subject: basic python question about for loop

  From the Python.org tutorial:

   for n in range(2, 10):
  ...     for x in range(2, n):
  ...         if n % x == 0:
  ...             print n, 'equals', x, '*', n/x
  ...             break
  ...     else:
  ...         # loop fell through without finding a factor
  ...         print n, 'is a prime number'
  ...
  2 is a prime number
  3 is a prime number
  4 equals 2 * 2
  5 is a prime number
  6 equals 2 * 3
  7 is a prime number
  8 equals 2 * 4
  9 equals 3 * 3

  first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
  Why did it fall through?

 a) 2 is prime, so nothing is wrong.

 b) Range isn't doing what you think it's doing:

  print range(2,2)
 []
  print range(2,3)
 [2]
  print range(2,4)
 [2, 3]
  print range(2,5)

 [2, 3, 4]



  print range(1,1)
 []
  print range(1,2)
 [1]
  print range(1,3)
 [1, 2]

 *

 The information transmitted is intended only for the person or entity to 
 which it is addressed and may contain confidential, proprietary, and/or 
 privileged material. Any review, retransmission, dissemination or other use 
 of, or taking of any action in reliance upon this information by persons or 
 entities other than the intended recipient is prohibited. If you received 
 this in error, please contact the sender and delete the material from all 
 computers. GA622- Hide quoted text -

 - Show quoted text -

So what is n and x in the first iteration?  Sorry.  I'm trying.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: basic python question about for loop

2008-04-09 Thread Reedick, Andrew


 -Original Message-
 From: [EMAIL PROTECTED] [mailto:python-
 [EMAIL PROTECTED] On Behalf Of jmDesktop
 Sent: Wednesday, April 09, 2008 5:04 PM
 To: python-list@python.org
 Subject: Re: basic python question about for loop
 
 
for n in range(2, 10):
   ...     for x in range(2, n):
   ...         if n % x == 0:
   ...             print n, 'equals', x, '*', n/x
   ...             break
   ...     else:
   ...         # loop fell through without finding a factor
   ...         print n, 'is a prime number'
   ...
 
   first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
   Why did it fall through?
 

 
 So what is n and x in the first iteration?  Sorry.  I'm trying.


You're never getting to n and x in the first iteration, because the 'for x in 
range(2, n)' loop isn't looping.

This:  
for x in range(2, n)
is equivalent in C/Perl/etc. to:
for(x=2; xn; x++)
which for the first iteration is:
for(x=2; x2; x++)

Since (2  2) is false, you never call 'if n %x == 0:' in the first iteration.

Or to put it another way:
Range(2, n) starts at 2, and stops _before_ n.
Range(2, n) starts at 2, and stops _before_ 2.




*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread Diez B. Roggisch
jmDesktop schrieb:
 On Apr 9, 4:58 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 jmDesktop schrieb:





 From the Python.org tutorial:
 for n in range(2, 10):
 ... for x in range(2, n):
 ... if n % x == 0:
 ... print n, 'equals', x, '*', n/x
 ... break
 ... else:
 ... # loop fell through without finding a factor
 ... print n, 'is a prime number'
 ...
 2 is a prime number
 3 is a prime number
 4 equals 2 * 2
 5 is a prime number
 6 equals 2 * 3
 7 is a prime number
 8 equals 2 * 4
 9 equals 3 * 3
 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
 Why did it fall through?
 print out what range(2, n) for n == 2 is.

 And if you didn't know - 2 *IS* a prime.

 Diez- Hide quoted text -

 - Show quoted text -
 
 I do not understand.

for variable in sequence loops over a sequence. And of course it 
doens't if the sequence is empty, because you can't loop over something 
that is empty, can't you?

and range(2,2) is the empty sequence.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python question about for loop

2008-04-09 Thread Terry Reedy
|So what is n and x in the first iteration?  Sorry.  I'm trying.

When n == 2, the inner loop executes 0 times (the length of range(2,n)) and 
then falls thru to the else clause, printing the correct answer. 



-- 
http://mail.python.org/mailman/listinfo/python-list


Question about 'for' loop

2007-08-17 Thread Robert Dailey
Hi,

I noticed that the 'for' loop can be used inline with a list definition. For
example:

print [i for i in mylist]

My first question is what is the name for this? I couldn't find this usage
in the python docs; I only managed to learn about it through code samples on
the internet.

Secondly, I'm wondering how I can use this method of a for loop to append
strings to strings in a list. For example:

mylist = [
Hello ,
Hello again 
]

I should be able to do this:

print [ i + World for i in mylist ]

Which should yield the output:

[Hello World, Hello again world]

However, instead I get an error message saying TypeError: cannot
concatenate 'str' and 'list' objects

How can I achieve the above? Thanks for reading.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Question about 'for' loop

2007-08-17 Thread Steve Holden
Robert Dailey wrote:
 Hi,
 
 I noticed that the 'for' loop can be used inline with a list definition. 
 For example:
 
 print [i for i in mylist]
 
 My first question is what is the name for this? I couldn't find this 
 usage in the python docs; I only managed to learn about it through code 
 samples on the internet.
 
That there is a list comprehension.

 Secondly, I'm wondering how I can use this method of a for loop to 
 append strings to strings in a list. For example:
 
 mylist = [
 Hello ,
 Hello again 
 ]
 
 I should be able to do this:
 
 print [ i + World for i in mylist ]
 
 Which should yield the output:
 
 [Hello World, Hello again world]
 
Who says you should? Beside you, that is. I am afraid the interpreter 
isn't psychic, and it doesn't have a DWIM [1] mode.

 However, instead I get an error message saying TypeError: cannot 
 concatenate 'str' and 'list' objects
 
 How can I achieve the above? Thanks for reading.
 
It's just a matter of understanding the syntax in a little more depth. 
In a week's time it will be blindingly obvious.

  nicknames = [bozo, newbie, newless cloob, and welcome to 
c.l.py]
  [(hello  + name) for name in nicknames]
['hello bozo', 'hello newbie', 'hello newless cloob', 'hello and welcome 
to c.l.py']
 

So, treat item [-1] from that list as the real sentiment of this message:

  [(hello  + name) for name in nicknames][-1]
'hello and welcome to c.l.py'

regards
  Steve

[1]: Do What I Mean [and never mind what I say ...]
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about 'for' loop

2007-08-17 Thread Carsten Haese
On Fri, 2007-08-17 at 17:45 -0500, Robert Dailey wrote:
 [...]
 Secondly, I'm wondering how I can use this method of a for loop to
 append strings to strings in a list. For example:
 
 mylist = [
 Hello ,
 Hello again 
 ]
 
 I should be able to do this: 
 
 print [ i + World for i in mylist ]
 
 Which should yield the output:
 
 [Hello World, Hello again world]
 
 However, instead I get an error message saying TypeError: cannot
 concatenate 'str' and 'list' objects 
 
 How can I achieve the above? Thanks for reading.

You must have done something different than what you're describing above
to get that error message. The code you posted works as expected:

 mylist = [
... Hello ,
... Hello again 
... ]
 print [ i + World for i in mylist ]
['Hello World', 'Hello again World']

-- 
Carsten Haese
http://informixdb.sourceforge.net


-- 
http://mail.python.org/mailman/listinfo/python-list