1

I am trying to implement a transition function. I have stored the transitions in a list:

def transistion(self, input):
    self.currentState = 'q1'
    self.delta = "[['q1', '0', 'q2'], ['q1', '1', 'q1']]"
    k = ast.literal_eval(self.delta)
    delta_list = [[x[0],tuple(x[1:])] for x in k]
    print(delta_list)

print(delta_list) # [['q1', ('0', 'q2')], ['q1', ('1', 'q1')]]

I need to:

  • search delta_list to see if the self.currentState is a first element in one of the lists
  • if the first element was found, check that list and see if the first element of the tuple is equal to input
  • if the input value was also found, set self.currentState equal to the second element of the tuple
senshin
  • 10,022
  • 7
  • 46
  • 59
  • 2
    You didn't "store them in a list comprehension". You used a list comprehension to store them in a list. – Andrew Jaffe Dec 04 '14 at 15:39
  • As a side note, the "question" itself (=> the 'I need to' part) is pretty trivial. What have you tried that didn't work ? – bruno desthuilliers Dec 04 '14 at 15:43
  • duplicate of this one http://stackoverflow.com/questions/27289287/how-to-parse-a-string-into-a-dictionary/27289395#27289395 – Hackaholic Dec 04 '14 at 15:48
  • @Hackaholic Not sure this is a dupe. Yeah, it's parsing and mentions ast.literal_eval, but the real question was about what to do next. The OP had at least gotten the string parsed to a list - one way or another. – kdopen Dec 04 '14 at 16:06
  • @Hackaholic : the current question is NOT a duplicate of http://stackoverflow.com/q/27289287/41316 – bruno desthuilliers Dec 04 '14 at 16:07

1 Answers1

1

Just to get you started, this

def transistion(self, input):
    self.currentState = 'q1'
    self.delta = "[['q1', '0', 'q2'], ['q1', '1', 'q1']]"
    k = ast.literal_eval(self.delta)
    delta_list = [[x[0],tuple(x[1:])] for x in k]
    print(delta_list)

can be more simply done as:

def transistion(self, input):
    self.currentState = 'q1'
    self.delta = eval("[['q1', '0', 'q2'], ['q1', '1', 'q1']]")
    delta_list = [[x[0],tuple(x[1:])] for x in self.delta]
    print(delta_list)

But why bother, unless this is a test harness? You can just set delta_list to exactly what you want - even in a test harness

delta_list = [['q1',('0','q2')],['q1', ('1', 'q1')]]

At this point, it's a basic for loop:

for t in delta_list:
    if self.current_state == t[0]:
        # rest of checks and actions. break if you change current state

But why bother with the tuples? You can just work with the original list of lists

kdopen
  • 8,032
  • 7
  • 44
  • 52