[英]return a list of strings in a recursive function python
我正在嘗試從功能Groups(s,k)返回一個字符串列表,其中基本上是字符串's'k> = 0和k <= len(s)的'k'元素的字符串列表,而這些這些字符串中的元素以與“ s”相同的順序出現,如下所示
群組(“ abcde”,2)→[“” ab“,” ac“,” ad“,” ae“,” bc“,” bd“,” be“,” cd“,” ce,“ de”]
群組(“ abcde”,5)→[“” abcde“]
組(“ abcde”,1)→[“ a”,“ b”,“ c”,“ d”,“ e”]
對於這個問題的措辭,我感到非常抱歉,因為這很難理解,但到目前為止,我的意思是:
def Groups(s,k):
if k == 0 or k > len(s):
return [""]
return [ i*k for i in s if k ==1]
if k > 1 and k <= 5:
return[ x for y in s y for x in s]
我將不勝感激任何幫助:)
我認為combinations
是您要尋找的詞:
list(''.join(letters) for letters in combinations("abcde", 2))
# returns ['ab', 'ac', 'ad', 'ae', 'bc', 'bd', 'be', 'cd', 'ce', 'de']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.