Can I access an array using a variable?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
eli2k
Posts: 26
Joined: Mon Jun 29, 2009 10:34 pm

Can I access an array using a variable?

Post by eli2k » Tue Sep 26, 2017 5:43 am

Hi all, need some guidance. I'm trying to set up something like a 2D array but not sure how.

Code: Select all

arrayHeader = [...]
array1 = [...]
array2 = [...]
array3 = [...]
All the arrays are of the same length. There will be an underlying condition that determines whether array1, array2, or array3 is accessed. The input will contain some form of text, and this text exists in one of the elements of arrayHeader. My initial plan was the input will give me the index in arrayHeader, so that takes care of the column. But I do not know how to get the row. The conditions generate a string, like array2, but I can't use that string to access the array. I'm probably doing it the wrong way. Please guide. Thank you.

Code: Select all

userInput > find the index in arrayHeader > needleIndex
conditions return array2 > needleArray

need to access needleArray[needleIndex]
setup: windows 7 + EventGhost 0.5.0-rc4 + Intel nuc6i5syh

User avatar
kgschlosser
Site Admin
Posts: 2889
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Can I access an array using a variable?

Post by kgschlosser » Tue Sep 26, 2017 8:01 am

in python they are called lists. and since python is a dynamic language there is no need to declare them unless you are indexing a specific part.

to create a list

Code: Select all

some_list = []
to create a nested list (multi dimensional array)

Code: Select all

some_list = [[]]
there is no need to be specific on the size of the array unless you plan on doing something like the following.

Code: Select all

some_list = []
print some_list[3]
now this can be handled one of 3 ways, initialize the list with the required number of indices

Code: Select all

some_list = [None] * 4
you can check and make sure there are enough indexes

Code: Select all

some_list = []
if len(some_list) >= 4:
    print some_list[3]
or you can simply catch the error
It is easier to ask for forgiveness then it is to ask permission

Code: Select all

some_list = []
try:
	print some_list[3]
except IndexError:
	print "empty list"
strings str() in python are lists and also strings at the same time

Code: Select all

some_list = "Hello World"
print some_list[3]
You can story anything you want in a list. it can be an object, int, char, str, float, tuple, dict

One thing you do have to remember is that lists are mutable so if you do the following

Code: Select all

some_list1 = [1, 2, 3, 4]
some_list2 = some_list1
some_list2[0] = 0
print some_list1[0]
print some_list2[0]
the output for both will be a 0.

this can be overcome by making a copy of the list

Code: Select all

some_list1 = [1, 2, 3, 4]
some_list2 = some_list1[:]
some_list2[0] = 0
print some_list1[0]
print some_list2[0]
the output will be a 1 and a 0

at any point you can append or extend the list by using the methods extend and append

Code: Select all

some_list = [1, 2, 3, 4]
some_list.append(5)
some_list.extend([6, 7, 8, 9])
print some_list
the output will be 123456789


there is also the pop and remove methods, pop will return the index it has removed and remove will remove the first matching item in the list without returning anything

Code: Select all

some_list = [1, 2, "3", 4]
print some_list.pop(1)
some_list.remove("3")
print some_list
the output would be 1 4

and as long as the variable that you are using to access a specific item contains an int() then you are good to go.

Code: Select all

some_list = [1, 2, 3, 4]
some_index = 2
print some_list[some_index]
outputs 3
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 2889
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Can I access an array using a variable?

Post by kgschlosser » Tue Sep 26, 2017 8:05 am

also if you post the code you are trying to use it would help me greatly to understand what is going on.
If you like the work I have been doing then feel free to Image

User avatar
topix
Experienced User
Posts: 367
Joined: Sat May 05, 2007 3:43 pm
Location: Germany
Contact:

Re: Can I access an array using a variable?

Post by topix » Tue Sep 26, 2017 8:20 am

If i understand correct you get for example as input a string like "Array 1". That should give you the list from array1? Then i would recommend to use a dict:

Code: Select all

input = "Array 1"

arrays = {
	"Array 1": ["This", "is", "list", 1],
	"Array 2": ["This", "is", 2],
	"Array 3": ["And", "list", "number", 3]
}

print arrays[input]  # if input is "Array 1" you will get ["This", "is", "list", 1]
x = arrays[input]  # now x is ["This", "is", "list", 1]
arrays[input][2] == "list"  # True

eli2k
Posts: 26
Joined: Mon Jun 29, 2009 10:34 pm

Re: Can I access an array using a variable?

Post by eli2k » Tue Sep 26, 2017 10:35 pm

@kgschlosser Thanks for taking the time to write all that out! You could have just pointed me to a Google search!

What @topix suggested is what I was looking for, using the dictionary. In my hobby programming I have not run across this concept before. What I Macgyver'd was the following:

Code: Select all

arrayHeader = [...]
array1 = [...]
array2 = [...]
array3 = [...]
arrayNumbers = [array1, array2, array3] # making an array (?list) of arrays (?lists)
arrayNumberNames = ['array1', 'array2', 'array3']

indexHeader = arrayHeader.index('something') # find out which column to pick
indexArray = arrayNumberNames.index('something-else') # find out which row to pick

desiredOutput = arrayNumbers[indexArray][indexHeader]
This works, but I just reimplented it using a dictionary, with the same result and a little neater code. Thank you for the help!
setup: windows 7 + EventGhost 0.5.0-rc4 + Intel nuc6i5syh

User avatar
kgschlosser
Site Admin
Posts: 2889
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Can I access an array using a variable?

Post by kgschlosser » Wed Sep 27, 2017 6:08 am

well i had the information in my bean and i do not mind typing it out. others can use the information as well. I was going to mention a dictionary to you but the closest thing that would match an "array" would be a list. I was not sure of your use case that is why i asked to see the actual code.
If you like the work I have been doing then feel free to Image

User avatar
Luca Brasi
Experienced User
Posts: 250
Joined: Sat Oct 11, 2008 12:39 pm

Re: Can I access an array using a variable?

Post by Luca Brasi » Wed Sep 27, 2017 10:44 am

This is why this forum is awesome!
I have this folder in my bookmarks where I put posts like kgschlosser's with all the info to get back to them whenever I need the info.
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi

Post Reply