Beginner programmer here. after a bunch of reading about 'variables' which dont exist in python, (still dont get that) Ive come to the opinion that I think I should be using lists in my data structure, but im not entirely sure.
im analysing a string which contains commands and normal words to be printed out, if the word is a command, which in this case is preceded by an '@' i want it to be in a separate list to the actual words. i will then process all this to be printed out afterwards, but i want the list to be ordered, so i can go through each element and test if it has a word in it, and if not do some action on the command.
so what i want really is a list with two indices(thankyou!) (what do you call that ?) like this:
arglist[x][y]
so i can go through arglist and process whether or not it contains a command or a word to be printed out. i want arglist[x]
to contain words and arglist[y]
to contain commands.
arglist = [] # not sure how to initialise this properly.
doh="my @command string is this bunch of words @blah with some commands contained in it"
for listindex, word in enumerate(doh):
if word.startswith('@'):
# its a command
arglist[listindex] = None
arglist[listindex][listindex]=command
else:
# its a normal word
arglist[listindex]=word
rglist[listindex][listindex]=None
then i want to be able to go down the list and pick out commands, i guess that would be something like this:
# get the number of items in the list and go through them...
for listindex, woo in enumerate(len(arglist)):
if arglist[listindex] is None:
# there is no word here, so print command
print arglist[listindex][listindex]
else:
# just print out word
print arglist[listindex]
so: my question is which data type/ structure should I be using and should I / how do I initialise it ? am I barking up the right tree here?
edit: i just found this gem and now im even more unsure - i want it to be the fastest lookup possible on my data but i still want it to be ordered.
dict and set are fundamentally different from lists and tuples`. They store the hash of their keys, allowing you to see if an item is in them very quickly, but requires the key be hashable. You don't get the same membership testing speed with linked lists or arrays.
many thanks as usual for any help.
edit: eg my string from above should look something like this. doh="my @command string is this bunch of words @blah with some commands contained in it"
arglist[1] = 'my'
arglist[1][1] = None
arglist[2] = None
arglist[2][1] = command
arglist[3] = 'string'
arglist[3][1] = None
etc etc
this whole thing has left me a bit baffled i shall try and update this later.
EDIT: if anyone wanted to know what this was all about look here