In addition, use collections.Counter to refactor your code:
from collections import Counter
words = ['a', 'b', 'c', 'a']
Counter(words).keys() # equals to list(set(words))
Counter(words).values() # counts the elements' frequency
Output:
['a', 'c', 'b']
[2, 1, 1]
More Related Contents:
- Find unique rows in numpy.array
- How do you create different variable names while in a loop? [duplicate]
- Assigning values to variables in a list using a loop
- How to increment variable names/Is this a bad idea [duplicate]
- Defined variable is not defined
- How to do for loop through all items of a list except the first [duplicate]
- What is the difference between range and xrange functions in Python 2.X?
- Simpler way to create dictionary of separate variables?
- Sorting arrays in NumPy by column
- Elegant way to check if a nested key exists in a dict?
- What does a for loop within a list do in Python?
- Selecting specific rows and columns from NumPy array
- How to sort and remove duplicates from Python list? [duplicate]
- Changing variable names with Python for loops [duplicate]
- Immutable numpy array?
- Django template can’t loop defaultdict
- How to get a list of all indices of repeated elements in a numpy array
- Binary random array with a specific proportion of ones?
- Access to value of variable with dynamic name
- Compress numpy arrays efficiently
- Index 2D numpy array by a 2D array of indices without loops
- Flattening a list of NumPy arrays?
- Element-wise string concatenation in numpy
- Fastest pairwise distance metric in python
- Should I use instance or class attributes if there will only be one instance? [closed]
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Is it possible to make a `for` loop without an iterator variable? (How can I make make code loop a set number of times?)
- Partition array into N chunks with Numpy
- Fast string array – Cython
- Optimal method to find the max of sublist items within list