如何删除 python 中嵌套列表中的嵌套列表?
How to Remove nested list which is inside a nested list in python?
我想删除嵌套列表中的所有嵌套列表。这意味着
x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]
我想删除索引 1,2,3,4... 中的嵌套列表并使其成为平面列表。为了清楚起见,下面是列表中的分隔值。
['-e']
['-d', ['-e'], '-d']
['-c', ['-d', ['-e'], '-d'], '-c']
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b']
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
我想要它
[['-e'], ['-d', '-e', '-d'], ['-c', '-d', '-e', '-d', '-c'], ['-b', '-c', '-d', '-e', '-d', '-c', '-b'], ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]
['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']
有没有办法像上面那样获得输入。
for i in range(0,size):
z = ['-{}'.format(alnum[size-i])]
if alnum[size] != alnum[size-i]:
x.append(z*2)
else:
x.append(z)
这是我用来获取 x 列表的片段。
具有用于展平嵌套子列表的辅助递归函数以及列表理解:
def nested_flatten(seq):
# start with an empty list
result = []
# for each item in the sublist...
for item in seq:
# is item a list?
if isinstance(item, list):
# then extend the result with the flattened item
result += nested_flatten(item)
else:
# otherwise extend with the item itself
result += [item]
return result
# invoke `nested_flatten` on each sublist
out = [nested_flatten(sub) for sub in x]
得到
>>> out
[['-e'],
['-d', '-e', '-d'],
['-c', '-d', '-e', '-d', '-c'],
['-b', '-c', '-d', '-e', '-d', '-c', '-b'],
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]
python-convert-list-of-lists-or-nested-list-to-flat-list
def flattenNestedList(nestedList):
''' Converts a nested list to a flat list '''
flatList = []
# Iterate over all the elements in given list
for elem in nestedList:
# Check if type of element is list
if isinstance(elem, list):
# Extend the flat list by adding contents of this element (list)
flatList.extend(flattenNestedList(elem))
else:
# Append the elemengt to the list
flatList.append(elem)
return flatList
x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]
for li in x:
print(flattenNestedList(li))
输出:
['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']
这段代码使用递归来实现你所需要的。
from copy import deepcopy
x = [
['-e'],
['-d', ['-e'], '-d'],
['-c', ['-d', ['-e'], '-d'], '-c'],
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'],
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
]
temp_list = []
def delist(x):
for i in x:
if type(i) == list:
delist(i)
else:
temp_list.append(i)
new_list = []
for item in x:
delist(item)
new_list.append(deepcopy(temp_list))
temp_list.clear()
print('Resultant:', new_list)
我想删除嵌套列表中的所有嵌套列表。这意味着
x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]
我想删除索引 1,2,3,4... 中的嵌套列表并使其成为平面列表。为了清楚起见,下面是列表中的分隔值。
['-e']
['-d', ['-e'], '-d']
['-c', ['-d', ['-e'], '-d'], '-c']
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b']
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
我想要它
[['-e'], ['-d', '-e', '-d'], ['-c', '-d', '-e', '-d', '-c'], ['-b', '-c', '-d', '-e', '-d', '-c', '-b'], ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]
['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']
有没有办法像上面那样获得输入。
for i in range(0,size):
z = ['-{}'.format(alnum[size-i])]
if alnum[size] != alnum[size-i]:
x.append(z*2)
else:
x.append(z)
这是我用来获取 x 列表的片段。
具有用于展平嵌套子列表的辅助递归函数以及列表理解:
def nested_flatten(seq):
# start with an empty list
result = []
# for each item in the sublist...
for item in seq:
# is item a list?
if isinstance(item, list):
# then extend the result with the flattened item
result += nested_flatten(item)
else:
# otherwise extend with the item itself
result += [item]
return result
# invoke `nested_flatten` on each sublist
out = [nested_flatten(sub) for sub in x]
得到
>>> out
[['-e'],
['-d', '-e', '-d'],
['-c', '-d', '-e', '-d', '-c'],
['-b', '-c', '-d', '-e', '-d', '-c', '-b'],
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]
python-convert-list-of-lists-or-nested-list-to-flat-list
def flattenNestedList(nestedList):
''' Converts a nested list to a flat list '''
flatList = []
# Iterate over all the elements in given list
for elem in nestedList:
# Check if type of element is list
if isinstance(elem, list):
# Extend the flat list by adding contents of this element (list)
flatList.extend(flattenNestedList(elem))
else:
# Append the elemengt to the list
flatList.append(elem)
return flatList
x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]
for li in x:
print(flattenNestedList(li))
输出:
['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']
这段代码使用递归来实现你所需要的。
from copy import deepcopy
x = [
['-e'],
['-d', ['-e'], '-d'],
['-c', ['-d', ['-e'], '-d'], '-c'],
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'],
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
]
temp_list = []
def delist(x):
for i in x:
if type(i) == list:
delist(i)
else:
temp_list.append(i)
new_list = []
for item in x:
delist(item)
new_list.append(deepcopy(temp_list))
temp_list.clear()
print('Resultant:', new_list)