简体   繁体   English

扁平化python中的列表

[英]Flattening the list in python

I have a list called normalized_rec, it has: 我有一个名为normalized_rec的列表,它具有:

normalized_rec = [[[['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)']], [['rchan', 'pts/9', '10.40.91.236', 'Wed', 'Feb', '14', '00:00:00', '2018', '-', 'Wed', 'Feb', '14', '23:59:59', '2018', '(00:03)'], [['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)']]]], [['rchan', 'pts/9', '10.40.91.236', 'Tue', 'Feb', '13', '16:53:42', '2018', '-', 'Tue', 'Feb', '13', '23:59:59', '2018', '(00:03)'], [[['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)']], [['rchan', 'pts/9', '10.40.91.236', 'Wed', 'Feb', '14', '00:00:00', '2018', '-', 'Wed', 'Feb', '14', '23:59:59', '2018', '(00:03)'], [['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)']]]]], ['cwsmith', 'pts/6', '10.40.43.94', 'Tue', 'Feb', '13', '16:51:47', '2018', '-', 'Tue', 'Feb', '13', '16:56:13', '2018', '(00:04)'], ['mlee18', 'pts/6', '10.40.43.94', 'Tue', 'Feb', '13', '16:50:20', '2018', '-', 'Tue', 'Feb', '13', '16:51:27', '2018', '(00:01)'], ['hfang', 'pts/4', '24.114.50.50', 'Tue', 'Feb', '13', '16:31:38', '2018', '-', 'Tue', 'Feb', '13', '17:48:39', '2018', '(01:17)'], ['bigia', 'pts/8', '24.114.50.50', 'Tue', 'Feb', '13', '19:28:43', '2018', '-', 'Tue', 'Feb', '13', '20:28:31', '2018', '(00:59)'], ['rchan', 'pts/2', '10.40.105.130', 'Tue', 'Feb', '13', '16:22:00', '2018', '-', 'Tue', 'Feb', '13', '16:45:00', '2018', '(00:23)'], ['asmith', 'pts/2', '10.43.115.162', 'Tue', 'Feb', '13', '16:19:29', '2018', '-', 'Tue', 'Feb', '13', '16:22:00', '2018', '(00:02)'], ['tsliu2', 'pts/4', '10.40.105.130', 'Tue', 'Feb', '13', '16:17:21', '2018', '-', 'Tue', 'Feb', '13', '16:30:10', '2018', '(00:12)'], ['mshana', 'pts/13', '10.40.91.247', 'Tue', 'Feb', '13', '16:07:52', '2018', '-', 'Tue', 'Feb', '13', '16:45:52', '2018', '(00:38)'], ['asmith', 'pts/11', '10.40.105.130', 'Tue', 'Feb', '13', '14:07:43', '2018', '-', 'Tue', 'Feb', '13', '16:07:43', '2018', '(02:00)'], ['asmith', 'pts/11', '10.40.105.130', 'Wed', 'Feb', '14', '14:07:43', '2018', '-', 'Wed', 'Feb', '14', '16:07:43', '2018', '(02:00)']]

I have a program that only accepts a list inside a list. 我有一个只接受列表中列表的程序。 The nesting of a list inside 2 list breaks my code. 在2个列表内嵌套一个列表会破坏我的代码。

I want to flatten everything out so that it would look like 我想把所有东西弄平,以便看起来像

normalized_rec = [['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Wed', 'Feb', '14', '00:00:00', '2018', '-', 'Wed', 'Feb', '14', '23:59:59', '2018', '(00:03)'], .....['asmith', 'pts/11', '10.40.105.130', 'Wed', 'Feb', '14', '14:07:43', '2018', '-', 'Wed', 'Feb', '14', '16:07:43', '2018', '(02:00)']]

I just want the list to go in 1 level. 我只希望列表进入1级。

I tried 我试过了

def flatten(some_list):
    for items in some_list:
        try:
            yield from flatten(items)
        except TypeError:
            yield items

But that just flattens everything out and gives me 但这只是使一切变得平淡无奇,给了我

RuntimeError: maximum recursion depth exceeded

My list normalized_rec always changes. 我的列表normalized_rec总是会更改。 Any help is appreciated. 任何帮助表示赞赏。

You can check that a sublist contains all non-list elements: 您可以检查子列表是否包含所有非列表元素:

def flatten(d):
  for i in d:
    if all(not isinstance(c, list) for c in i):
       yield i
    else:
       yield from flatten(i)

print(list(flatten(normalized_rec)))

Output: 输出:

[['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Wed', 'Feb', '14', '00:00:00', '2018', '-', 'Wed', 'Feb', '14', '23:59:59', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Tue', 'Feb', '13', '16:53:42', '2018', '-', 'Tue', 'Feb', '13', '23:59:59', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Wed', 'Feb', '14', '00:00:00', '2018', '-', 'Wed', 'Feb', '14', '23:59:59', '2018', '(00:03)'], ['rchan', 'pts/9', '10.40.91.236', 'Thu', 'Feb', '15', '00:00:00', '2018', '-', 'Tue', 'Feb', '15', '16:57:02', '2018', '(00:03)'], ['cwsmith', 'pts/6', '10.40.43.94', 'Tue', 'Feb', '13', '16:51:47', '2018', '-', 'Tue', 'Feb', '13', '16:56:13', '2018', '(00:04)'], ['mlee18', 'pts/6', '10.40.43.94', 'Tue', 'Feb', '13', '16:50:20', '2018', '-', 'Tue', 'Feb', '13', '16:51:27', '2018', '(00:01)'], ['hfang', 'pts/4', '24.114.50.50', 'Tue', 'Feb', '13', '16:31:38', '2018', '-', 'Tue', 'Feb', '13', '17:48:39', '2018', '(01:17)'], ['bigia', 'pts/8', '24.114.50.50', 'Tue', 'Feb', '13', '19:28:43', '2018', '-', 'Tue', 'Feb', '13', '20:28:31', '2018', '(00:59)'], ['rchan', 'pts/2', '10.40.105.130', 'Tue', 'Feb', '13', '16:22:00', '2018', '-', 'Tue', 'Feb', '13', '16:45:00', '2018', '(00:23)'], ['asmith', 'pts/2', '10.43.115.162', 'Tue', 'Feb', '13', '16:19:29', '2018', '-', 'Tue', 'Feb', '13', '16:22:00', '2018', '(00:02)'], ['tsliu2', 'pts/4', '10.40.105.130', 'Tue', 'Feb', '13', '16:17:21', '2018', '-', 'Tue', 'Feb', '13', '16:30:10', '2018', '(00:12)'], ['mshana', 'pts/13', '10.40.91.247', 'Tue', 'Feb', '13', '16:07:52', '2018', '-', 'Tue', 'Feb', '13', '16:45:52', '2018', '(00:38)'], ['asmith', 'pts/11', '10.40.105.130', 'Tue', 'Feb', '13', '14:07:43', '2018', '-', 'Tue', 'Feb', '13', '16:07:43', '2018', '(02:00)'], ['asmith', 'pts/11', '10.40.105.130', 'Wed', 'Feb', '14', '14:07:43', '2018', '-', 'Wed', 'Feb', '14', '16:07:43', '2018', '(02:00)']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM