Python Forum
BASIC to python3: using lists like arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASIC to python3: using lists like arrays
#2
my_array = [[0 for y in range(20)] for x in range(10)] #dim myArray(10,20)

for x in range(10): #for x=1 to 10
   for y in range(20): #for y=1 to 20
       my_array[x][y] = (x+1)-(y+1) #let myArray(x,y)=(x-y) 'populate array with data using a formula
# next y
# next x

for x in range(10): #for x=1 to 10
   for y in range(20): #for y=1 to 20
       print(my_array[x][y])# print myArray(x,y) 'output contents - could also be print to file
# next y
# next x

The creation and setting of values of my_array can be done in the list comprehension
my_array2 = [[(x+1)-(y+1) for y in range(20)] for x in range(10)]

print(my_array == my_array2)
Output:
True
Reply


Messages In This Thread
RE: BASIC to python3: using lists like arrays - by Yoriz - Dec-24-2016, 12:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,586 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 5,120 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,528 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020