简体   繁体   English

如何创建一个递归函数以返回单个参数(字符串列表)的副本,但每个字符在每个位置都重复?

[英]How to create a recursive function that returns a copy of a single parameter (a list of strings), but with every character duplicated in each place?

Basically, I need to write a recursive function that takes a list of strings and then returns a list of strings with each character in each string repeated/doubled. 基本上,我需要编写一个递归函数,该函数需要一个字符串列表,然后返回一个字符串列表,每个字符串中的每个字符都重复/加倍。

For example, doubleStringList(['I','love','overflow']) would return ['II','lloovvee','oovveerrffllooww']. 例如,doubleStringList(['I','love','overflow'])将返回['II','lloovvee','oovveerrffllooww']。

I decided to split it up into two recursive functions. 我决定将其分为两个递归函数。 I already made one function that returns the strings with doubled characters: 我已经做了一个函数,该函数返回带有双倍字符的字符串:

def doubleCharacter(seq):
    if len(seq) == 0:
        return seq
    else:
        return (seq[0] * 2) + doubleCharacter(seq[1:])

For example, entering doubleCharacter("overflow") would return 'oovveerrffllooww' 例如,输入doubleCharacter(“ overflow”)将返回'oovveerrffllooww'

I'm trying to create a second recursive function that uses the first except for a list of strings as the paremeter rather than a single string. 我正在尝试创建第二个递归函数,该函数使用第一个递归函数,但将字符串列表作为参数而不是单个字符串。 For example: 例如:

def doubleStringList(sList):
    if len(sList) == 0:
        return sList
    else:
        return doubleCharacter(sList[0]) + doubleStringList(sList[1:])

Theoretically, if I entered doubleStringList(["overflow","is","great"]), it should return ["oovveerrffllooww", "iiss", "ggrreeaatt"]. 从理论上讲,如果我输入doubleStringList([“ overflow”,“ is”,“ great”]),它应该返回[“ oovveerrffllooww”,“ iiss”,“ ggrreeaatt”]。 If someone could please help me figure out this code, I would appreciate it greatly. 如果有人可以帮助我找出该代码,我将不胜感激。

The error message gives a big clue: 错误消息提供了一个重要线索:

TypeError: can only concatenate str (not "list") to str

So, the last line should be 所以,最后一行应该是

return [doubleCharacter(sList[0])] + doubleStringList(sList[1:])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单个字符上的 Split() 函数返回一个包含 2 个空字符串的数组? - Split() function on a single character returns an array with 2 empty strings? 获取字符串并返回8个字符的字符串列表的函数 - Function that takes a string and returns a list of 8-character strings 如何从字符串列表中的每个字符串中删除最后一个字符 - How to delete the very last character from every string in a list of strings C#如何将不包含单个字符的字符串列表转换为char? - C# How to convert a list of strings with not a single character to char? 在Haskell中是否可以将putStrLn函数应用于字符串列表的每个元素,使其打印到屏幕上,而无需递归 - Is it possible in Haskell to apply the function putStrLn to every element of a list of Strings, have it print to the screen, while being non recursive 在列表中,如何将每个字符串(带有混合特殊字符)分隔为单个字符? - In a list, how to separate each string (with mixed special characters) into a single character? 在递归函数python中返回字符串列表 - return a list of strings in a recursive function python 如何将字符串列表中的每个字符串发送到 function,然后用 function 生成的列表替换该列表元素? - How to send each of the strings within a list of strings to a function, and then replace that list element with the list that the function generates? (wx)Maxima:创建一个字符列表(作为字符串)而不必单独引用每个字符? - (wx)Maxima: create a list of characters (as strings) without having to quote each character, individually? 使用双精度列表中的 foreach 循环创建字符串列表。 每两个值必须压缩成一个字符串,然后添加到列表中 - create a list of strings using a foreach loop from list of doubles. every two values must be compressed into a single string then added to list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM