I need help with something I'm working on. I'm new to python so bare with me.

I have a string being randomly generated in such format (example):

F' B D D2 A2 C'

6 Letters from A to F with ether a number " 2 " or " ' " attached to a letter or nothing at all.

What I'm trying to do is to assign each letter as a variable.

The string is basically a sequence in which certain functions have to be executed.

Any thoughts on how I could achieve this?

I tried generating a list and then extracting variables based on position [x:x] but as the letters without 2 or " ' " are randomly placed in a string its impossible to it.

7 Years
Discussion Span
Last Post by wtzolt

You could start like this

mystring = "F' B D D2 A2 C'"
mylist = mystring.split()

""" my output -->
["F'", 'B', 'D', 'D2', 'A2', "C'"]

Edited by Gribouillis: n/a


Ok but how would I assign a name to each of the letters
["F'", 'B', 'D', 'D2', 'A2', "C'"]? The string is random so range slicing wont work.


Perhaps have a dictionary?

>>> def FFunc():
	print "This is the F function"

>>> def BFunc():
	print"This is the B function"

>>> def DFunc():
	print "This is the D function"

>>> randomstuff = ['F','B','D','F','B']
>>> funcdict = {'F':FFunc,'B':BFunc,"D":DFunc}
>>> for f in randomstuff:

This is the F function
This is the B function
This is the D function
This is the F function
This is the B function

See by using the dictionary i could link the strings to a function, you could add functions and strings to the dictionary easily.

That was just a quick example made with IDLE but i think it illustrates the power of a dictionary and how it can be useful in these situations. :)

hope that helps :)

EDIT: Just re-read the question.. not so sure if this is relevant anymore :S

Edited by Paul Thompson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.