assigning values to array element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    assigning values to array element

    Hi all,

    This may sound easy but I'm having trouble assigning values to array
    element. My problem is as follows:

    m = ['Peter', 'Sam', 'Dave', 'Carl']
    for o in m:
    # Here first o is 'Peter'.. I want to do something like this:
    Peter = 10

    # if i do %s %o = 10, it gives me error...

    How can I do it?

    Thanks
    Ben
  • Peter Hansen

    #2
    Re: assigning values to array element

    Ben wrote:[color=blue]
    >
    > This may sound easy but I'm having trouble assigning values to array
    > element. My problem is as follows:
    >
    > m = ['Peter', 'Sam', 'Dave', 'Carl']
    > for o in m:
    > # Here first o is 'Peter'.. I want to do something like this:
    > Peter = 10
    >
    > # if i do %s %o = 10, it gives me error...
    >
    > How can I do it?[/color]

    You seem to want to create variables with names Peter, Sam, etc.

    If that's so, you should explain your problem in more detail,
    because doing this dynamically is useless: after all, how do
    you plan to *retrieve* those variables if you don't know in
    advance what they are called?

    What you are trying to do is probably better accomplished using
    Python dictionary type:

    # using your "m" list of names, above:
    d = {}
    for name in m:
    d[name] = 10

    # then to access things, do this:
    print d['Peter']

    If you need more, please explain the rationale behind the program,
    rather than just examples of code that didn't work, so we'll
    understand *why* you are trying to do what you are trying to do.

    -Peter

    Comment

    • Paul Rubin

      #3
      Re: assigning values to array element

      crescent_au@yah oo.com (Ben) writes:[color=blue]
      > This may sound easy but I'm having trouble assigning values to array
      > element. My problem is as follows:
      >
      > m = ['Peter', 'Sam', 'Dave', 'Carl']
      > for o in m:
      > # Here first o is 'Peter'.. I want to do something like this:
      > Peter = 10
      >
      > # if i do %s %o = 10, it gives me error...
      >
      > How can I do it?[/color]

      Um, you probably really don't want to do that. See the docs about
      how Python dictionaries work. Then try something like:

      m = ['Peter', 'Sam', 'Dave', 'Carl']
      table = {} # note these are curly braces
      for o in m:
      table[o] = 10

      Comment

      • Michael Peuser

        #4
        Re: assigning values to array element


        "Ben" <crescent_au@ya hoo.com>[color=blue]
        >
        > This may sound easy but I'm having trouble assigning values to array
        > element. My problem is as follows:
        >
        > m = ['Peter', 'Sam', 'Dave', 'Carl']
        > for o in m:
        > # Here first o is 'Peter'.. I want to do something like this:
        > Peter = 10
        >
        > # if i do %s %o = 10, it gives me error...
        >
        > How can I do it?[/color]

        I think 'old stuff' is really appropriate here:

        for index in xrange(len(m)):
        m[index]=10

        Kindly
        MichaelP


        Comment

        Working...