mercredi 29 avril 2015

Python, can I iterate through a list within an if statement?

I am trying to edit a str (myString) based on user input str (userInput). There are already a known set of subStrings that myString may or may not contain and are put into a list of str (namingConventionList) that, if any are used in myString should be replaced with userInput. If none of the set of naming conventions are used, I want the userInputadded to myString in a couple of different ways depending on if and where an underscore ("_") is. Is there a way to iterate through namingConventionList in an if statement?

        if myString.count(conv for conv in namingConventionList):
            myString= myString.replace(conv, userInput))
        elif userInput.startswith('_'):
            myString= '%s%s' % (name, userInput)
        elif userInputConvention.endswith('_'):
            myString= '%s%s' % (userInput, name)
        else:
            myString= '%s_%s' % (name, userInput)

Aucun commentaire:

Enregistrer un commentaire