加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:用于显示包含特定字符串的列表的语句?

发布时间:2020-12-20 11:28:36 所属栏目:Python 来源:网络整理
导读:我是 Python的新手,并试图学习使用for语句以某种方式显示信息….有没有办法使用for语句来显示这样的列表? 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]pr
我是 Python的新手,并试图学习使用for语句以某种方式显示信息….有没有办法使用for语句来显示这样的列表?

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

我尝试了很多不同的方法,但不断获得价值和索引错误….

谢谢你的帮助….

解决方法

像这样的东西?

>>> 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读