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


Groups > comp.lang.python > #36079 > unrolled thread

Question on for loop

Started bysubhabangalore@gmail.com
First post2013-01-03 12:04 -0800
Last post2013-01-04 10:16 +0000
Articles 9 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Question on for loop subhabangalore@gmail.com - 2013-01-03 12:04 -0800
    Re: Question on for loop MRAB <python@mrabarnett.plus.com> - 2013-01-03 20:21 +0000
    Re: Question on for loop Peter Otten <__peter__@web.de> - 2013-01-03 21:22 +0100
    Re: Question on for loop Matt Jones <matt.walker.jones@gmail.com> - 2013-01-03 14:28 -0600
    Re: Question on for loop Don Ross <donrosszx@gmail.com> - 2013-01-03 15:22 -0800
    Re: Question on for loop alex23 <wuwei23@gmail.com> - 2013-01-03 16:47 -0800
    Re: Question on for loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-04 05:48 +0000
      Re: Question on for loop subhabangalore@gmail.com - 2013-01-15 11:30 -0800
    Re: Question on for loop Alister <alister.ware@ntlworld.com> - 2013-01-04 10:16 +0000

#36079 — Question on for loop

Fromsubhabangalore@gmail.com
Date2013-01-03 12:04 -0800
SubjectQuestion on for loop
Message-ID<456df1a1-9225-44e7-9aa9-6b6fe3fde4a0@googlegroups.com>
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.

[toc] | [next] | [standalone]


#36080

FromMRAB <python@mrabarnett.plus.com>
Date2013-01-03 20:21 +0000
Message-ID<mailman.57.1357244517.2939.python-list@python.org>
In reply to#36079
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.

[toc] | [prev] | [next] | [standalone]


#36081

FromPeter Otten <__peter__@web.de>
Date2013-01-03 21:22 +0100
Message-ID<mailman.58.1357244525.2939.python-list@python.org>
In reply to#36079
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

[toc] | [prev] | [next] | [standalone]


#36082

FromMatt Jones <matt.walker.jones@gmail.com>
Date2013-01-03 14:28 -0600
Message-ID<mailman.59.1357244916.2939.python-list@python.org>
In reply to#36079

[Multipart message — attachments visible in raw view] — view raw

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>
>

[toc] | [prev] | [next] | [standalone]


#36087

FromDon Ross <donrosszx@gmail.com>
Date2013-01-03 15:22 -0800
Message-ID<8ae59f96-3cbf-457b-82e4-8045c83d85cb@googlegroups.com>
In reply to#36079
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.

[toc] | [prev] | [next] | [standalone]


#36094

Fromalex23 <wuwei23@gmail.com>
Date2013-01-03 16:47 -0800
Message-ID<dbf98c21-0f66-4c47-b44f-f00d0bac27f2@v9g2000pbi.googlegroups.com>
In reply to#36079
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.

[toc] | [prev] | [next] | [standalone]


#36100

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-01-04 05:48 +0000
Message-ID<50e66d28$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#36079
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

[toc] | [prev] | [next] | [standalone]


#36864

Fromsubhabangalore@gmail.com
Date2013-01-15 11:30 -0800
Message-ID<71096f2e-be87-449a-b5d9-7a02ded89a67@googlegroups.com>
In reply to#36100
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. 

[toc] | [prev] | [next] | [standalone]


#36104

FromAlister <alister.ware@ntlworld.com>
Date2013-01-04 10:16 +0000
Message-ID<g_xFs.984577$vW7.783635@fx19.am4>
In reply to#36079
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web