循环中的Python匹配字符串

2024-06-16 13:47:59 发布

您现在位置:Python中文网/ 问答频道 /正文

apdb = {'AP Database': [{'AP Type': '110H',
                  'Name': 'varagu',
                  'Public IP': '100.20.300.400',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
                 {'AP Type': '110H',
                  'Name': 'thinai',
                  'Public IP': '100.20.300.500',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
                 {'AP Type': '110H',
                  'Name': 'Ragi',
                  'Public IP': '100.20.300.600',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}


ap_database = apdb.get('AP Database')

apall = ap_database[0], ap_database[1], ap_database[2]

for ap in apall:

    public = ap.__getitem__('Public IP')
    name = ap.__getitem__('Name')
    ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
    for ip in ip_list:
        if ip == public:
            print public  + ' ' + name + ' ' + "Success"

我受够了。我需要将“ip”与“public”映射。你知道吗

预期结果:

结果1:(这需要存储在变量中,因为我需要在邮件正文中发送)

100.20.300.400瓦拉古成功

100.20.300.500蒂奈成功

结果2:(这需要存储在一个变量中,也需要通过单独的邮件发送)

100.20.300.600拉吉故障


Tags: nameipaddressmactypepublicdatabaseaa
3条回答

请参见下面的一些内联注释

apdb = {'AP Database': [{'AP Type': '110H',
              'Name': 'varagu',
              'Public IP': '100.20.300.400',
              'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
             {'AP Type': '110H',
              'Name': 'thinai',
              'Public IP': '100.20.300.500',
              'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
             {'AP Type': '110H',
              'Name': 'Ragi',
              'Public IP': '100.20.300.600',
              'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}


ap_database = apdb.get('AP Database')

# no need to do this since ap_database is already a list, so we can iterate over it.
# apall = ap_database[0], ap_database[1], ap_database[2]

# ip_list does not change, so we can define it once outside the loop
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']

# let us add two more variables to hold the fail and success list
success_list = []
fail_list = []

for ap in ap_database:

    # ap is a dict, so use ap.get('key') or ap['key'] instead of ap.__getitem__('key')
    # note that ap.get('key') returns None if key does not exist, while ap['key'] throws an error.
    public = ap.get('Public IP')
    name = ap.get('Name')

    # check whether public is in ip_list and print appropriate message
    if public in ip_list:
        print public, name, 'Success'
        success_list.append(ap)
    else:
        print public, name, 'Fail'
        fail_list.append(ap) 

有很多方法可以实现你的目标。你知道吗

解析apdb时,可以检查“Public IP”是否在ip_list中,然后附加failed_ipsucceed_ip列表。你知道吗

然后,您只需处理这两个列表:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

apdb = {'AP Database': [{'AP Type': '110H',
                  'Name': 'varagu',
                  'Public IP': '100.20.300.400',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
                 {'AP Type': '110H',
                  'Name': 'thinai',
                  'Public IP': '100.20.300.500',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
                 {'AP Type': '110H',
                  'Name': 'Ragi',
                  'Public IP': '100.20.300.600',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}


ap_database = apdb.get('AP Database')

apall = ap_database[0], ap_database[1], ap_database[2]
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
succeed_ip = []
failed_ip = []
for ap in apall:
    success = None
    failed = None
    public = ap.__getitem__('Public IP')
    name = ap.__getitem__('Name')
    if public in ip_list:
        succeed_ip.append((public, name))
        continue
    failed_ip.append((public, name))

print succeed_ip
print failed_ip

table = PrettyTable(['IP', 'Name', 'Result'])
table.add_row(succeed_ip[0])
table.add_row(succeed_ip[1])
print(table)

ftable = PrettyTable(['IP', 'Name', 'Result'])
ftable.add_row(failed_ip[0])
print(ftable)

结果:

[('100.20.300.400', 'varagu', 'success'), ('100.20.300.500', 'thinai', 'success')]
[('100.20.300.600', 'Ragi', 'Failed')]
+        +    +    -+
|       IP       |  Name  |  Result |
+        +    +    -+
| 100.20.300.400 | varagu | success |
| 100.20.300.500 | thinai | success |
+        +    +    -+
+        +   +    +
|       IP       | Name | Result |
+        +   +    +
| 100.20.300.600 | Ragi | Failed |
+        +   +    +

我对你的代码做了一些调整: 1) apall = ap_database[0], ap_database[1], ap_database[2]此赋值不是必需的,因为ap_database = apdb.get('AP Database')这将使ap\u数据库已经成为一个列表

2)ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']这应该是循环外的,不需要为每个循环重新分配

3)代替下面的2行,您可以直接在ip\列表中搜索公共ip,如:if public in ip_list:

for ip in ip_list:
        if ip == public:

4)不使用getitem而只使用get方法对我来说更具可读性(仅对我而言,如果您愿意,您可以使用getitem

apdb = {'AP Database': [{'AP Type': '110H',
                  'Name': 'varagu',
                  'Public IP': '100.20.300.400',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
                 {'AP Type': '110H',
                  'Name': 'thinai',
                  'Public IP': '100.20.300.500',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
                 {'AP Type': '110H',
                  'Name': 'Ragi',
                  'Public IP': '100.20.300.600',
                  'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}

ap_database = apdb.get('AP Database')
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
results = {'success': [], 'fail': []}

for ap in ap_database:
    public = ap.get('Public IP')
    name = ap.get('Name')
    if public in ip_list:
        results['success'].append(public)
    else:
        results['fail'].append(public)

print(results['success'], reuslts['fail'])

相关问题 更多 >