-2

this is not a duplicate question. I already researched the provided links and the suggestions did not help. I did open a new question that hopefully provided some clarification.

Let me clarify my questions: My ultimate goal is to remove the duplicate IP addresses from the list. I primarily need help with the following issue: I keep running into issues with maintaining the integrity of the IP addresses whenever I try to apply code (itertools, 'for' loops, etc) that removes duplicates from the output.

In summary:

1.My output is a tuple containing the matching strings (ip addresses)

2.I convert the tuple to a list of lists - print(hop_list)

3.I index [0] on the list of lists to obtain the IP addresses I need - print(ips) Is this where the problem lies? the output doesn't look like a list of list?

4.I apply various code solutions to remove duplicate addresses but for some reason I lose the integrity of my IP addresses.

per exscript api docs: • anymatch: If a match is found and the regular expression has multiple groups, a tuple containing the matching strings from the groups is returned.

def ios_commands(job, host, conn):        #function to execute IOS commands
conn.execute('terminal length 0')
conn.execute('tr {}'.format(DesAddr))
print('The results of the traceroute', repr(conn.response))
for hops in any_match(conn, r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})"):
    hop_list = list(hops)  #create a list from tuple
    ips = hop_list[0]   #index on targeted addresses
    print(list(uniqify(ips)))  #filter out duplicates

print(hop_list) #so far so good, list of lists from tuple

['192.33.12.4', '192', '.4', '.', '4']

['192.33.12.4', '192', '.4', '.', '4']

['192.32.0.174', '192', '.174', '.', '174']

['192.32.0.190', '192', '.190', '.', '190']

['192.33.226.225', '192', '.225', '.', '225']

['192.33.226.237', '192', '.237', '.', '237']

['192.33.12.4', '192', '.4', '.', '4']

Print(ips) #index first value as that is the address I need

I was expecting something like: ['192.33.12.4', '192.33.12.4', etc]

192.33.12.4

192.33.12.4

192.32.0.174

192.32.0.190

192.33.226.225

192.33.226.237

192.33.12.4

print(list(uniqify(ips)))

This is an edited sample output and where I run into a problem with the address format

['1', '9', '.', '3', '2', '4']

['1', '9', '.', '3', '2', '4']

['1', '9', '.', '3', '2', '7', '4']

['1', '9', '.', '3', '2', '9']

['1', '9', '.', '3', '2', '6', '5']

['1', '9', '.', '3', '2', '6', '7']

['1', '9 ', '.', '3', '2', '4']

Community
  • 1
  • 1
J.Chan
  • 77
  • 2
  • 8

2 Answers2

2
set(yourList)

Will remove any duplicate values

Stuart Clark
  • 611
  • 8
  • 13
0

You could always use ips_as_set = set(ips) to remove duplicate IPs, but that's a batch operation and throws away the order (which might be okay).

If you want to iterate over the IPs in order and remove duplicates on-the-fly, you can do this:

from __future__ import print_function


def uniqify(iterable):
    seen = set()
    for item in iterable:
        if item not in seen:
            seen.add(item)
            yield item

for unique_item in uniqify([1, 2, 3, 4, 3, 2, 4, 5, 6, 7, 6, 8, 8]):
    print(unique_item, end=' ')

print()

Output:

1 2 3 4 5 6 7 8
Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • Thanks for the responses so far. Unfortunately, for both responses: set(yourlist) and def uniqify(iterable) I get results that do not retain the IP address format but breaks it down to individual values. FYI, I do need to retain the order. thanks – J.Chan Aug 25 '15 at 23:50
  • I posted [a version of this answer](http://stackoverflow.com/a/32215911/892383) on [the duplicate](http://stackoverflow.com/q/7961363/892383), since none of the answers there were using a generator. – Cyphase Aug 25 '15 at 23:53
  • @J.Chan, what do you mean it breaks it down to individual values? – Cyphase Aug 25 '15 at 23:54
  • results from uniqify(iterable)1 0 . 3 2 4 1 0 . 3 2 4 1 0 . 3 2 7 4 1 0 . 3 2 9 1 0 . 3 2 6 5 1 0 . 3 2 6 7 1 0 . 3 2 4 set(yourlist) results:set(['.', '1', '0', '3', '2', '4']) set(['.', '1', '0', '3', '2', '4']) set(['.', '1', '0', '3', '2', '4', '7']) set(['.', '1', '0', '3', '2', '9']) set(['.', '1', '0', '3', '2', '5', '6']) set(['.', '1', '0', '3', '2', '7', '6']) set(['.', '1', '0', '3', '2', '4']) – J.Chan Aug 26 '15 at 00:06
  • @J.Chan, what is `iterable`? – Cyphase Aug 26 '15 at 00:07
  • referring to my example code, the iterable would be hop_addresses basically I need to keep the format of my IP addresses intact and in order (referring to output hop_addresses = hops[0]), just removing the duplicates – J.Chan Aug 26 '15 at 00:10
  • @J.Chan, if `hop_addresses` is as shown, in either case, you wouldn't get the output you mentioned. – Cyphase Aug 26 '15 at 00:15
  • `ips = ['192.33.12.4', '192.33.12.4', '192.32.0.174', '192.32.0.190', '192.33.226.225', '192.33.226.237', '192.33.12.4']; print(list(uniqify(ips)))` **results in** `['192.33.12.4', '192.32.0.174', '192.32.0.190', '192.33.226.225', '192.33.226.237']`. – Cyphase Aug 26 '15 at 00:16
  • this is the results I got:hops[0] ips = hops[0] print(list(uniqify(ips))) ['1', '0', '.', '3', '2', '4'] ['1', '0', '.', '3', '2', '4'] ['1', '0', '.', '3', '2', '7', '4'] ['1', '0', '.', '3', '2', '9'] ['1', '0', '.', '3', '2', '6', '5'] ['1', '0', '.', '3', '2', '6', '7'] ['1', '0', '.', '3', '2', '4'] maybe my existing hops[0] output is off? I'm not sure if it is a list or not. – J.Chan Aug 26 '15 at 00:36
  • @J.Chan, yes, that's definitely the issue, but I'm not sure why the items in `hops[0]` are of the form `['1', '0', '.', '3', '2', '4']`. That shouldn't even work; `set`'s can't hold lists. You're missing something :). – Cyphase Aug 26 '15 at 00:38