[英]How to make an argument in python accept multiple inputs
I am still new to python; 我还是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". 这将打印出“ **** off”。 Which is what I wanted;
这就是我想要的; but, what if I want to add another input other than "foo".
但是,如果我想添加除“ foo”以外的其他输入该怎么办。
Is there another way to do that without having to add a third, fourth, and nth argument in the function? 还有另一种方法可以不必在函数中添加第三,第四和第n个参数吗?
Of course you could pass a list, but also you could just use *args. 当然,您可以传递一个列表,但也可以只使用* 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 这是* args和** kwargs的一个很好的SO
You can iterate over the bad words and use replace
to perform the substitutions if any of the words are present. 您可以遍历不良词,如果存在任何词,则使用
replace
进行替换。
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: 除了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.
但是,这是了解/理解的有用技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.