Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate python nested lists efficiently

I am working on a network traffic monitor project in Python. Not that familiar with Python, so I am seeking help here.

In short, I am checking both in and out traffic, I wrote it this way:

for iter in ('in','out'):
        netdata = myhttp()
        print data

netdata is a list consisting of nested lists, its format is like this:

[ [t1,f1], [t2,f2], ...]

Here t represents the moment and f is the flow. However I just want to keep these f at this moment for both in and out, I wonder any way to get an efficient code.

After some search, I think I need to use create a list of traffic(2 elements), then use zip function to iterate both lists at the same time, but I have difficulty writing a correct one. Since my netdata is a very long list, efficiency is also very important.

If there is anything confusing, let me know, I will try to clarify. Thanks for help

like image 549
Jin Avatar asked Jun 09 '13 21:06

Jin


People also ask

How do you iterate over two or more lists at the same time?

Iterate over multiple lists at a time We can iterate over lists simultaneously in ways: zip() : In Python 3, zip returns an iterator. zip() function stops when anyone of the list of all the lists gets exhausted. In simple words, it runs till the smallest of all the lists.

How does Python handle nested lists?

Adding Lists to a Two Dimensional Array We can add an element to a nested list with the append() function. In our example, we create a list and append it to one of our existing lists from above. This should result in this list: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2], [8, 7, 6]]

Can you nest list comprehensions?

As it turns out, you can nest list comprehensions within another list comprehension to further reduce your code and make it easier to read still. As a matter of fact, there's no limit to the number of comprehensions you can nest within each other, which makes it possible to write very complex code in a single line.


1 Answers

Apart from minor fixes on your code (the issues raised by @Zero Piraeus), your question was probably answered here. A possible code to traverse a list of lists in N degree (a tree) is the following:

def traverse(item):
    try:
        for i in iter(item):
            for j in traverse(i):
                yield j
    except TypeError:
        yield item

Example:

l = [1, [2, 3], [4, 5, [[6, 7], 8], 9], 10]
print [i for i in traverse(l)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The key to make it work is recursion and the key to make it work efficiently is using a generator (the keyword yield gives the hint). The generator will iterate through your list of lists an returning to you item by item, without needing to copy data or create a whole new list (unless you consume the whole generator assigning the result to a list, like in my example)

Using iterators and generators can be strange concepts to understand (the keyword yield mainly). Checkout this great answer to fully understand them

like image 169
Bruno Penteado Avatar answered Oct 11 '22 14:10

Bruno Penteado