Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question on for loop

Reply
Thread Tools

Question on for loop

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      01-03-2013
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.

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-03-2013
On 2013-01-03 20:04, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-03-2013
(E-Mail Removed) 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


 
Reply With Quote
 
Matt Jones
Guest
Posts: n/a
 
      01-03-2013
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 <(E-Mail Removed)> wrote:

> On 2013-01-03 20:04, (E-Mail Removed) 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>
>


 
Reply With Quote
 
Don Ross
Guest
Posts: n/a
 
      01-03-2013
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, (E-Mail Removed) 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.


 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-04-2013
On Jan 4, 6:04*am, (E-Mail Removed) 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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-04-2013
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
 
Reply With Quote
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      01-15-2013
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
newbie question: for loop within for loop confusion takayuki Python 17 06-17-2008 09:49 PM
newbie: for loop within for loop question takayuki Python 2 06-16-2008 03:22 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments