Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python list iter issue
#1
from easysnmp import snmp_get, snmp_set, snmp_walk
from collections import Counter
import sys
#start = time.time()
host = '172.19.2.130'
interface = []
interfaceoid = '1.3.6.1.2.1.2.2.1.1'
indexwalk = snmp_walk(interfaceoid, hostname=host, community='sky123', version=2)
interfaces = [item.value for item in indexwalk]
interface.append(interfaces)

for x in iter(interface):
    b = interfaceoid+'.'+ ''.join(x)
    print (b)
    
Output:
1.3.6.1.2.1.2.2.1.1.285278721285278722285278723285278724285278725285278726285278727285278728285278729285278730285278731285278732285278733285278734285278735285278736285278977285278978285278979285278980285278981285278982285278983285278984285278985285278986285278987285278988285278989285278990285278991285278992285280769285280770285280771285280772285280773285281025285281026285281027285281028285281029285282817553648129654311425
expected output is

Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724
Reply
#2
Print x for each pass through the for so you know what is happening. There is no input in the post and none of us are psychic so we can't run the program to see what is happening.
Reply
#3
(Apr-05-2018, 06:18 PM)anna Wrote:
interfaces = [item.value for item in indexwalk]
interface.append(interfaces)
 
for x in iter(interface):
You create a list of items you call interfaces, and then append it to an empty list called interface. So, interface is a list with only one element, and that element happens to be a list of a lot of things. Then you enter a for loop, looping over the list that only has one element, so x is a list of all your interfaces.

Output makes sense, given what you're iterating over.

You could have easily determined this yourself by using a non-empty string for your joining. ie: '!!'.join(x)
Reply
#4
Output:
[u'285278721', u'285278722', u'285278723', u'285278724', u'285278725', u'285278726', u'285278727', u'285278728', u'285278729', u'285278730', u'285278731', u'285278732', u'285278733', u'285278734', u'285278735', u'285278736', u'285278977', u'285278978', u'285278979', u'285278980', u'285278981', u'285278982', u'285278983', u'285278984', u'285278985', u'285278986', u'285278987', u'285278988', u'285278989', u'285278990', u'285278991', u'285278992', u'285280769', u'285280770', u'285280771', u'285280772', u'285280773', u'285281025', u'285281026', u'285281027', u'285281028', u'285281029', u'285282817', u'553648129', u'654311425']
this is list of interfaces, need to append to oid.

b = interfaceoid+'.'+ ''.join(x)
current output

Output:
1.3.6.1.2.1.2.2.1.1.285278721285278722285278723285278724285278725285278726285278727285278728285278729285278730285278731285278732285278733285278734285278735285278736285278977285278978285278979285278980285278981285278982285278983285278984285278985285278986285278987285278988285278989285278990285278991285278992285280769285280770285280771285280772285280773285281025285281026285281027285281028285281029285282817553648129654311425
its appending to oid single time only, where am i going wrong?

expected output is

Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724
Reply
#5
interface = [u'285278721', u'285278722', u'285278723', u'285278724', u'285278725']
interfaceoid = '1.3.6.1.2.1.2.2.1.1'
for value in interface:
    print('{}.{}'.format(interfaceoid, value))
Output:
1.3.6.1.2.1.2.2.1.1.285278721 1.3.6.1.2.1.2.2.1.1.285278722 1.3.6.1.2.1.2.2.1.1.285278723 1.3.6.1.2.1.2.2.1.1.285278724 1.3.6.1.2.1.2.2.1.1.285278725
Reply
#6
(Apr-06-2018, 01:45 AM)anna Wrote: its appending to oid single time only, where am i going wrong?
I get from your post that you didn't read my response. So I'll try again, by showing you your code, with a few things reorganized to be more obvious:
things = [item.value for item in indexwalk]
interface = [things]

for x in interface:
You're iterating over a list that only has one element. If you iterate over all the interfaces instead, it'll work as you intend.
Reply
#7
Wavic,Buran,nimalo,

Sorry, was out of station.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 542 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Python List Issue Aggie64 5 1,620 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  List to table issue robdineen 2 1,464 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,285 Jun-11-2021, 10:13 PM
Last Post: topfox
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,222 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  type of result of iter() Skaperen 12 4,404 Feb-07-2020, 07:23 PM
Last Post: Skaperen
  For List Loop Issue Galdain 2 2,052 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,539 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,494 Sep-20-2019, 11:57 AM
Last Post: perfringo
  List Issue Batman 3 2,653 Jun-06-2019, 11:56 PM
Last Post: Batman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020