Найти - Пользователи
Полная версия: Не получается создать словарь
Начало » Python для новичков » Не получается создать словарь
1
Tramp
Добрый день!

Есть такой код:
access = {'FastEthernet0/12': 10,
'FastEthernet0/14': 11,
'FastEthernet0/16': 17,
'FastEthernet0/17': 150}

access_template = ['switchport mode access',
'switchport access vlan',
'switchport nonegotiate',
'spanning-tree portfast',
'spanning-tree bpduguard enable']

a = []
access_conf = {}
for port in access:
a.clear()
for conf in access_template:
if conf == 'switchport access vlan':
a.append(str(conf) + ' ' + str(access[port]))
else:
a.append(conf)
# access_conf.append(a)
access_conf[port] = (a)
print(access_conf)

Необходимо, чтобы
access_conf = {'FastEthernet0/12': ['switchport mode access', 'switchport access vlan 10', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/14': ['switchport mode access', 'switchport access vlan 11', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/16': ['switchport mode access', 'switchport access vlan 17', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/17': ['switchport mode access', 'switchport access vlan 150', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable']}
Но получается (и через append тоже)
{'FastEthernet0/12': ['switchport mode access', 'switchport access vlan 150', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/14': ['switchport mode access', 'switchport access vlan 150', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/16': ['switchport mode access', 'switchport access vlan 150', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable'], 'FastEthernet0/17': ['switchport mode access', 'switchport access vlan 150', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable']}

Python 3.6.1
Подскажите, что не так?
Romissevd
Дональд, вот возможное решение
 access = {'FastEthernet0/12': 10,
           'FastEthernet0/14': 11,
           'FastEthernet0/16': 17,
           'FastEthernet0/17': 150}
access_template = ['switchport mode access',
                   'switchport access vlan',
                   'switchport nonegotiate',
                   'spanning-tree portfast',
                   'spanning-tree bpduguard enable']
for port, value in access.items():
    access[port] = access_template.copy()
    access[port][1] += ' ' + str(value)
print(access)
Tramp
Спасибо! Как все просто оказалось!
Не подскажите, почему мой вариант не работает?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB