列表联盟

时间:2018-05-24 09:13:31

标签: python python-3.x python-2.7 list dictionary

我需要以下列方式在列表中的dicts之间建立联合:

当前:[{A},{B},{C},{D},...]

预期:[{A,B},{A,C},{A,D},...]

我有以下代码:

import xlsxwriter

word='Chain'

def create_chain(chain_segment):
    chains=[]
    chain_lines = [line for line in chain_segment.split('\n') if line]
    for line in chain_lines:
        chain={}
        if word in line:
            chain['type'] = line.split()[1]
        elif line[0].isdigit():
            chain['num']=line[0]
            chain['pkts']=line.split()[1]
            chain['bytes']=line.split()[2]
            chain['target']=line.split()[3]
            chain['prot']=line.split()[4]
            chain['opt']=line.split()[5]
            chain['in']=line.split()[6]
            chain['out']=line.split()[7]
            chain['source']=line.split()[8]
            chain['destination']=line.split()[9]
        chains.append(chain)
        chains=filter(None, chains)
    return chains

with open('/media/sf_vboxshared/iptables-list.log') as f:
    log_content = f.read()

host_sections = [host for host in log_content.split('---') if host]
hosts = {}

for host in host_sections:
    hostname, chains_segment = host.split('\n', 1)
    hostname = hostname.strip()
    chains=[]
    for segment in chains_segment.split('\n\n'):
            chains.extend(create_chain(segment))
    chains=filter(None,chains)
    hosts[hostname] = chains

workbook=xlsxwriter.Workbook('/media/sf_vboxshared/iptables-1st.xlsx')
worksheet1=workbook.add_worksheet('Sheet1')

worksheet1.write(0,0,'hostname')
worksheet1.write(0,1,'chain')
worksheet1.write(0,2,'num')
worksheet1.write(0,3,'pkts')
worksheet1.write(0,4,'bytes')
worksheet1.write(0,5,'target')
worksheet1.write(0,6,'prot')
worksheet1.write(0,7,'opt')
worksheet1.write(0,8,'in')
worksheet1.write(0,9,'out')
worksheet1.write(0,10,'source')
worksheet1.write(0,11,'destination')
row = 1

for host, chains in hosts.items():
    for chain in chains:
        worksheet1.write(row, 1, chain.get('type'))
        worksheet1.write(row, 0, host)
        worksheet1.write(row, 2, chain.get('num'))
        worksheet1.write(row, 3, chain.get('pkts'))
        worksheet1.write(row, 4, chain.get('bytes'))
        worksheet1.write(row, 5, chain.get('target'))
        worksheet1.write(row, 6, chain.get('prot'))
        worksheet1.write(row, 7, chain.get('opt'))
        worksheet1.write(row, 8, chain.get('in'))
        worksheet1.write(row, 9, chain.get('out'))
        worksheet1.write(row, 10, chain.get('source'))
        worksheet1.write(row, 11, chain.get('destination'))
        row += 1

workbook.close()

目前,代码输出的一小部分是:

[{'type': 'INPUT'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '281M', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '229K', 'out': '*'}, {'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '1560', 'source': '0.0.0.0/0', 'num': '2', 'in': '*', 'pkts': '26', 'out': '*'}, {'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '1560', 'source': '0.0.0.0/0', 'num': '3', 'in': '*', 'pkts': '26', 'out': '*'}, {'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '3120', 'source': '0.0.0.0/0', 'num': '4', 'in': '*', 'pkts': '52', 'out': '*'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '929K', 'source': '0.0.0.0/0', 'num': '5', 'in': '*', 'pkts': '15480', 'out': '*'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'icmp', 'bytes': '1510K', 'source': '0.0.0.0/0', 'num': '6', 'in': '*', 'pkts': '15415', 'out': '*'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '7', 'in': 'lo', 'pkts': '0', 'out': '*'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'REJECT', 'prot': 'all', 'bytes': '4875K', 'source': '0.0.0.0/0', 'num': '8', 'in': '*', 'pkts': '65200', 'out': '*'}]
[{'type': 'FORWARD'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '0', 'out': '*'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'REJECT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '2', 'in': '*', 'pkts': '0', 'out': '*'}]
[{'type': 'OUTPUT'}, {'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '45M', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '334K', 'out': '*'}]

预期产出:

[{'type': 'INPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '281M', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '229K', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '1560', 'source': '0.0.0.0/0', 'num': '2', 'in': '*', 'pkts': '26', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '1560', 'source': '0.0.0.0/0', 'num': '3', 'in': '*', 'pkts': '26', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '10.243.0.43', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '3120', 'source': '0.0.0.0/0', 'num': '4', 'in': '*', 'pkts': '52', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'tcp', 'bytes': '929K', 'source': '0.0.0.0/0', 'num': '5', 'in': '*', 'pkts': '15480', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'icmp', 'bytes': '1510K', 'source': '0.0.0.0/0', 'num': '6', 'in': '*', 'pkts': '15415', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '7', 'in': 'lo', 'pkts': '0', 'out': '*'}, {'type': 'INPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'REJECT', 'prot': 'all', 'bytes': '4875K', 'source': '0.0.0.0/0', 'num': '8', 'in': '*', 'pkts': '65200', 'out': '*'}]
[{'type': 'FORWARD', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '0', 'out': '*'}, {'type': 'FORWARD', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'REJECT', 'prot': 'all', 'bytes': '0', 'source': '0.0.0.0/0', 'num': '2', 'in': '*', 'pkts': '0', 'out': '*'}]
[{'type': 'OUTPUT', 'opt': '--', 'destination': '0.0.0.0/0', 'target': 'ACCEPT', 'prot': 'all', 'bytes': '45M', 'source': '0.0.0.0/0', 'num': '1', 'in': '*', 'pkts': '334K', 'out': '*'}]

这里也是iptables-list.log:

---node1
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        1     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:123
2       25  16K  ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:123
3        7   28  ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:53
4       58  39K  ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:53
5      81K  25M  ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 398 packets, 23K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     2K      3M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node2
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:123
2       62    4K ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:123
3        6   214 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:53
4       58   30K ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:53

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 34 packets, 18K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      27K    3M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node3
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       32    4K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:993
2       10   24K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
3       36   59K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:25

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 57 packets, 3K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       1K    3M ACCEPT     all  --  *      *       0.0.0.0/0             0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node4
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       9K  8M   ACCEPT      all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        1  78   ACCEPT      tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
4        0   0   ACCEPT     icmp  --  *      *       0.0.0.0/0            0.0.0.0/0           
5      52K 1M    REJECT      all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 1K packets, 1M bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       1K   3M ACCEPT      all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node5
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       2M   2G  ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2       21  13K  ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW   
5        2   50  REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 72778 packets, 5392K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       9M    3G ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node6
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       8K  8M   ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        1  93   ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
3        1  60   ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4       44  20   ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5       59  29   REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 8 packets, 42K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       1M    7M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
---node7
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      19K   21M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2       26    10 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.243.0.43          tcp dpt:4000 ctstate NEW
8       60   45K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject

Chain OUTPUT (policy ACCEPT 11 packets, 60K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       3K   41M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED

有没有办法实现这个目标?

非常感谢, 罗曼

1 个答案:

答案 0 :(得分:2)

这是使用列表理解的一种方法。注意**语法需要Python 3.6。

lst = [{'A': 1}, {'B': 2}, {'C': 3}, {'D': 4}] 

res = [{**lst[0], **i} for i in lst[1:]]

[{'A': 1, 'B': 2}, {'A': 1, 'C': 3}, {'A': 1, 'D': 4}]

在Python 2.7中,您可以定义一个函数来执行合并:

def merge_dicts(x, y):
    z = x.copy()
    z.update(y)
    return z

res = [merge_dicts(lst[0], i) for i in lst[1:]]
抱歉,我只读了你问题的前三行,这似乎已经足够了。我建议,如果这解决了您的解决方案,请相应地更新您的问题。