简体   繁体   English

我如何获得给定两个单词的所有组合列表?

[英]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.

相关问题 如何在python中获取单词列表的组合 - How do I get combinations of a list of words in python 如何获得具有重复值的列表的所有组合 - How do I get all combinations of a list with repeating values 鉴于两个项目不能在同一列表中,我如何获得列表的所有组合? - How can I get all the combinations of a list given that two items can not be in the same list? 如何打印此列表的所有组合? - How do I print all combinations of this list? 给定总体和子集,如何创建所有组合的列表? (nCr) - How do I create a list of all combinations, given a population and subset? (nCr) 如何获得列表的所有组合? - How can I get all combinations of a list? 创建既在给定字符串中又在给定字典中的所有单词的列表 - Creating a list of all words that are both in a given string and in a given dictionary 我如何使组合列表的参数最多为我提供给算法的 7 个单词中的 5 个单词? - How do I make that the list of combinations has a parameter of max 5 words out of the 7 words that I gave to the algorithm? 如何从python中的给定字符串生成1、2和3个单词的所有后续组合? - How to generate all subsequent combinations of 1, 2 and 3 words from a given string in python? 如何从给定的字典中获取所有组合? - How to get all combinations from a given dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM