python - Making sure only a particular group of characters are in a string -
python - Making sure only a particular group of characters are in a string -
is there way create sure characters 'm' 'c' 'b' in string without resorting regex?
for instance, if user inputs 'm', programme print 'major'. if user inputs 'mc', programme print 'major, critical'.
so want create sure if user inputs 'mca', programme print 'not applicable'.
try: if 'a' in args.findbugs: if len(args.findbugs) > 1: print 'findbugs: not applicable argument.' else: print 'findbugs:all' else: if 'm' in args.findbugs: print 'findbugs:major' if 'c' in args.findbugs: print 'findbugs:critical' if 'b' in args.findbugs: print 'findbugs:blocker' except typeerror: print "findbugs: none"
well, simplest way you've described be:
some_string = 'mca' if set(some_string) <= {'m', 'c', 'b'}: # string contains 'm', 'c', or 'b'. else: # string 'mca' not match because of 'a'. or, if intend require @ to the lowest degree m, c, or b:
some_string = 'mca' if set(some_string) & {'m', 'c', 'b'}: # string contains 'm', 'c', or 'b', 'mca' match. note: pointed out bgporter, set literal notation not available in python versions less 2.7. if back upwards required, utilize set(('m', 'c', 'b')).
python string
Comments
Post a Comment