Reputation: 2189
I know someone might think this question has been answered here but it doesn't have answer to what I want to achieve.
I have list of phone numbers, a very large one, and a whole lot of them starts with 08
and there is a lot of duplication, which is what I am trying to remove. Now I need to put them in a list
or set
so that I can use them in my program but it returns Invalid token as shown in the picture below:
Python assumes anything that starts with 0
as octal. How do I device a mean to bypass this and have these numbers in a list and then in a set?
Upvotes: 4
Views: 7596
Reputation: 4812
If you need to have them prepended by 08, use strings instead of ints.
a = ["08123","08234","08123"]
a = list(set(a)) # will now be ["08123","08234"]
Since (as you say) you don't have an easy way of surrounding the numerous numbers with quotes, go to http://www.regexr.com/ and enter the following:
Expression: ([0-9]+)
Text: Your numbers
Substitution (expandable pane at the bottom of the screen: "$&"
Upvotes: 2
Reputation: 31905
read your phone input file, save each phone as string
to a set
, then the duplicates will be removed due to set
only hold unique elements, and you can do further work on them.
def get_unique_phones_set():
phones_set = set()
with open("/path/to/your/duplicated_phone_file", "r") as inputs:
for phone in inputs:
# phone is read as a string
phones_set.add(phone.strip())
return phones_set
Upvotes: 2