简体   繁体   English

检查元素是否已经在列表中?

[英]check if element is in list already?

I have a list of lists as follows: 我有一个列表列表,如下所示:

aList= [['192.168.1.3', '0080.4522.ad08', '2013/05/02 19:10:10', 'automatic'],
['192.168.1.2', '0080.4522.ad08', '2013/05/02 19:05:00', 'automatic']]

When adding another list I want to check if the ip or mac address are in another list before adding them, if they are already in the list of lists I don't want to add them. 当添加另一个列表时,我想在添加它们之前检查ip或mac地址是否在另一个列表中,如果它们已经在列表列表中,我不想添加它们。 For instance if I was testing 192.168.1.3 it would not add it to the list as that element exists somewhere already. 例如,如果我正在测试192.168.1.3,则不会将其添加到列表中,因为该元素已经存在。 So I may be trying to add this list again but I want it to fail: 因此,我可能试图再次添加此列表,但我希望它失败:

['192.168.1.3', '0080.4522.ad08', '2013/05/02 19:10:10', 'automatic']

The way I have been doing it is looping through the lists and setting a boolean once the element is found, but I don't feel this way is great. 我一直在做的方式是遍历列表,并在找到元素后设置一个布尔值,但是我觉得这种方式不是很好。 How would you do it? 你会怎么做? I know that IP will always be element one of each list but relying on index also seems a bad way to do things. 我知道IP永远是每个列表中的元素之一,但是依赖索引似乎也做事不好。

You can use any : 您可以使用any

>>> lis = ['192.168.1.3', '0080.4522.ad08', '2013/05/02 19:10:10', 'automatic']
if any(item[0] == lis[0] or item[1] == lis[1]  for item in aList)
    #then don't add
else:
    #add

A good way to solve this is to use objects, and make the list a list of objects instead of a list of list/tuples. 解决此问题的一种好方法是使用对象,并使列表成为对象列表,而不是列表/元组列表。 You could then override the compare-function for objects and use the normal "in" statement. 然后,您可以覆盖对象的比较功能,并使用常规的“ in”语句。

class Client:
    def __init__(self,  ip, mac, timedate, mode):
        self.ip = ip
        self.mac = mac
        self.timedate = timedate
        self.mode = mode

    def __eq__(self, object):
        if type(object) != type(self):
            return False

        if object.ip == self.ip or object.mac == self.mac
            return True
        return False

you could then do something like 然后您可以做类似的事情

client = Client("127.0.0.1", "abwdds", date, mode)
if client not in list:
    doSomething()
    list.append(client)

如果IP地址是数据的关键列,则应使用字典将IP地址与其他列映射到元组(或列表)。

Use a dictionary to hold your connections. 使用字典来保持您的连接。

cons = [{'IP':'192.168.1.3', 'MAC':'0080.4522.ad08', 'timestamp':'2013/05/02 19:10:10', 'type':'automatic'},
        {'IP':'192.168.1.2', 'MAC':'0080.4522.ad08', 'timestamp':'2013/05/02 19:05:00', 'type':'automatic'}]

def add_con(con):
    if any((con['IP'] == c['IP'] or con['MAC'] == c['MAC']) for c in con):
        # duplicate
    else:
        # new

You could easily wrap this functionality into a class. 您可以轻松地将此功能包装到一个类中。


class ConnectionManager():

    def __init__(self):
        self.connections = []

    def add_con(ip, mac, timestamp, mode):
        if any((ip == c['IP'] or mac == c['MAC']) for c in self.connections):
            # duplicate
            return False # possible option?
        else:
            self.connections.append({'IP':ip, 'MAC':mac, 'timestamp':timestamp, 'mode':mode})
            return True # possible option?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM