how to parsing mutil host:port by python 3

扁豆焖面先生 发布于 2016/05/08 17:45
阅读 223
收藏 0

origin source:

"192.168.56.20:4001,192.168.56.21:4001,192.168.56.22:4001"

formate:

(('192.168.56.20',4001),('192.168.56.21',4001),('192.168.56.22',4001))
加载中
1
幻视Vision
幻视Vision

不用的正则表达:

s="192.168.56.20:4001,192.168.56.21:4001,192.168.56.22:4001";s.split(",");tuple([tuple(r.split(":")) for r in s.split(",")])





扁豆焖面先生
扁豆焖面先生
回复 @幻视Vision : 不过你这个还是有些不完美 我想把 端口号(4001)的单引号去调
扁豆焖面先生
扁豆焖面先生
回复 @幻视Vision : 哈哈 刚重新安装了一下操作系统 还没安装中文输入法。谢谢
幻视Vision
幻视Vision
回复 @扁豆焖面先生 : 哈哈,你说的毛我也看不懂。
扁豆焖面先生
扁豆焖面先生
wo ju ran si zai zui hou yi ge tuple fang fa shang. xiexie~
0
扁豆焖面先生
扁豆焖面先生
To sort dictionary, we could make use of operator module. Here is the operator module documentation.
import operator              #Importing operator module
dc =  {"aa": 3, "bb": 4, "cc": 2, "dd": 1}  #Dictionary to be sorted

dc_sort = sorted(dc.items(),key=operator.itemgetter(1),reverse=True)
print dc_sort

Output sequence will be a sorted list : 

[('bb', 4), ('aa', 3), ('cc', 2), ('dd', 1)]

If we want to sort with respect to keys, we can make use of 

dc_sort = sorted(dc.items(),key=operator.itemgetter(0),reverse=True)
Output sequence will be :
[('dd', 1), ('cc', 2), ('bb', 4), ('aa', 3)]
返回顶部
顶部