从python中的列表项中删除单个符号和实践[]

2024-06-17 12:04:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个结构奇怪的列表,如下所示:

List_input = [['statment01'], ['statment02'], ['statment03'],.....['statement1000']]

我需要删除内括号[]和每个项目的单个符号

所需的输出列表如下所示:

List_output = ["statment01", "statment02", "statment03",....., "statement1000"]

有人能帮忙吗


Tags: 项目列表inputoutput符号结构list括号
2条回答

只需迭代列表并将其附加到新数组中

List_output = []
for i in List_input:
    List_output.append(i[0])

我希望这是你所期望的:

def remove_nest(l): 
    for i in l: 
        if type(i) == list: 
            remove_nest(i) 
        else: 
            output.append(i) 

l = [[1,2,3],[4,5,6],[6,7,8]] 
output = []    
print ('The given list: ', l) 
remove_nest(l) 
print ('The list after removing nesting: ', output) 

输出:

The given list:  [[1, 2, 3], [4, 5, 6], [6, 7, 8]]
The list after removing nesting:  [1, 2, 3, 4, 5, 6, 6, 7, 8]

相关问题 更多 >