简体   繁体   English

接受字符串嵌套列表并返回所有字符串大写的新嵌套列表的函数?

[英]Function that takes a nested list of strings and returns a new nested list with all strings capitalized?

This will capitalize them but only if there are no nested lists.这将大写它们,但前提是没有嵌套列表。

t = ['this','that', ['other']]

def capitalize_nested(t):
    res = []
    for s in t:
        res.append(s.capitalize())
    return res

print capitalize_nested(t)

I can't figure out how to get it to print out a nested list having all of the strings start with a capital letter.我不知道如何让它打印出所有字符串都以大写字母开头的嵌套列表。 I must be missing something obvious, this just has me stumped.我一定遗漏了一些明显的东西,这让我很难过。

Use a recursive solution (and using list comprehensions also helps make it more compact):使用递归解决方案(使用列表推导式也有助于使其更紧凑):

def capitalize_nested(t):
    if isinstance(t, list):
        return [capitalize_nested(s) for s in t]
    else:
        return t.capitalize()

For example:例如:

print capitalize_nested(['this', 'that', ['other']])
# ['This', 'That', ['Other']]
def cap(L):
    for i,elem in enumerate(L):
         if isinstance(elem, str):
             L[i] = elem.capitalize()
         elif isinstance(elem, list):
             cap(L[i])

Just check if s is a list then recursively call your capitalize_nested function:只需检查s是否是一个列表,然后递归调用您的capitalize_nested函数:

t = ['this','that', ['other']]

def capitalize_nested(t):
    res = []
    for s in t:
        if type(s) == list:
            res.append(capitalize_nested(s))
        else:
            res.append(s.capitalize())
    return res

print capitalize_nested(t)

The recursive solution is first solution and most beautiful solution, but not always the best solution, Check this iterative solution too:递归解决方案是第一个解决方案和最漂亮的解决方案,但并不总是最好的解决方案,请检查此迭代解决方案:

def capitalize(t):
    lists = [t]
    while lists:
        l = lists.pop()
        for i, item in enumerate(l):
            if isinstance(item, list):
                lists.append(item)
            else:
                l[i] = item.capitalize()

Here's a version that supports an arbitrary deep nested list:这是一个支持任意深度嵌套列表的版本:

from collections import MutableSequence

def capitalize_inplace(nested_list):
    stack = [nested_list]
    while stack:
        lst = stack.pop()
        for i, item in enumerate(lst):
            if isinstance(item, MutableSequence):
                stack.append(item)
            else:
                lst[i] = item.capitalize()

Example例子

L = ['this', 'that', ['other'], ['may',['be', ['nested'], 'further']]]
capitalize_inplace(L)
print(L)
# -> ['This', 'That', ['Other'], ['May', ['Be', ['Nested'], 'Further']]]

暂无
暂无

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

相关问题 将嵌套字符串列表转换为嵌套字符串列表 - Converting a list of nested strings into a nested list of strings 构建一个 function 以字符串列表作为输入,返回一个 boolean 指示是否所有字符串都包含一个单词 - Build a function that takes a list of strings as input, returns a boolean indicating whether all the strings containing a word 计算嵌套列表中的字符串 - Count strings in nested list 嵌套列表-要浮动的字符串 - Nested List - Strings to Float 实现一个 function 反向字符串(我的列表),它接受一个字符串列表,并返回以相反顺序连接的字符串 - Implement a function reverse strings(my list) that takes a list of strings, and returns the strings concatenated in reverse order Python - 将字符串列表转换为嵌套的字符串列表 - Python - converting a list of strings into a nested list of strings 从嵌套的索引列表和嵌套的字符串列表创建字符串列表 - Create list of strings from nested list of indices and nested list of strings 获取字符串并返回8个字符的字符串列表的函数 - Function that takes a string and returns a list of 8-character strings 如何加入嵌套的字符串列表并将结果作为新的字符串列表? - How to join nested list of strings and get the result as new list of string? 该函数接受输入的字符串列表x并返回整数ptr - The function takes in input a list of strings x and returns an integer ptr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM