473,785 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8278
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*********@yah oo.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*********@ya hoo.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
1825
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. For lists, I understand this: C= print C some But I can't seem to do this:
1
2179
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 work. It works if the tags are <input type=radio...>. This line is OK:
14
74900
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; myarray=0; myarray=1; myarray=8;
4
2719
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
3274
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. Source Error: Line 553: AppDTO.zip = sZip Line 554: AppDTO.dob = sDOB '** line 555 is the highlighted row with the problem.
28
2452
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 1: Why cannot I do the following:
5
2967
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 the tab key when the down arrow key is pressed. (we know there are other way to control focus flow but we use a lot of dynamic jsp fields, that will make the flow control a nightmare, we just want basic tabbing from the arrow key)
25
9544
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
2676
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 the equivalent (acceptable) code be this? = some_function_that_return_an_array();
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.