[英]how do i get a list of all combinations for both words given?
i was trying to come up with a way to get a list of words like "yes" in all possible (but common) ways like lower cased and upper case and capitalized.我试图想出一种方法来以所有可能的(但常见的)方式(如小写和大写和大写)获取像“是”这样的单词列表。 and then i found out you cant put two words in this function (the words "sup" & "hello").
然后我发现你不能在这个 function 中放两个词(“sup”和“hello”)。 is there a way possible to be able to have all the words in one list with this function or should start over?
有没有办法可以用这个 function 将所有单词放在一个列表中,还是应该重新开始?
def case_insensetive(text) :
insensetive_string = [text.lower(),text.upper(),text.capitalize()]
print (insensetive_string)
try :
case_insensetive("sup","hello")
except :
raise Exception ("you screwed something")
This is a typical job for the map() function.这是map() function 的典型工作。
Also, use return
instead of print
in your function.此外,在 function 中使用
return
而不是print
。
def case_insensetive(text):
insensetive_string = [text.lower(),text.upper(),text.capitalize()]
return insensetive_string
words = ['yes', 'hello']
r = list(map(case_insensetive, words))
print(r)
Output: Output:
[['yes', 'YES', 'Yes'], ['hello', 'HELLO', 'Hello']]
If you want a single list, instead of a nested one:如果您想要一个列表,而不是嵌套列表:
flat_list = [item for sublist in r for item in sublist]
print(flat_list)
['yes', 'YES', 'Yes', 'hello', 'HELLO', 'Hello']
Just pass an arbitrary parameter(*args)..then take it can any numbers of arguments..只需传递一个任意参数(*args)..然后可以任意数量的 arguments..
def case_insensetive(*texts):
insensetive_string = []
for text in texts:
insensetive_string+=[text.lower(),text.upper(),text.capitalize()]
print(insensetive_string)
case_insensetive("sup",'hello')
Output: ['sup', 'SUP', 'Sup', 'hello', 'HELLO', 'Hello'] Output:['sup','SUP','Sup','你好','你好','你好']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.