Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Question on for loop (http://www.velocityreviews.com/forums/t956105-question-on-for-loop.html)

subhabangalore@gmail.com 01-03-2013 08:04 PM

Question on for loop
 
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.


MRAB 01-03-2013 08:21 PM

Re: Question on for loop
 
On 2013-01-03 20:04, subhabangalore@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.

Peter Otten 01-03-2013 08:22 PM

Re: Question on for loop
 
subhabangalore@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



Matt Jones 01-03-2013 08:28 PM

Re: Question on for loop
 
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 <python@mrabarnett.plus.com> wrote:

> On 2013-01-03 20:04, subhabangalore@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<http://mail.python.org/mailman/listinfo/python-list>
>



Don Ross 01-03-2013 11:22 PM

Re: Question on for loop
 
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.



alex23 01-04-2013 12:47 AM

Re: Question on for loop
 
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.

Steven D'Aprano 01-04-2013 05:48 AM

Re: Question on for loop
 
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

subhabangalore@gmail.com 01-15-2013 07:30 PM

Re: Question on for loop
 
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.


All times are GMT. The time now is 02:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.