|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I create a multidimensional list?
Answer: You probably tried to make a multidimensional array like this:
A = [[None] * 2] * 3
This looks correct if you print it:
>>> A [[None, None], [None, None], [None, None]]
But when you assign a value, it shows up in multiple places:
>>> A[0][0] = 5 >>> A [[5, None], [5, None], [5, None]]
The reason is that replicating a list with * doesn't create copies, it only creates references to the existing objects. The *3 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.
The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:
A = [None]*3 for i in range(3): A[i] = [None] * 2
This generates a list containing 3 different lists of length two. You can also use a list comprehension:
w,h = 2,3 A = [ [None]*w for i in range(h) ]
Or, you can use an extension that provides a matrix datatype; Numeric Python is the best known.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 6963 users |
Added on |
9/23/2014 |
Views |
68252 |
Rate it! |
|
|
Question:
How do I create a multidimensional list?
Answer:
You probably tried to make a multidimensional array like this:
A = [[None] * 2] * 3
This looks correct if you print it:
>>> A [[None, None], [None, None], [None, None]]
But when you assign a value, it shows up in multiple places:
>>> A[0][0] = 5 >>> A [[5, None], [5, None], [5, None]]
The reason is that replicating a list with * doesn't create copies, it only creates references to the existing objects. The *3 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.
The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:
A = [None]*3 for i in range(3): A[i] = [None] * 2
This generates a list containing 3 different lists of length two. You can also use a list comprehension:
w,h = 2,3 A = [ [None]*w for i in range(h) ]
Or, you can use an extension that provides a matrix datatype; Numeric Python is the best known. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do you remove duplicates from a list?
|
View Answer
|
|
How do I iterate over a sequence in reverse order?
|
View Answer
|
|
What's a negative index?
|
View Answer
|
|
How do I convert between tuples and lists?
|
View Answer
|
|
Is there a scanf() or sscanf() equivalent?
|
View Answer
|
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
View Answer
|
|
How do I use strings to call functions/methods?
|
View Answer
|
|
How do I modify a string in place?
|
View Answer
|
|
How do I convert a number to a string?
|
View Answer
|
|
Is there an equivalent of C's "?:" ternary operator?
|
View Answer
|
|
How can my code discover the name of an object?
|
View Answer
|
|
Explain about pickling and unpickling?
|
View Answer
|
|
Explain about repr function?
|
View Answer
|
|
Explain about assert statement?
|
View Answer
|
|
What is a Lambda form?
|
View Answer
|
|
Explain about raising error exceptions
|
View Answer
|
|
Explain about indexing and slicing operation in sequences?
|
View Answer
|
|
Explain about the dictionary function in Python?
|
View Answer
|
|
Explain and statement about list?
|
View Answer
|
|
What is tuple?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|