473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

assigning values to array element

Ben
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
Jul 18 '05 #1
3 8250
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
Jul 18 '05 #2
cr*********@yahoo.com (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
Jul 18 '05 #3

"Ben" <cr*********@yahoo.com>

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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matthew Sims | last post by:
Python Newbie here. This is my first time learning object-oriented programming and trying to break out of the usual Korn/Perl/PHP style of programming. Having some difficulty understand some items....
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
2
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
5
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
37
by: miken32 | last post by:
In PHP, if a function returns an array it's fairly common to capture its return values like this: <?php list($foo, $bar, $baz) = some_function_that_return_an_array(); ?> In Javascript, would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.