473,385 Members | 1,736 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,385 software developers and data experts.

problem with two-dimensional list/array

hi,

i've created a 2D list(ie: A=[]). i choose some of element from the list(ie: A[0][0],A[1][1],A[2][2]) and do simple add operations.

Then i created another empty 2D list to store the result. but i wanted the result to be stored at a specific index (ie: at B=[0][0],B[1][1],B[2][2]).

but i get an error."Error - list index out of range "

here is my code...can anyone spot my mistake...

Expand|Select|Wrap|Line Numbers
  1. A=[]
  2.         for i in range(3):
  3.             A.append([])
  4. A[0]=(1,3,4,5)
  5. A[1]=(2,4,6,5)
  6. A[2]=(3,6,7,4)
  7.  
  8. B=[]
  9.       for j in range(3):
  10.              B.append([])
  11.              for i in range(3):
  12.                  B[i][i]=2+A[i][i]
  13.  
  14.  
Sep 2 '10 #1

✓ answered by bvdet

Your indentation is wrong, but I suspect it is a copy/paste problem.

You did not initialize list B properly.
Expand|Select|Wrap|Line Numbers
  1. A=[]
  2. for i in range(3):
  3.     A.append([])
  4. A[0]=(1,3,4,5)
  5. A[1]=(2,4,6,5)
  6. A[2]=(3,6,7,4)
  7.  
  8. B=[[None for i in range(4)] for j in range(3)]
  9.  
  10. for i in range(3):
  11.     B[i][i]=2+A[i][i]
Expand|Select|Wrap|Line Numbers
  1. >>> B
  2. [[3, None, None, None], [None, 6, None, None], [None, None, 9, None]]
  3. >>> 

2 5881
bvdet
2,851 Expert Mod 2GB
Your indentation is wrong, but I suspect it is a copy/paste problem.

You did not initialize list B properly.
Expand|Select|Wrap|Line Numbers
  1. A=[]
  2. for i in range(3):
  3.     A.append([])
  4. A[0]=(1,3,4,5)
  5. A[1]=(2,4,6,5)
  6. A[2]=(3,6,7,4)
  7.  
  8. B=[[None for i in range(4)] for j in range(3)]
  9.  
  10. for i in range(3):
  11.     B[i][i]=2+A[i][i]
Expand|Select|Wrap|Line Numbers
  1. >>> B
  2. [[3, None, None, None], [None, 6, None, None], [None, None, 9, None]]
  3. >>> 
Sep 2 '10 #2
dwblas
626 Expert 512MB
Then i created another empty 2D list to store the result.
You can use an intermediate list for this as well, unless I misunderstand.
Expand|Select|Wrap|Line Numbers
  1. A=[]
  2. A.append((1,3,4,5))
  3. A.append((2,4,6,5))
  4. A.append((3,6,7,4))
  5.  
  6. B=[]
  7. for j in range(3):
  8.     temp_list = []     ## starts as empty for each "j" loop
  9.     for k in range(4):
  10.         temp_list.append(2+A[j][k])
  11.  
  12.     B.append(temp_list) 
Sep 3 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: simon | last post by:
Hi, I have two tables Table A = {ID, Item} Table B = {ID, IDA, subItem} Where ID auto increment. INSERT INTO TABLE_A ('items') values ('a')
3
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
4
by: | last post by:
I am stuck in a situation and I do believe that this should work, but it doesn't. I have a unmanaged dll, that uses MFC. This works great. Now I recompile the unmanaged dll so it contains...
1
by: Teemu Keiski | last post by:
Hi, I have following type of scenario (also explained here http://blogs.aspadvice.com/joteke/archive/2005/01/10/2196.aspx ) We have problematic web server (wink2 Standard, 1.5GB of physical...
0
by: abottchow | last post by:
Hi all, I'm new to the group and am seeking your advice on my Linux programming problem. Two programs are involved. One is myProgram.cc, which reads user's input from keyboard and prints...
3
by: Chifo | last post by:
hello. i have a problem with a populate html table with data from table here it's the problem two querys retrieving data from table, one of querys show me a colletion of data from 6:00 am to...
1
by: ilucks | last post by:
I have DB(Access 97) where multi- user work in a network. Today, I encountered this problem. Two users were in it and do edition and adding a new data on the same form at the same time. User...
4
by: TD | last post by:
I have a main form which is unbound. On this form I have two subforms that are linked thru an unbound textbox on the main form. Both both of the subforms are in datasheet view. This setup work...
1
by: mikeDA | last post by:
Hello to all, I am writing a query that is attempting to take three fields in a table, and create a new field called "MyKey." I'm doing this using concatenation. The problem: two of these fields,...
1
by: shobram | last post by:
All problems below require some kind of input. You are free to implement any mechanism for feeding input into your solution (for example, using hard coded data within a unit test). You should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.