I want to count unique values in arrays and tuples

Thing you want to do

['a', 'a', 'b', 'b', 'b'],[('a', 2), ('b', 3)]I want to I want to take only two or more from there. SQL seems easy. Is it something that can't be done with Python alone?

manner

Made with

[(g[0], len(list(g[1]))) for g in itertools.groupby(array.sort())]

Example

>>> import itertools
>>> array = ['a', 'a', 'b', 'b', 'b']
>>> uniq = [(g[0], len(list(g[1]))) for g in itertools.groupby(array.sort())]
>>> uniq
[('a', 2), ('b', 3)]

What I wanted to do

>>> tuples = [('aaa', 'bbb'), ('aaa', 'bbb'), ('aaa', 'vvv'), ('bbb', 'ccc'), ('bbb', 'ccc')]
>>> uniq = [(g[0], len(list(g[1]))) for g in itertools.groupby(tuples.sort())]
>>> uniq
[(('aaa', 'bbb'), 2), (('aaa', 'vvv'), 1), (('bbb', 'ccc'), 2)]
>>>  filter(lambda f: f[1] > 1, uniq)
[(('aaa', 'bbb'), 2), (('bbb', 'ccc'), 2)]

Only two or more were pulled out.

reference

http://stackoverflow.com/questions/2392929/how-to-get-unique-values-with-respective-occurance-count-from-a-list-in-python


I got a comment about a more useful library.

There was a more convenient collections

>>> from collections import Counter
>>> tuples = [('aaa', 'bbb'), ('aaa', 'bbb'), ('aaa', 'vvv'), ('bbb', 'ccc'), ('bbb', 'ccc')]
>>> uniq = Counter(tuples)
>>> uniq 
Counter({('aaa', 'bbb'): 2, ('bbb', 'ccc'): 2, ('aaa', 'vvv'): 1})
>>> filter(lambda f: f[1] > 1, uniq.items())
[(('aaa', 'bbb'), 2), (('bbb', 'ccc'), 2)]

collections.Let's use Counter!!



# reference
http://docs.python.jp/2/library/collections.html



Recommended Posts

I want to count unique values in arrays and tuples
I want to make the Dictionary type in the List unique
I want to create a pipfile and reflect it in docker
I want to print in a comprehension
I want to embed Matplotlib in PySimpleGUI
I want to do Dunnett's test in Python
I want to pin Datetime.now in Django tests
I want to store DB information in list
I want to merge nested dicts in Python
I want to display the progress in Python!
I want to visualize where and how many people are in the factory
I want to get the file name, line number, and function name in Python 3.4
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to handle optimization with python and cplex
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to replace the variables in the python template file and mass-produce it in another file.
I want to write a triple loop and conditional branch in one line in python
Script to count and stop up to 5 seconds in Python in Blender
I want to get rid of import warnings from Pyright and pylint in VS Code
I want to solve Sudoku (Sudoku)
I tried to illustrate the time and time in C language
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
I want to copy an English paper from pdf and put it in Google Translate
How to get all the keys and values in the dictionary
[Django] I want to log in automatically after new registration
I want to know the features of Python and pip
[Introduction to Pytorch] I want to generate sentences in news articles
I want to map the EDINET code and securities number
I want to align the significant figures in the Numpy array
I want Sphinx to be convenient and used by everyone
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I want to use VS Code and Spyder without anaconda! !! !!
I didn't want to write the AWS key in the program
I want to record the execution time and keep a log.
[Linux] I want to know the date when the user logged in
I want to run Rails with rails s even in vagrant environment
LINEbot development, I want to check the operation in the local environment
I want to use both key and value of Python iterator
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I want to acquire and list Japanese stock data without scraping
Scraping and tabelog ~ I want to find a good restaurant! ~ (Work)
I want to make the second line the column name in pandas
I want to connect remotely to another computer, and the nautilus command
I want to pass the G test in one month Day 1
I was addicted to confusing class variables and instance variables in Python
I want to know the population of each country in the world.
I want to hack Robomaster S1 ① Rooting and file configuration check
I want to understand systemd roughly
I want to scrape images to learn