开发者

get list item which have count more than specific number by Python

I have nested list :

   ip[0] = ['23:30:42.476071', '55729', '192.168.98.138.49341', '201.20.49.239.80', '562034568', -1]
   ip[1] = ['23:30:43.110194', '20442', '201.20.49.239.80', '192.168.98.138.49341', '364925831', '562034569']
    ip[2] = ['23:30:43.110290', '55730', '19开发者_开发问答2.168.98.138.49341', '201.20.49.239.80', -1, '5840']
    ip[3] = ['23:30:43.170344', '55731', '192.168.98.138.49341', '201.20.49.239.80', '562034569:562034972', '364925832']
    ip[4] = ['23:30:43.170918', '20443', '201.20.49.239.80', '192.168.98.138.49341', -1, '64240']
    ip[5] = ['23:30:44.022511', '20444', '201.20.49.239.80', '192.168.98.138.49341', '364925832:364925978', '562034972']

I want get index and sublist from my original list that have ip[i][2] = 192.168.98.138

for the above list I want get :

    ip[0] = ['23:30:42.476071', '55729', '192.168.98.138.49341', '201.20.49.239.80', '562034568', -1]
    ip[2] = ['23:30:43.110290', '55730', '192.168.98.138.49341', '201.20.49.239.80', -1, '5840']
    ip[3] = ['23:30:43.170344', '55731', '192.168.98.138.49341', '201.20.49.239.80', '562034569:562034972', '364925832']


To return results where ip[i][2] == ip[0][2] use a list comprehension:

result = [ d for d in ip if d[2] == ip[0][2] ]


[addr for addr in ip if addr[2].startswith("192.168.98.138"]

which is the same as, though much neater than:

addrs = []
for addr in ip:
    if addr[2].startswith("192.168.98.138"):
        addrs.append(addr)


Thanks for clarifying. Use a list comprehension:

>>> ip = [['23:30:42.476071', '55729', '192.168.98.138.49341', '201.20.49.239.80', '562034568', -1], 
['23:30:43.110194', '20442', '201.20.49.239.80', '192.168.98.138.49341', '364925831', '562034569'],
['23:30:43.110290', '55730', '192.168.98.138.49341', '201.20.49.239.80', -1, '5840'],
['23:30:43.170344', '55731', '192.168.98.138.49341', '201.20.49.239.80', '562034569:562034972', '364925832'],
['23:30:43.170918', '20443', '201.20.49.239.80', '192.168.98.138.49341', -1, '64240'],
['23:30:44.022511', '20444', '201.20.49.239.80', '192.168.98.138.49341', '364925832:364925978', '562034972']]

>>> needle = ip[0][2]
>>> [item for item in ip if item[2]==needle]
[['23:30:42.476071', '55729', '192.168.98.138.49341', '201.20.49.239.80', '562034568', -1], 
['23:30:43.110290', '55730', '192.168.98.138.49341', '201.20.49.239.80', -1, '5840'], 
['23:30:43.170344', '55731', '192.168.98.138.49341', '201.20.49.239.80', '562034569:562034972', '364925832']]


If you want to get both the index and the sublist (according to what you describe), then the following might work:

>>> print [(index, x) for index, x in enumerate(ip) if x[2] == ip[0][2]]
[(0, ['23:30:42.476071', '55729', '192.168.98.138.49341', '201.20.49.239.80', '562034568', -1]), (2, ['23:30:43.110290', '55730', '192.168.98.138.49341', '201.20.49.239.80', -1, '5840']), (3, ['23:30:43.170344', '55731', '192.168.98.138.49341', '201.20.49.239.80', '562034569:562034972', '364925832'])]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜