使用递归展平嵌套列表的Python程序
给定一个嵌套列表,任务是编写一个Python程序来使用递归来展平嵌套列表。
例子:
Input: [[8, 9], [10, 11, ‘geeks’], [13]]
Output: [8, 9, 10, 11, ‘geeks’, 13]
Input: [[‘A’, ‘B’, ‘C’], [‘D’, ‘E’, ‘F’]]
Output: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’]
循序渐进的方法:
- 首先,我们尝试将一个变量初始化到链表中。
- 然后,我们的下一个任务是将我们的列表作为参数传递给用于展平列表的递归函数。
- 在该递归函数中,如果我们发现列表为空,则返回该列表。
- 否则,我们以递归形式调用函数及其子列表作为参数,直到列表变平。
- 最后,我们将打印扁平列表作为输出。
下面是一些基于上述方法的Python程序:
示例 1:
Python3
# Python program to flatten a nested list
# explicit function to flatten a
# nested list
def flattenList(nestedList):
# check if list is empty
if not(bool(nestedList)):
return nestedList
# to check instance of list is empty or not
if isinstance(nestedList[0], list):
# call function with sublist as argument
return flattenList(*nestedList[:1]) + flattenList(nestedList[1:])
# call function with sublist as argument
return nestedList[:1] + flattenList(nestedList[1:])
# Driver Code
nestedList = [[8, 9], [10, 11, 'geeks'], [13]]
print('Nested List:\n', nestedList)
print("Flattened List:\n", flattenList(nestedList))
Python3
# Python program to flatten a nested list
# explicit function to flatten a
# nested list
def flattenList(nestedList):
# check if list is empty
if not(bool(nestedList)):
return nestedList
# to check instance of list is empty or not
if isinstance(nestedList[0], list):
# call function with sublist as argument
return flattenList(*nestedList[:1]) + flattenList(nestedList[1:])
# call function with sublist as argument
return nestedList[:1] + flattenList(nestedList[1:])
# Driver Code
nestedList = [['A', 'B', 'C'], ['D', 'E', 'F']]
print('Nested List:\n', nestedList)
print("Flattened List:\n", flattenList(nestedList))
Python3
# Python program to flatten a nested list
# explicit function to flatten a
# nested list
def flattenList(nestedList):
# check if list is empty
if not(bool(nestedList)):
return nestedList
# to check instance of list is empty or not
if isinstance(nestedList[0], list):
# call function with sublist as argument
return flattenList(*nestedList[:1]) + flattenList(nestedList[1:])
# call function with sublist as argument
return nestedList[:1] + flattenList(nestedList[1:])
# Driver Code
nestedList = [[1], [2], [3], [4], [5]]
print('Nested List:\n', nestedList)
print("Flattened List:\n", flattenList(nestedList))
输出:
Nested List:
[[8, 9], [10, 11, 'geeks'], [13]]
Flattened List:
[8, 9, 10, 11, 'geeks', 13]
示例 2:
蟒蛇3
# Python program to flatten a nested list
# explicit function to flatten a
# nested list
def flattenList(nestedList):
# check if list is empty
if not(bool(nestedList)):
return nestedList
# to check instance of list is empty or not
if isinstance(nestedList[0], list):
# call function with sublist as argument
return flattenList(*nestedList[:1]) + flattenList(nestedList[1:])
# call function with sublist as argument
return nestedList[:1] + flattenList(nestedList[1:])
# Driver Code
nestedList = [['A', 'B', 'C'], ['D', 'E', 'F']]
print('Nested List:\n', nestedList)
print("Flattened List:\n", flattenList(nestedList))
输出:
Nested List:
[['A', 'B', 'C'], ['D', 'E', 'F']]
Flattened List:
['A', 'B', 'C', 'D', 'E', 'F']
示例 3:
蟒蛇3
# Python program to flatten a nested list
# explicit function to flatten a
# nested list
def flattenList(nestedList):
# check if list is empty
if not(bool(nestedList)):
return nestedList
# to check instance of list is empty or not
if isinstance(nestedList[0], list):
# call function with sublist as argument
return flattenList(*nestedList[:1]) + flattenList(nestedList[1:])
# call function with sublist as argument
return nestedList[:1] + flattenList(nestedList[1:])
# Driver Code
nestedList = [[1], [2], [3], [4], [5]]
print('Nested List:\n', nestedList)
print("Flattened List:\n", flattenList(nestedList))
输出:
Nested List:
[[1], [2], [3], [4], [5]]
Flattened List:
[1, 2, 3, 4, 5]