Best way to Sort Python Dictionary by number of same Values - python-3.x

I have a dictionary as follows:
input = {1:'a', 2:'b', 3:'c', 4:'b', 5:'c', 6:'b'}
a - 1 time
b - 3 times
c - 2 times
I want to sort the dictionary in such a way that the value which repeats maximum times will come at fist followed by the value which repeats second most times and so on...
Desired Output
output = {1:'b', 2:'c', 3:'a'}
Please help
Thanks in advance

First of all, you need to realize you do not even care about input dict keys, only about input dict values.
real_input = input.values()
Second of all, you need to count occurences:
counted_items = collections.Counter(real_input)
Third of all, you want to iterate over them in order from most common to least common. .most_common() returns list of (key, count) tuples in expected order.
most_common_in_order = counted_items.most_common()
After that you want to convert that list to dict, with consecutive inegers as keys
result = {i: v for i, (v, _) in zip(itertools.count(1), most_common_in_order)}
Or, in concise form:
result = {i: v for i, (v, _) in zip(itertools.count(1),
collections.Counter(input.values()).most_common())}
Note that dictionaries are inherently not ordered, so actual order is implementation detail, and it's not guaranteed.

from collections import Counter
input = {1: 'a', 2: 'b', 3: 'c', 4: 'b', 5: 'c', 6: 'b'}
output = {index: value for index, (value, _)
in enumerate(Counter(input.values()).most_common(),
start=1)}
print(output)

Edited for Python 3 compatibility.
from collections import Counter
i = {1:'a', 2:'b', 3:'c', 4:'b', 5:'c', 6:'b'}
print (Counter(i.values()))
#Output
Counter({'b': 3, 'c': 2, 'a': 1})

Related

Is there a simpler way to extract the last value of a dictionary?

So I was tasked to make a function using python, that returns how many values there is in a dictionary that ONLY contains lists. An example of such a dictionary would be:
animals = { 'a': ['alpaca','ardvark'], 'b': ['baboon'], 'c': ['coati']}
The values inside the list also count towards the total values returned from the function, which means that it has to return 4. This is the function I made:
def how_many(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: int, how many values are in the dictionary.
'''
numValues = 0;
while aDict != {}:
tupKeyValue = aDict.popitem();
List = tupKeyValue[1];
numValues += len(List);
return numValues;
So I was wondering if there was a way to pop the last value of a dictionary without popitem() which extracts the key-value pair. Just trying to make it as simple as possible.
Since you are not using the dictionaries keys maybe you could just use values() along with sum():
def how_many(d):
return sum(len(v) for v in d.values())
animals = {'a': ['alpaca', 'ardvark'], 'b': ['baboon'], 'c': ['coati']}
print(how_many(animals))
Output:
4

Creating a dictionary from list of lists using only one for loop

I came across this question in a test. There are two parts to this question:
Part i:
Given a list of flavors, eg. ['A','A','A','A','B','B','B','B','B','C','C','C','C'], write a function that returns a dictionary of the number of each flavor respectively.
My solution:
flavors = ['A','A','A','A','B','B','B','B','B','C','C','C','C']
def count_flavors(l):
dict_flavors={}
for i in l:
dict_flavors[i] = l.count(i)
return dict_flavors
print(count_flavors(flavors))
Part ii:
Using not more than ONE for loop write a function that accepts a list of lists of flavors eg. [['A', 'A', 'B', 'B', 'B', 'C', 'C'], ['A', 'A', 'B', 'B', 'B', 'B', 'C'], ['A', 'B', 'C', 'C']] and returns a dictionary for the total number of each flavor. You must include the function that you defined in part one in this solution.
(To clarify, essentially there should only be two for loops; one from part one and one from part two)
So far my solution is the following:
batches = [['A','A','A','A','B','B','B','B','B','C','C','C','C'], ['A', 'A', 'B', 'B' ,'B','B','C'], ['A','B','C','C']]
def batch_count(b):
batch_dict = []
result = {}
for j in b:
batch_dict.append(count_flavors(j))
print(batch_dict)
for i in batch_dict:
for k in i.keys():
result[k] = result.get(k,0) + i[k]
return result
print('batch count 1:' + str(batch_count(batches)))
I am struggling to find a solution that only uses one for loop for this part. I am aware that there are modules that exist for this sort of thing like collections.Counter(). Is a naive solution that does not include any modules possible for this problem?
Thanks!
Here is the best naive solution which I can think of in order to achieve what you want
Benefits of using the solution
No need to create extra variable like batch_dict = [], which takes unnecessary space in your system
No need to carry out multiple computations using different methods, like you did above using count_flavors()
Straight forward and easy to understand
FINAL SOLUTION
batches = [['A','A','A','A','B','B','B','B','B','C','C','C','C'], ['A', 'A', 'B', 'B' ,'B','B','C'], ['A','B','C','C']]
def batch_count(b):
result = {} # for storing final count results
# two loops are required to get into the arrays of array, not other option is there
for items in b:
# Getting the nested array item here
for item in items:
# final computation, if the item is there in the result dict, then increment
# else simply assign 1 to the item as a key which eventually gives you the total number
# of counts of each item throughout the batches array items
if item in result:
result[item] += 1
else:
result[item] = 1
return result
print('batch count 1:' + str(batch_count(batches)))
# OUTPUT
# >>> batch count 1:{'A': 7, 'C': 7, 'B': 10}
Feel free to test this out for some other batches too, and let me know. This is by far the naive solution which is possible to give out what you want to achieve. Keep learning :)
ANOTHER SOLUTION [MAKING USE OF FIRST METHOD COUNT_FLAVORS]
Hey, if you really want to use the first method, then there is a work around, but you need to compromise with one thing now, that is Counter has to be imported, but I assure you, it will be as simple as that, and will give you straight forward answer
Your count_flavors works fine, so we take the count_falvors() as is.
We will be making changes to the batch_count method now
FINAL SOLUTION
from collections import Counter
# Taking your method as is, to get the dictionary which counts
# the items occurence from your array
def count_flavors(l):
dict_flavors={}
for i in l:
dict_flavors[i] = l.count(i)
return dict_flavors
# This method will do your stuffs
def batch_count(b):
result = {} #this will be used to return the final result
# now just one loop, since we will passing the array
# to our method for computation count_flavors()
for items in b: # this will give out single array
'''
now we will call your count_flavor method
we will use Counter() to merge the dictionary data
coming from the count_flavor and then add it to the result
Counter() keep track of same item, if present in multiple
dict, ADDS +1 to the same item, doesn't duplicate value
Hence counter required
'''
if len(result) != 0:
# if the result is not empty, then result = result + data
result += Counter(count_flavors(items)) # no more extra for loop
else:
# else first fill the data by assigning it
result = Counter(count_flavors(items))
# this will give out the output in {}
# else the output will come in Counter({}) format
return dict(result)
# our test array of arrays
batches = [['A','A','A','A','B','B','B','B','B','C','C','C','C'], ['A', 'A', 'B', 'B' ,'B','B','C'], ['A','B','C','C']]
print('batch count 1:' + str(batch_count(batches)))
# OUTPUT
# >>> batch count 1:{'A': 7, 'B': 10, 'C': 7}
In this way you achieve the output with the usage of your count_flavors() method too, that too with no multiple loops in the batch_count(). Hope that will give you more clarity :). If this works out for you, you may accept the answer, for the the people who will come looking for answer to this question :)
The first function can become much faster by modifying your approach in this way:
def count_flavors(lst):
dict_flavors = {}
for item in lst:
if item in dict_flavors:
dict_flavors[item] += 1
else:
dict_flavors[item] = 1
return dict_flavors
You could also use Counter to simplify your code:
from collections import Counter
def count_flavors(lst):
return dict(Counter(lst))
The second function can use itertools.chain:
from collections import Counter
from itertools import chain
def batch_count(b):
return dict(Counter(chain(*b)))

Initialising Python dictionary with list comprehension

I coded up this code randomly trying to learn comprehension and now I am unable to understand what is happening in the code.
lst = "this is really strange behaviour".split()
dt = dict({i,j} for i,j in enumerate(lst))
print(dt)
I tried to make a dictionary with keys as integers and values as corresponding words. But the output of this code is
{0: 'this', 1: 'is', 2: 'really', 'strange': 3, 4: 'behaviour'}
I don't understand why some keys are numbers while others are strings.
P.S. I don't even understand why this syntax is correct and gives no errors.
Please explain about it. Sorry for my bad English.
{i,j} creates a set. Sets have no inherit order, so the set is sometimes i, j and sometimes j, i. dict always use the "first" (left-most) value as the key and the "second" as the value. This means that sometimes i is picked to be the key and sometimes j is.
Use an ordered data-structure instead, for example a tuple:
dt = dict((i, j) for i, j in enumerate(lst))
# ^ ^ note parenthesis to create tuple, not braces
then dt will always be
{0: 'this', 1: 'is', 2: 'really', 3: 'strange', 4: 'behaviour'}
You can actually skip (i, j) for i, j in enumerate(lst) which does nothing useful really, and go straight with
dt = dict(enumerate(lst))
Using a set here does not make any sense. Since elements of a set are unique, this code will break if the list contains a number which happens to equal its index, for example:
lst = ['a', 1]
dt = dict({i, j} for i, j in enumerate(lst))
Will cause an error
ValueError: dictionary update sequence element 1 has length 1; 2 is required
because {1, 1} becomes {1} and dict expects a sequence of key-value pairs.

How to Sort Alphabets

Input : abcdABCD
Output : AaBbCcDd
ms=[]
n = input()
for i in n:
ms.append(i)
ms.sort()
print(ms)
It gives me ABCDabcd.
How to sort this in python?
Without having to import anything, you could probably do something like this:
arr = "abcdeABCDE"
temp = sorted(arr, key = lambda i: (i.lower(), i))
result = "".join(temp)
print(result) # AaBbCcDdEe
The key will take in each element of arr and sort it first by lower-casing it, then if it ties, it will sort it based on its original value. It will group all similar letters together (A with a, B with b) and then put the capital first.
Use a sorting key:
ms = "abcdABCD"
sorted_ms = sorted(ms, key=lambda letter:(letter.upper(), letter.islower()))
# sorted_ms = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']
sorted_str = ''.join(sorted_ms)
# sorted_str = 'AaBbCcDd'
Why this works:
You can specify the criteria by which to sort by using the key argument in the sorted function, or the list.sort() method - this expects a function or lambda that takes the element in question, and outputs a new criteria by which to sort it. If that "new criteria" is a tuple, then the first element takes precedence - if it's equal, then the second argument, and so on.
So, the lambda I provided here returns a 2-tuple:
(letter.upper(), letter.islower())
letter.upper() as the first element here means that the strings are going to be sorted lexigraphically, but case-insensitively (as it will sort them as if they were all uppercase). Then, I use letter.islower() as the second argument, which is True if the letter was lowercase and False otherwise. When sorting, False comes before True - which means that if you give a capital letter and a lowercase letter, the capital letter will come first.
Try this:
>>>s='abcdABCD'
>>>''.join(sorted(s,key=lambda x:x.lower()))
'aAbBcCdD'

the code that i've wrote works fine with a list of four elements but when the length of the list increases it is not giving the right answer

I'm trying to write a python code for a problem wherein I will be given with a list of string characters for example ["A", "B", "B", "C"] and the output that I should get is B and if there are more than one repeated value with equal number of repetitions or no elements in the list it should give"NONE" AS output. and my code is doing good so far but when the size of the list is increasing my code is giving wrong output please help me in optimizing the code so that it takes a list of any size and gives a correct output
lis = ["A","B","B","A"] #INPUT LIST
catch = []
final_catch=[]
for i in range(len(lis)):
for j in range(i + 1, len(lis)):
if lis[i] == lis[j]:
catch.append(lis[i])
final_catch =list(set(catch))
print(final_catch)
if len(final_catch)>=2 or len(final_catch) == 0:
print("NONE")
else:
print(final_catch.pop())
for input ["A,"B","B","A"] expected output:"NONE" actual output: "NONE"
for input ["A","A","A","A"] expected output :"A" actual output : "A"
for input ["A","A","B","B","A","B","A","B","B"] expected output : "B"
Try this,
>>> from collections import Counter
>>> l = ["A","A","B","B","A","B","A","B","B"]
>>> d = Counter(l)
>>> result = d.most_common() # result looks like [('B', 5), ('A', 4)]
Output:
>>> result[0][0] if result[0][1] >2 else 'None' # conditional if-else statement
'B'
Explanation:
Use Counter to get number of occurrences of each element in a list
Use .most_common() to get list of most occurrence in the form of tuplesi.e., [(element, no. of occurrence)]
Return a list of the n most common elements and their counts from the
most common to the least
result[0][0] - we are passing index values to get first element of tuple in list.
result[0][1] - This gives you second element of tuple in a list.
*result[0] - select first element in a list

Resources