Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DevNet Help
#2
temp is a dict. A dict can have any hashable object as a key, including strings. temp['name'] = name assigns the value associated with the variable name to the key 'name' in temp. Stepping through the code:

vlans_list = [
    'vlan 10', 'name DATA', 'vlan 20', 'name VOICE', 'vlan 30', 'name WIRELESS'
]
 
vlans = []
 
for item in vlans_list:                           # loop through vlans_list, assigning each member to item
    if 'vlan' in item:                            # check for vlan being in the string
        temp = {}                                 # create a new dictionary
        id = item.strip().strip('vlan').strip()   # remove vlan from the string, assign the result to id
        temp['id'] = id                           # store id in the new dictionary (key = 'id')
    elif 'name' in item:                          # check for name being in the string
        name = item.strip().strip('name').strip() # remove 'name' from the string, assign it to variable name
        temp['name'] = name                       # store name in the dictionary.
        vlans.append(temp)                        # store the dictionary with the vlan info in a list
Each vlan ends up with it's own dictionary in the list vlans.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
DevNet Help - by benniehanas - Aug-07-2019, 03:29 PM
RE: DevNet Help - by ichabod801 - Aug-07-2019, 05:25 PM
RE: DevNet Help - by benniehanas - Aug-07-2019, 07:41 PM
RE: DevNet Help - by ichabod801 - Aug-08-2019, 01:37 AM
RE: DevNet Help - by benniehanas - Aug-08-2019, 06:05 PM
RE: DevNet Help - by ichabod801 - Aug-08-2019, 06:28 PM
RE: DevNet Help - by benniehanas - Feb-04-2020, 11:51 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020