Summing the contents of two collections.Counter() objects [duplicate]

All you need to do is add them: >>> from collections import Counter >>> a = Counter({‘menu’: 20, ‘good’: 15, ‘happy’: 10, ‘bar’: 5}) >>> b = Counter({‘menu’: 1, ‘good’: 1, ‘bar’: 3}) >>> a + b Counter({‘menu’: 21, ‘good’: 16, ‘happy’: 10, ‘bar’: 8}) From the docs: Several mathematical operations are provided for combining … Read more

How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

str.split() without any arguments splits on runs of whitespace characters: >>> s=”I am having a very nice day.” >>> >>> len(s.split()) 7 From the linked documentation: If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain … Read more

Transform a Counter object into a Pandas DataFrame

You can construct using from_dict and pass param orient=”index”, then call reset_index so you get a 2 column df: In [40]: from collections import Counter d = Counter({‘fb_view_listing’: 76, ‘fb_homescreen’: 63, ‘rt_view_listing’: 50, ‘rt_home_start_app’: 46, ‘fb_view_wishlist’: 39, ‘fb_view_product’: 37, ‘fb_search’: 29, ‘rt_view_product’: 23, ‘fb_view_cart’: 22, ‘rt_search’: 12, ‘rt_view_cart’: 12, ‘add_to_cart’: 2, ‘create_campaign’: 1, ‘fb_connect’: 1, … Read more

Count characters in textarea

What errors are you seeing in the browser? I can understand why your code doesn’t work if what you posted was incomplete, but without knowing that I can’t know for sure. You should probably clear the charNum div, or write something, if they are over the limit. function countChar(val) { var len = val.value.length; if … Read more

Get loop counter/index using for…of syntax in JavaScript

for…in iterates over property names, not values, and does so in an unspecified order (yes, even after ES6). You shouldn’t use it to iterate over arrays. For them, there’s ES5’s forEach method that passes both the value and the index to the function you give it: var myArray = [123, 15, 187, 32]; myArray.forEach(function (value, … Read more

Counter increment in Bash loop not working

First, you are not increasing the counter. Changing COUNTER=$((COUNTER)) into COUNTER=$((COUNTER + 1)) or COUNTER=$[COUNTER + 1] will increase it. Second, it’s trickier to back-propagate subshell variables to the callee as you surmise. Variables in a subshell are not available outside the subshell. These are variables local to the child process. One way to solve … Read more

Code for a simple JavaScript countdown timer?

var count=30; var counter=setInterval(timer, 1000); //1000 will run it every 1 second function timer() { count=count-1; if (count <= 0) { clearInterval(counter); //counter ended, do something here return; } //Do code for showing the number of seconds here } To make the code for the timer appear in a paragraph (or anywhere else on the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)