Skip to content

Instantly share code, notes, and snippets.

@wesleyterry
Created December 19, 2013 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleyterry/8045475 to your computer and use it in GitHub Desktop.
Save wesleyterry/8045475 to your computer and use it in GitHub Desktop.
Basically I had a large number of email addresses in a .txt file and needed find matches for a partial string match (@gmail.com for example) and put those into a new list to work with. This is the first time I've been able to use python for something really useful after I started to learn the language. Hopefully someone else can find a use for i…
# open a .txt file and turn it into a list with variable name mylist
# that can be easily worked with in python
f = open('list1.txt','r')
mylist = list(f)
# saving that list into a new variable without line breaks
updatedlist = [l.replace('\n','') for l in mylist]
# global variable list to catch all the appended lookups
newlist = []
# function to look up partial string matches in each list item
# and add to the newlist global variable
def listlookup(name):
for i, elem in enumerate(updatedlist):
if name in elem:
newlist.append(i)
# run the below for loop in the python shell to output all the items that
# matched the listlookup function
# for item in newlist:
# updatedlist[item]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment