I really like bvdet's solution, succinct and Pythonic. Make it in a function and you are good to go.
It's a little cryptic for some beginners as
list comprehensions are not often a well practiced looping tool. All those
string escapes can camoflage its elegance.
And the
join() function is backwards looking to beginners too as it looks like this:
seperator.join(sequence) instead of
join(sequence, seperator)
Following I broke his recipe down into parts so you can learn to rebuild his solution. I think you could have easily figured out how to program a recipe, if you knew how to break your problem down.
As bvdet's recipe shows, the trick to your solution is to break the string apart on the seperator " or " (with spaces included there). The second trick is knowing about how to rebuild strings in Python. There are many tools to parse and build strings. Knowing these leads you to your solutions. Look at the strings library for old ways to do it. Look at str() methods for some easy ways too.
The two Python tools (s)he combines are:
1. Python format strings: "%s" % (item,)
2. and str.join() (do a dir(str) and then help(str.join))
An aside regarding your original question about user input; Look into validators. Validators are designed to work with "input". Personally I've never used them.
The not-so-Pythonic solution I give below is intended to give you some other tools that might help. Sometimes it's best to see solutions to real problems one has to see the benefits. Some tools I use below are
lambda, a one line unnamed function definition,
yield statement in a function to make an iterator (instead of the
return statement), simpler looking
list comprehensions (a fast for loop).
Also I tried to break it down and use terms which can lead you to learn
templating. As user various user input might have different formats I try to show an home-grown starter "template". Templates are handy if you have lots of different formats to deal with.
The specific data you gave may be the only format you need to convert but others with a similar question my need something more adaptable.
I did this code in PySlicesShell; Google it.
- #start code
-
items = [
-
'999-99-999',
-
'999-99-999 or 999-99-998',
-
'999-99-999 or 999-99-998 or 999-99-997',
-
'999-99-999 or 999-99-998 or 999-99-997 or 999-99-996',
-
]
-
-
seperator = ' or '
-
prefix = '"APM" = '
-
item_tags = "'%s'"
-
-
template = prefix + item_tags
-
split = lambda item: item.split(seperator)
-
tag = lambda parsed: [ template % item for item in parsed]
-
join = lambda tagged: seperator.join( tagged )
-
reformat = lambda item: join(tag(split(item)))
-
process = lambda items: [reformat(item) for item in items]
-
-
def iprocess(items):
-
for item in items:
-
yield reformat(item)
-
-
#parsed = split( items[3] )
-
#tagged = tag( parsed )
-
#output = join( tagged )
-
#print join(tag(split(items[3])))
-
#print reformat(items[3])
-
-
processed = process(items)
-
print('"""')
-
print('Using a simple custom template-like algorithim')
-
for item in processed: print(item)
-
-
print('\nUsing above with an iterator')
-
for item in iprocess(items): print(item)
-
print('"""')
-
-
"""
-
Using a simple custom template-like algorithim
-
"APM" = '999-99-999'
-
"APM" = '999-99-999' or "APM" = '999-99-998'
-
"APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
-
"APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
-
-
Using above with an iterator
-
"APM" = '999-99-999'
-
"APM" = '999-99-999' or "APM" = '999-99-998'
-
"APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997'
-
"APM" = '999-99-999' or "APM" = '999-99-998' or "APM" = '999-99-997' or "APM" = '999-99-996'
-
"""
-
#end code