0
点赞
收藏
分享

微信扫一扫

Python---内置方法

Sikj_6590 2023-10-06 阅读 65

列表展开

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

from iteration_utilities import deepflatten

  

# if you only have one depth nested_list, use this

def flatten(l):

  return [item for sublist in l for item in sublist]

  

l = [[1,2,3],[3]]

print(flatten(l))

# [1, 2, 3, 3]

  

# if you don't know how deep the list is nested

l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]

  

print(list(deepflatten(l, depth=3)))

# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

举报

相关推荐

0 条评论