Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: For statement to display a list with specific strings?

I'm new to Python and trying to learn to use a for statement to display information in a certain way.... Is there a way to use the for statement to display a list like this?

w = "Fa1/1                           connected    42         a-full  a-100 10/100BaseTX"
v = w.split()

x=v[0]
print "Port ", x 

y=v[1]
print "Status ", y 

z=v[2]
print "VLAN ", z 

a=v[3]
print "Duplex ", a 

b=v[4]
print "Speed ", b 

c=v[5]
print "Type ", c 

-------------------------
Port  Fa1/1
Status  connected
VLAN  42
Duplex  a-full
Speed  a-100
Type  10/100BaseTX

I have tried a lot of different methods but keep getting value and index errors....

Thanks for any help....

like image 665
Justin Parker Avatar asked Jan 18 '26 16:01

Justin Parker


1 Answers

Something like this?

>>> w = "Fa1/1                           connected    42         a-full  a-100 10/100BaseTX"
>>> firstList = ['Port', 'Status', 'VLAN', 'Duplex', 'Speed', 'Type']
>>> testList = zip(firstList, w.split())
>>> for a, b in testList:
        print a, b


Port Fa1/1
Status connected
VLAN 42
Duplex a-full
Speed a-100
Type 10/100BaseTX
like image 135
Sukrit Kalra Avatar answered Jan 20 '26 05:01

Sukrit Kalra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!