简体   繁体   中英

How to make an argument in python accept multiple inputs

I am still new to python; so, sorry for the somewhat vague question. I was just curious if it is possible to add more than one input to an argument. For example:

def censored(sentence, word):
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
    return sentence
print censored("foo off", "foo")

This will print out "**** off". Which is what I wanted; but, what if I want to add another input other than "foo".

Is there another way to do that without having to add a third, fourth, and nth argument in the function?

Of course you could pass a list, but also you could just use *args. It sometimes depends on how you expect to use the function.

def censored(sentence, *args):
  for word in args:    
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", "foo", "bar")

Or as a list or iter

def censored(sentence, words):
  for word in words:    
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", ("foo", "bar"))
print censored("foo off", ["foo", "bar"])

Here is a good SO for *args and **kwargs

You can iterate over the bad words and use replace to perform the substitutions if any of the words are present.

def censored(sentence, bad_words):
    for word in bad_words:
        sentence = sentence.replace(word, '*' * len(word))
    return sentence

>>> censored('foo off ya dingus', ['foo', 'dingus'])
'*** off ya ******'

In addition to CoryKramer:

Or create a global variable and use a default value for words.

EXLCUDED_WORDS = ['foo', 'dingus']

def censored(sentence, bad_words=EXCLUDED_WORDS):
    # check if the list is not empty
    if bad_words:
        for word in bad_words:
            sentence = sentence.replace(word, '*' * len(word))
    return sentence

censored('foo off ya dingus')

you could do it as a list.

def censored(sentence, words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence
print censored("foo off", ["foo","asp"])

There are a couple of ways to do this. The easiest is to pass a collection of strings as your second argument. For instance:

def censored(sentence, words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence

So, your usage could be:

print("foo that ship", ["foo", "ship"]) # passing a list
print("foo that ship", {"foo", "ship"}) # passing a set

Another way, but one that I wouldn't recommend, is to use variable length argument lists like:

def censored(sentence, *words):
    for word in words:
        if word in sentence:
            sentence = sentence.replace(word, "*" * len(word))
    return sentence

With the usage as:

print("foo that ship", "foo", "ship") # as many arguments as you'd like with the same function definition

The problem with this approach is that you cannot easily extend the list of banned words. However, it is a useful technique to know/understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM