Python - Nested Lists

487 views Asked by At

I'm trying to cross check two nested list values, and if I get a match, append the values from list1 into list2.

For example;

list1 = [[ip, hostname], [ip, hostname], [ip, hostname]]
list2 = [[ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location]]

I want to check if the value in list1[x][0] is in list2, if so, replace the value of List2[x][3] with List1[x][1].

What I'm attempting is;

count = 0;
for row in list2:
    if row[0] in hosts[count][0]:
        new_hostname = hosts[count][1]
        row[4].append(new_hostname)
        count += 1
    else:
         continue
         count += 1

I know the above is incorrect, i'm struggling to figure out how to access the values list1 whilst traversing list2. The reason is because I need to check each row, and then the value with the row and then amend specific values within the row.

Thanks for the RAPID response!

I've tried to implement the code given but am running into trouble when trying to create a diction from my list:

wow, you guys!

    def doStuff(list1, list2):
        mydic = dict(list2)
        for l in list1:
            l[3] = mydic.get(l[0], l[3])
        return mydic

new_dic = doStuff(hostname, old_rows) 

I'm receiving the error;

    mydic = dict(list2)
ValueError: dictionary update sequence element #0 has length 6; 2 is required

Any ideas?

3

There are 3 answers

4
Pynchia On BEST ANSWER

Assuming the ip field value supports equality comparison (==), this solution may help you. It's quite plain and lame and probably not very optimised, but it works.

Note: it works for every occurrence of each element of list1 in list2, so if you had multiple matches in list2, they would all get updated.

list1 = [['1.2.3.4', 'ciao.com'],
         ['1.2.3.5', 'prova.net'],
         ['1.2.2.2', 'uovo.net']]
list2 = [['1.2.3.4', '1.2.3.x', 'type', 'xyz.com', 'puglia'],
         ['1.2.3.7', '1.2.3.x', 'type', 'zyx.com', 'lazio'],
         ['1.2.3.5', '1.2.3.x', 'type', 'yxz.com', 'molise']]

print list1
print list2

for i in list1:
    ip = i[0]
    for j in list2:
        cur_ip = j[0]
        if ip == cur_ip:
            j[3] = i[1]

print list2

outputs:

[['1.2.3.4', 'ciao.com'], ['1.2.3.5', 'prova.net'], ['1.2.2.2', 'uovo.net']]
[['1.2.3.4', '1.2.3.x', 'type', 'xyz.com', 'puglia'], ['1.2.3.7', '1.2.3.x', 'type', 'zyx.com', 'lazio'], ['1.2.3.5', '1.2.3.x', 'type', 'yxz.com', 'molise']]
[['1.2.3.4', '1.2.3.x', 'type', 'ciao.com', 'puglia'], ['1.2.3.7', '1.2.3.x', 'type', 'zyx.com', 'lazio'], ['1.2.3.5', '1.2.3.x', 'type', 'prova.net', 'molise']]
0
wwii On

Sort the target list on ip then use itertools.groupby to iterate over the targets. It may have a slight advantage if there are a lot of items in list2 that don't need to be modified (they are skipped).

import operator, itertools
ip = operator.itemgetter(0)
keys = dict(list1)
list2.sort(key = ip)
for key, group in itertools.groupby(list2, ip):
    if key in keys:
        for item in group:
            item[3] = keys[key]
3
Dleep On

This works fine i guess, and its time complexity is only O(n + m) with n and m being the length of each list.

def doStuff(list1, list2):
    mydic = dict(list1)
    for l in list2:
        l[3] = mydic.get(l[0], l[3])