assigning values to array element

Discussion in 'Python' started by Ben, Oct 15, 2003.

  1. Ben

    Ben Guest

    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
    Ben, Oct 15, 2003
    #1
    1. Advertising

  2. Ben

    Peter Hansen Guest

    Ben wrote:
    >
    > 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?


    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
    Peter Hansen, Oct 15, 2003
    #2
    1. Advertising

  3. Ben

    Paul Rubin Guest

    (Ben) writes:
    > 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?


    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
    Paul Rubin, Oct 15, 2003
    #3
  4. "Ben" <>
    >
    > 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?


    I think 'old stuff' is really appropriate here:

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

    Kindly
    MichaelP
    Michael Peuser, Oct 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnsy Joseph
    Replies:
    4
    Views:
    4,366
    Jim Lewis
    Sep 21, 2004
  2. Eric Bantock

    assigning values to an array after declaring it

    Eric Bantock, Jun 16, 2004, in forum: C Programming
    Replies:
    14
    Views:
    23,857
    Dave Thompson
    Jul 1, 2004
  3. Ray D.
    Replies:
    2
    Views:
    382
    Peter Nilsson
    Oct 3, 2007
  4. Richard Lionheart
    Replies:
    27
    Views:
    380
    Jean-Hugues ROBERT
    May 4, 2004
  5. weston
    Replies:
    1
    Views:
    239
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page