Create a two-dimensional array at runtime: Difference between revisions
Content added Content deleted
(Regrouped BASIC examples, added Commodore BASIC example) |
(→{{header|Python}}: Python 2 is reaching end of life. Adjusting code so that it works in Python 3) |
||
Line 1,899: | Line 1,899: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python| 2.5}} |
{{works with|Python| 2.5 and 3.6}} |
||
<lang python>width = int(raw_input("Width of myarray: ")) |
<lang python>width = int(raw_input("Width of myarray: ")) |
||
height = int(raw_input("Height of Array: ")) |
height = int(raw_input("Height of Array: ")) |
||
myarray = [[0] * width for i in |
myarray = [[0] * width for i in range(height)] |
||
myarray[0][0] = 3.5 |
myarray[0][0] = 3.5 |
||
print myarray[0][0]</lang> |
print (myarray[0][0])</lang> |
||
'''Note:''' Some people may instinctively try to write myarray as [[0] * width] * height, but the * operator creates ''n'' references to [[0] * width] |
'''Note:''' Some people may instinctively try to write myarray as [[0] * width] * height, but the * operator creates ''n'' references to [[0] * width] |
||
Line 1,914: | Line 1,914: | ||
# or, in pre 2.7 versions of Python: myarray = dict(((w,h), 0) for w in range(width) for h in range(height)) |
# or, in pre 2.7 versions of Python: myarray = dict(((w,h), 0) for w in range(width) for h in range(height)) |
||
myarray[(0,0)] = 3.5 |
myarray[(0,0)] = 3.5 |
||
print myarray[(0,0)]</lang> |
print (myarray[(0,0)])</lang> |
||
=={{header|R}}== |
=={{header|R}}== |