简体   繁体   中英

Is there a more pythonic way to merge the maximum number of a element of two lists into one

I'm writing a packet diff tool in python using scapy to find the differences in two pcap files and then output the difference in a human readable format. The script compares each packet on an individual basis, and splits up the different layers/protocols/im-not-that-much-of-a-networking-guy-sorry to compare individually. This is where my dilemma begins, you don't know what layers are present, or how many, or if there are multiple of the same layer, or how the two packets may have completely different layers. My plan to go through the differences was to pull out the names of the layers and then smash the two lists together and use that list to know what sort of layers i should be looking through the two packets for. I'm not quite sure if this is the best way to go about it, but its all I can really think of. If you have a better Idea on how to do it, PLEASE share

tl;dr I need to figure out how to 'merge' two lists of layer names together so I can compare two packets

I tried to write it, but I couldn't quite get what I wanted. Then I was told that it looked like c written in python and to make it more pythonic, which I completely understand.

def get_common_layers(layers_1, layers_2):  # doesn't quite work
    layers_total = []
    i = 0
    while i < len(layers_1):
        temp_count = layers_1.count(layers_1[i])
        if layers_1[i] not in layers_total:
            if layers_1[i] not in layers_2:
                layers_total.extend(layers_1[i:i + temp_count])
            elif layers_1[i] in layers_2:
                if temp_count >= layers_2.count(layers_1[i]):
                    layers_total.extend(layers_1[i:i + temp_count])
        i = i + temp_count
    i = 0
    while i < len(layers_2):
        temp_count = layers_2.count(layers_2[i])
        if layers_2[i] not in layers_total:
            if layers_2[i] not in layers_1:
                layers_total.extend(layers_2[i:i + temp_count])
            elif layers_2[i] in layers_1:
                if temp_count >= layers_1.count(layers_2[i]):
                    layers_total.extend(layers_2[i:i + temp_count])
        i = i + temp_count
    return layers_total

This is kind of close, but its a bit off. I'm sorry that I haven't really been able to explain what I mean, but the unittest and the desired input and output should give a bit better of a picture

desired input and output:

layers_1 = ['Ether', 'UDP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR'],
layers_2 = ['Ether', 'TCP', 'DNS', 'DNSRR', 'DNSRR', 'DNSQR'])

layers_total = ['Ether', 'UDP', 'TCP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR', 'DNSRR']

screenshot of the error that unittest shows: https://imgur.com/UFi92jY.png "unittest"

screenshot of what i'm trying to actually do: https://imgur.com/eMZNX5V.png "example_output"

(would have had pics show up but new account)

Are you looking for the union of the two lists? This should work:

layers_total = list(set(layers_1).union(set(layers_2)))

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