Re: change vars in place w/loop or list comprehension



Hi
what u can do over here is
add a,b,c... in a list e.g. list.append(vars..)
and then use the statement
newlist = map(lambda x:x.upper(),list)
Now ur newlist will contain the modified list.
HOPE THIS THE BETTER SOLUTION TO UR PROBLEM


Quoting ken.boss@xxxxxxxxxxxxxxx:

> I am a python newbie, and am grappling with a fundamental concept. I
> want to
> modify a bunch of variables in place. Consider the following:
>
> >>> a = 'one'
> >>> b = 'two'
> >>> c = 'three'
> >>> list = [a, b, c]
> >>> for i in range(len(list)):
> ... list[i] = list[i].upper()
> ...
> >>> [a, b, c] = list
> >>> a
> 'ONE'
>
> or, better:
>
> >>> a = 'one'
> >>> b = 'two'
> >>> c = 'three'
> >>> [a, b, c] = [s.upper() for s in [a, b, c]]
> >>> a
> 'ONE'
>
> Both of these accomplish what I'm after; I prefer the second for its
> brevity.
> But either approach requires that I spell out my list of vars-to-alter
> [a, b, c] twice. This strikes me as awkward, and would be difficult
> with longer lists. Any suggestions for a better way to do this?
>
> --Ken
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
Regards,
Jatinder Singh

? Everyone needs to be loved... especially when they do not deserve it.?
.



Relevant Pages

  • Re: Syntax simplification
    ... >>teach people why there are two kinds of quoting (brackets and quote), ... >>and I thought quote-quoting would be easier dispended with. ... There are two kinds of quoting because ... it would be conceivable that lists would require ...
    (comp.lang.logo)
  • Re: 2nd RfD: Directories
    ... in Unix, i.e. if I define a list of paths, I separate them with:. ... the Forth system, including path lists. ... triggers a variable expansion, ... cmd.exe, and I don't know if quoting works that well there (honestly, I ...
    (comp.lang.forth)
  • Re: [Full-Disclosure] Will a vote for John Kerry be counted by a Hart InterCivic eSlate3000 in Honol
    ... I am not quoting any previous emails, as this is not directed at anyone ... vulnerabilities, etc. ... There are lists for that type of discussion; ... I add this to the long list of folks asking, please, take this ...
    (Full-Disclosure)
  • Re: How many plots are there? (Re: Public whine)
    ... are only a few plots or not--the subject wasn't my idea. ... aside from quoting Heinlein? ... Here's a somewhat interesting set of lists: ... Gerry Quinn ...
    (rec.arts.sf.composition)
  • Re: "Fixing" Clerics
    ... I still feel that the standard Cleric has too broad a spell list. ... In Complete Psionics, you have the concept of "mantles", which are collections ... You might consider requiring your clerics to draw all of their spells from the ... lists of their deity's domains - to compensate for the reduction in power, ...
    (rec.games.frp.dnd)