简体   繁体   中英

Python: Iterate through list, output blank to second list to then call in order

Thanks for taking the time to help me out. Still a noob when it comes to Python but I'm slowly getting there.

Backstory: Basically I'm transferring configuration from a PBX to a CSV file to then migrate to another system.

Where I'm stuck: Currently I have a list of both extension numbers and then voice mail configuration. Not every phone has a voice mail email address so currently what I'm doing is comparing the data to see If the current extension has an email associated. Because of the way the data is exported, I'm storing everything to a list and It all needs to line up.

What I need it to do: I need the emails to output nicely to a list and have that line up with the extension list. Eg:

list_extension_email = [' ', 'test@test', ' ', ' ', 'test2@test']

list_extension_numbers = ['1000', '401', '402', '403', '404']

Code:

list_extension_voicemail = ['401, test@test', '404, test2@test']
list_extension_numbers = ['1000', '401', '402', '403', '404']
list_extension_email = []
line_variable = 0
blank_voicemails = 0

for extension in list_extension_numbers:
    extension_number = list_extension_numbers[line_variable]
    blank_line = " "
    for extension_voicemail in list_extension_voicemail:
      extension_and_voicemail = extension_voicemail.split(',')
      if extension_and_voicemail[0] in extension_number:
        while blank_voicemails != 0:
          list_extension_email.append(blank_line)
          blank_voicemails-=1
        list_extension_email.append(extension_and_voicemail[1])
        blank_voicemails = 0
      else:
        blank_voicemails+=1


extension_email = list_extension_email[line_variable]

Sorry if this is very vague and non descriptive, I'm open to hearing any solutions. Once again, thanks all

I think this does what you want:

list_extension_voicemail = ['401, test@test', '404, test2@test']
list_extension_numbers = ['1000', '401', '402', '403', '404']

# Make list of empty emails
list_extension_email = [''] * len(list_extension_numbers)
# Make a dict that gives you the list index for each number
number_idx = {number: idx for idx, number in enumerate(list_extension_numbers)}
for number_email in list_extension_voicemail:
    # Split by comma
    number, email = number_email.split(',')
    # Remove extra spaces
    number = number.strip()
    email = email.strip()
    # Set email in list if number exists
    if number in number_idx:
        list_extension_email[number_idx[number]] = email

# Print result
print('list_extension_email =', list_extension_email)
print('list_extension_numbers =', list_extension_numbers)

Output:

list_extension_email = ['', 'test@test', '', '', 'test2@test']
list_extension_numbers = ['1000', '401', '402', '403', '404']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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