snmpwalk :获取整数列表而不是字符串

snmpwalk : get list of integers not string

在我的 python 脚本中,我获取了 5 个 OID,其中 return 每个都是一个整数 这是我的代码:

OIDs = []
res = []

OIs.append(netsnmp.VarList(netsnmp.Varbind('1.3.6.1.4.1.45.1.1.4.1')))
OIs.append(netsnmp.VarList(netsnmp.Varbind('1.3.6.1.4.1.45.1.1.4.2')))
OIs.append(netsnmp.VarList(netsnmp.Varbind('1.3.6.1.4.1.45.1.1.4.3')))
OIs.append(netsnmp.VarList(netsnmp.Varbind('1.3.6.1.4.1.45.1.1.4.4')))
OIs.append(netsnmp.VarList(netsnmp.Varbind('1.3.6.1.4.1.45.1.1.3.1')))

for i in range(len(OIDs)):
    res.append(netsnmp.snmpwlak(OIDs[i], Version = 2, DestHost='192.168.0.1', Community='public'))
return res

print res return 我那个列表 :

[('18',), ('13',), ('1',), ('2',), ('1',)]

OID 编辑的所有值 return 都是整数

问题:

我怎样才能得到一个只有整数的列表,就像这样:

[18, 13, 1, 2, 1]

有人知道如何解决我的问题吗?

使用map

a = [('18',), ('13',), ('1',), ('2',), ('1',)]

print(map(lambda x: int(x[0]), a))

输出:

[18, 13, 1, 2, 1]
  • 编辑*

虽然这行得通,但列表理解的性能更高。查看回答

res = [('18',), ('13',), ('1',), ('2',), ('1',)]
res = [int(x[0]) for x in res] ## [18, 13, 1, 2, 1]

您可以使用列表理解:

[int(x[0]) for x in res]