Python 2.7, Using Counter On Open('file.txt')
I got a good answer before for a similar problem but I didnt consider the scalability of the question that I asked. I had trouble with the text editor I am using concerning pasting in large amounts of text and ending up with either newlines, or if I remove all the newlines from the document then the text goes off the screen and wont allow me to scroll to the end. So I saw how to use open on the text as a file but now the code wont work properly.
Here is the code:
import sys
import os
from collections import Counter
def main():
with open('garbledText.txt') as text:
print [k for k,v in Counter(text).items() if v<3]
if __name__=='__main__':
main()
it seemed like it was going in the right direction because if I change 'v<3' to 'v<1' I get an empty list, but with 'v<3' I get all the chars.
what I am trying to do is parse the 'garbledText.txt' to find chars that appear 1-2 times.
Answer
replace text
for text.read(),
the first make a collection of lines and the second of caracters.
from collections import Counter
def main():
with open('garbledText.txt') as text:
collection = Counter(text.read())
print [char for char, times in collection.items() if times < 3]
if __name__=='__main__':
main()
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module