Py学习  »  Python

如何在Python中对列表中的字符串执行方法

lastwords • 5 年前 • 2065 次点击  

list_in_list = all_lists[0] #prints first list
string_in_list = list_in_list[0] #prints string in first list

我想创建一个for循环,它允许我用一个方法改变所有字符串,并将它们放回列表中,并将所有列表放回all_列表中。这就是我现在所拥有的:

new_list = []
for i in range(len(all_lists)): # all lists
    list_in_list = all_lists[i] # separate lists from all_lists
    for j in range(len(list_in_list)): # every list in all_list
        string_in_list = line[j] # separate string from list
        new_string = decontracted(string_in_list) # apply the method on string
        new_list.append(new_string) # put new string back in a list

我不确定如何将每个列表放回包含所有列表的列表中。有人能帮我吗?

例如,如果该方法将每个字符串大写:

发件人:

[['list one'],['list two'],['list...'],['list n']]

致:

[['LIST ONE'],['LIST TWO'],['LIST...'],['LIST N']]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55955
 
2065 次点击  
文章 [ 3 ]  |  最新文章 5 年前