dimanche 19 janvier 2020

Python If loop isn't matching a string length test

First post here, so apologies ahead of time for any breaches in etiquette. This site has been enormously helpful to me, so thanks for that!

I have a list of strings that I am running a for loop on. The logic has been working without issue, but I had to add a new first IF statement to check for indices where the string within is longer than a certain length. I don't seem to be able to get that statement to match even though some of my lengths show longer than 60. What am I doing wrong here?

Here is the code:

for idx, x in enumerate(final_vlan_list_build2):
    length = len(final_vlan_list_build2[idx])
    print(length)
    print(type(length))
    if length >= 60:
        final_vlan_list_build2[idx] = final_vlan_list_build2[idx].replace(
            final_vlan_list_build2[idx], final_vlan_list_build2[idx] + "   default 1       none qtagged")
    elif 'qtagged' in x:
        final_vlan_list_build2[idx] = final_vlan_list_build2[idx]
    elif 'qtagged' and 'default' not in x:
        final_vlan_list_build2[idx] = final_vlan_list_build2[idx].replace(
            final_vlan_list_build2[idx], final_vlan_list_build2[idx] + "   default 1       none qtagged")
    else:
        final_vlan_list_build2[idx] = final_vlan_list_build2[idx].replace(
            final_vlan_list_build2[idx], (final_vlan_list_build2[idx] + '       none qtagged'))
print(final_vlan_list_build2)

FYI the lengths for each index definitely show some over 60:

10
<class 'int'>
10
<class 'int'>
32
<class 'int'>
32
<class 'int'>
50
<class 'int'>
30
<class 'int'>
32
<class 'int'>
76
<class 'int'>
54
<class 'int'>
33
<class 'int'>
55
<class 'int'>
77
<class 'int'>
31
<class 'int'>
33
<class 'int'>
33
<class 'int'>
31
<class 'int'>
71
<class 'int'>
31

Partial input list:
['int g1/0/1', 'int g1/0/2', 'int g1/0/3  3999    default     ', 'int g1/0/4  3999    default     ', 'int g1/0/5   261    qtagged
 368    default   ', 'int g1/0/6   368    default   ', 'int g1/0/7  3999    default     ', 'int g1/0/8   258    qtagged        261
qtagged       3999    default     ', 'int g1/0/9   261    qtagged       3999    default     ', 'int g1/0/10   368    default     ', 'int g1/0/11   261    qtagged       3999    default     ', 'int g1/0/12   258    qtagged        261    qtagged       3999    default
 ', 'int g1/0/13   367    default   ', 'int g1/0/14  3999    default     ', 'int g
...

The partial output list:
['int g1/0/1   default 1       none qtagged', 'int g1/0/2   default 1       none qtagged', 'int g1/0/3  3999    default            none qtagged', 'int g1/0/4  3999    default            none qtagged', 'int g1/0/5   261    qtagged      368    default   ', 'int g1/0/6
 368    default          none qtagged', 'int g1/0/7  3999    default            none qtagged', 'int g1/0/8   258    qtagged        261    qtagged       3999    default        default 1       none qtagged', 'int g1/0/9   261    qtagged       3999    default     ', 'int g1/0/10   368    default            none qtagged', 'int g1/0/11   261    qtagged       3999    default     ', 'int g1/0/12   258    qtagged        261    qtagged       3999    default        default 1       none qtagged',
.....

Aucun commentaire:

Enregistrer un commentaire