Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Array filtering not working?

Hi Python masters 🤝

I am having a weird issue, I have this:
Code
    file = open(proxyList ,'r')
    proxies = file.read()
    file.close()
    proxies = [s.strip() for s in proxies.splitlines()]

    file = open(notWorkingProxyList ,'r')
    notWorkingProxies = file.read()
    file.close()
    notWorkingProxies = [s.strip() for s in notWorkingProxies.splitlines()]

    print proxies
    print notWorkingProxies

    for x in proxies:
        if x in notWorkingProxies:
            proxies.remove(x)
        else:
            print 'Proxy [' + x + '] is ok to use.'

    print proxies

The output from that is the following:
Code
['187.32.4.66:8080', '202.162.217.3:8080']
['187.32.4.66:8080', '202.162.217.3:8080']
['202.162.217.3:8080']

I don't really get why it outputs the IP at the end? Shouldn't it be an empty array?
I expect it to be an empty array, any idea what I am doing wrong?
[Image: 6KMyuYs.png]
you got an issue, because you edit the list you iterate on....


try this:
Code
file = open(proxyList ,'r')
proxies = file.read()
file.close()
proxies = [s.strip() for s in proxies.splitlines()]

file = open(notWorkingProxyList ,'r')
notWorkingProxies = file.read()
file.close()
notWorkingProxies = [s.strip() for s in notWorkingProxies.splitlines()]

print proxies
print notWorkingProxies


for x in [p for p in proxies]:
    if x in notWorkingProxies:
        proxies.remove(x)

print proxies
(Oct 17, 2018 04:09 AM).kr. Wrote:  you got an issue, because you edit the list you iterate on....


try this:
Code
truncated

Thanks, that solved my issue. I will remember to do this in the future.
Really appreciate the help.
[Image: 6KMyuYs.png]
this is the same with a little less code:
Code
proxyList = "proxy.txt"
notWorkingList = "nw.txt"

def toList(file):
    with open(file) as f:
        return [x.strip() for x in f.read().strip().split()]

proxies = toList(proxyList)
notWorking = toList(notWorkingList)

for x in [p for p in proxies if p not in notWorking]:
    print x
(Oct 17, 2018 04:17 AM).kr. Wrote:  this is the same with a little less code:
Code
proxyList = "proxy.txt"
notWorkingList = "nw.txt"

def toList(file):
   with open(file) as f:
       return [x.strip() for x in f.read().strip().split()]

proxies = toList(proxyList)
notWorking = toList(notWorkingList)

for x in [p for p in proxies if p not in notWorking]:
   print x

Thats much better, its easier to understand when I can name the functions myself 👍
[Image: 6KMyuYs.png]
Code
with open("proxy.txt") as working, open("nw.txt") as not_working:
  working = set(map(str.strip, working))
  not_working = set(map(str.strip, not_working))

for proxy in (working - not_working):
  print(proxy)

should work, haven't tested it.
does a simple set subtraction on working from not_working.
Contact for professional programming services.
unazed#5906


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Jake JohnsonOct 28, 2020 10:15 AM 1 192 Oct 28, 2020 10:15 AM
Last Post: itcorp
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 260 May 19, 2020 05:18 PM
Last Post: k1r3n
[C#] by eXeroApr 29, 2018 08:59 PM 1 129 Apr 29, 2018 08:59 PM
Last Post: Vyxn