python - Accessing values of a dictionary in a list using lambda -
i'm newbie python. i've multiple dictionaries inside list , want analysis based on values. reason i'm using lambda because function not expected. i'm showing 2 dictionaries reference, output gives me multiple dictionaries @ times.
statistics = [{"ip_dst": "10.0.0.1", "ip_proto": "icmp", "ip_src": "10.0.0.3", "bytes": 1380, "port_dst": 0, "packets": 30, "port_src": 0}, {"ip_dst": "10.0.0.3", "ip_proto": "icmp", "ip_src": "10.0.0.1", "bytes": 1564, "port_dst": 0, "packets": 34, "port_src": 0}] packets = filter(lambda x: x[0]["packets"], statistics) ip_src = filter(lambda x: x[0]["ip_src"], statistics) ip_proto = filter(lambda x: x[0]["ip_proto"], statistics)
when i'm using print
statement, it's giving me key error: 0. understand value of packets integer , ip_src/ip_proto, value string.
how access these values using lambdas?
if trying extract packets separate list of items wouldn't use filter. filter reduce number of dictionaries in new list. use list comprehension,
packets = [x['packets'] x in statistics] print(packets) # [30, 34]
this creates list of x['packets']
values in statistics.
the same other 2 sets of values.
Comments
Post a Comment