Created
July 4, 2018 15:48
-
-
Save n1ckfg/ca24117965fc131303549f8d3ca53d1f to your computer and use it in GitHub Desktop.
Intro to manipulating text in Python. Find people who don't follow you back on Instagram.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def cleanList(text): | |
returns = [] | |
patterns= ['[A-Z]+'] | |
for line in text: | |
line = line.strip() | |
if " " not in line: | |
match = None | |
for p in patterns: | |
match = re.findall(p, line) | |
if (len(match) == 0): | |
returns.append(line) | |
return returns | |
file = open("followers.txt", "r") | |
followers = file.readlines() | |
file.close() | |
file = open("following.txt", "r") | |
following = file.readlines() | |
file.close() | |
followers_cleaned = cleanList(followers) | |
following_cleaned = cleanList(following) | |
non_mutuals = [] | |
mutuals = [] | |
for line in following_cleaned: | |
followsYou = False | |
for line2 in followers_cleaned: | |
if line == line2: | |
followsYou = True | |
if (followsYou == False): | |
non_mutuals.append(line) | |
else: | |
mutuals.append(line) | |
if len(following_cleaned) == len(mutuals) + len(non_mutuals): | |
finalCheck = True | |
checkErrorCount = 0 | |
for line in non_mutuals: | |
for line2 in followers: | |
if line == line2: | |
finalCheck = False | |
checkErrorCount += 1 | |
if (finalCheck == True): | |
file = open("non_mutuals.txt", "w") | |
for line in non_mutuals: | |
print(line) | |
file.write(line + "\r\n") | |
file.close() | |
print("Found " + str(len(followers_cleaned)) + " followers and " + str(len(following_cleaned)) + " following.") | |
print("You are following " + str(len(mutuals)) + " mutuals and " + str(len(non_mutuals)) + " non-mutuals.") | |
else: | |
print("Error: " + str(checkErrorCount) + " non-mutuals were found in followers.") | |
else: | |
print("Error: non-mutual and mutual counts don't add up.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment