简体   繁体   中英

Flattening the list in python

I have a list called normalized_rec, it has:

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.

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.

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. 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)']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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