Counter in Python - list

Is there a way to collect values in Counter with respect to occurring number?
Example:
Let's say I have a list:
list = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd']
When I do the Counter:
Counterlist = Counter(list)
I'll get:
Counter({'b': 3, 'a': 3, 'c': 3, 'd': 2})
Then I can select let's say a and get 3:
Counterlist['a'] = 3
But how I can select the occurring number '3'?
Something like:
Counterlist[3] = ['b', 'a', 'c']
Is that possible?

You can write the following
import collections
my_data = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd']
result = collections.defaultdict(list)
for k, v in collections.Counter(my_data).items():
result[v].append(k)
and then access result[3] to obtain the characters with that count.

Related

Merging to lists of different length to for list of dictionaries

Hi please help me develop a logic which does following.
list_1 = [1,2,3]
list_2 = [a,b,c,d,e,f,g,h,i]
Required output (List of dictionaries):
output = [{1:a,2:b,3:c}, {1:d,2:e,3:f}, {1:g,2:h,3:i}]
My script:
return_list = []
k = 0
temp_dict = {}
for i, value in enumerate(list_2):
if k <= len(list_1)-1:
temp_dict[list_1[k]] = value
if k == len(list_1)-1:
k = 0
print temp_dict
return_list.append(temp_dict)
print return_list
print '\n'
else:
k = k + 1
print return_list
My output:
{1: 'a', 2: 'b', 3: 'c'}
[{1: 'a', 2: 'b', 3: 'c'}]
{1: 'd', 2: 'e', 3: 'f'}
[{1: 'd', 2: 'e', 3: 'f'}, {1: 'd', 2: 'e', 3: 'f'}]
{1: 'g', 2: 'h', 3: 'i'}
[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]
[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]
As you can see, temp_dict is getting printed correctly, but return_list is the last temp_dict 3 times.
please help to solve.
The issue here is that you are not reseting temp_dict to a new object.
When you append it to the list, it still maintains the reference to the dict object, after you change it on the next loop, it changes the value on the array because it's the same reference.
If you reset the value it should work
list_1 = [1,2,3]
list_2 = ['a','b','c','d','e','f','g','h','i']
return_list = []
k = 0
temp_dict = {}
for i, value in enumerate(list_2):
if k <= len(list_1)-1:
temp_dict[list_1[k]] = value
if k == len(list_1)-1:
k = 0
print temp_dict
return_list.append(temp_dict)
temp_dict = {} # Here is the change
print return_list
print '\n'
else:
k = k + 1
print return_list
This should work:
list_1 = [1,2,3]
list_2 = ['a','b','c','d','e','f','g','h','i']
output = []
j = 0
for i in range(1, len(list_1) + 1):
output.append(dict(zip(list_1, list_2[j:i * 3])))
j = i * 3
print(output)
The assumption is that you second list is exactly 3 times larger than the first list.
def merge_them(list1, list2):
output = []
i = 0
while i < len(list_2):
output.append(dict(zip(list_1, list_2[i: i + len(list1)])))
i += len(list1)
return output
and you can test it:
test1:
list_1 = [1,2,3]
list_2 = ['a','b','c','d','e','f','g','h','i']
print merge_them(list_1, list_2)
you will get:
[{1: 'a', 2: 'b', 3: 'c'}, {1: 'd', 2: 'e', 3: 'f'}, {1: 'g', 2: 'h', 3: 'i'}]
test2:
list_1 = [1,2,3,]
list_2 = ['a','b','c','d','e']
print merge_them(list_1, list_2)
you will get:
[{1: 'a', 2: 'b', 3: 'c'}, {1: 'd', 2: 'e'}]
test3:
list_1 = [1,2,3,4,5,6,7,8]
list_2 = ['a','b','c','d','e']
print merge_them(list_1, list_2)
you will get:
[{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}]

Python merge two lists with different lengths

Hi please help me develop a logic which does following.
list_1 = [1,2,3]
list_2 = [a,b,c,d,e,f,g,h,i]
Required output (List of dictionaries):
output = [{1:a,2:b,3:c}, {1:d,2:e,3:f}, {1:g,2:h,3:i}]
My script:
return_list = []
k = 0
temp_dict = {}
for i, value in enumerate(list_2):
if k <= len(list_1)-1:
temp_dict[list_1[k]] = value
if k == len(list_1)-1:
k = 0
print temp_dict
return_list.append(temp_dict)
print return_list
print '\n'
else:
k = k + 1
print return_list
My output:
{1: 'a', 2: 'b', 3: 'c'}
[{1: 'a', 2: 'b', 3: 'c'}]
{1: 'd', 2: 'e', 3: 'f'}
[{1: 'd', 2: 'e', 3: 'f'}, {1: 'd', 2: 'e', 3: 'f'}]
{1: 'g', 2: 'h', 3: 'i'}
[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]
[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]
As you can see, temp_dict is getting printed correctly, but return_list is the last temp_dict 3 times.
please help to solve.
return_list = []
k = 0
temp_dict = {}
for i, value in enumerate(list_2):
if k <= len(list_1)-1:
temp_dict[list_1[k]] = value
if k == len(list_1)-1:
k = 0
print temp_dict
return_list.append(temp_dict)
temp_dict = {}
else:
k = k + 1
For simple, you could use zip
list_1 = [1,2,3]
list_2 = ['a','b','c','d','e','f','g','h','i']
chunks = [list_2[idx:idx+3] for idx in range(0, len(list_2), 3)]
output = []
for each in chunks:
output.append(dict(zip(list_1, each)))
print(output)
from itertools import cycle
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
zipped = tuple(zip(cycle(list_1), list_2))
out = list(map(dict, chunks(zipped, len(list_1))))
print(out)
Gives you:
[{1: 'a', 2: 'b', 3: 'c'}, {1: 'd', 2: 'e', 3: 'f'}, {1: 'g', 2: 'h', 3: 'i'}]

Combinations of strings from data

So, we have data. a little big data.
example.
0 -> [a, b]
1 -> c
2 -> [d, e, f]
3 -> [g, h]
4 -> k
5 -> l
we need to combine each of this values. for this example output will be a lot of variants.
acdgkl
acdhkl
acegkl
acehkl
acfgkl
acfhkl
bcdgkl
bcdhkl
bcegkl
bcehkl
bcfgkl
bcfhkl
Quetion: what is this action procedure of combinating values called?)
and how to realize it :)
THX.
The process which turns something like
[['a','b'], ['c'], ['d', 'e', 'f'], ['g', 'h'], ['k'], ['l']]
into the sequence
[['a','c','d','g','k','l'],
['a','c','d','h','k','l'],
...
is called taking the Cartesian product. I don't think there's a name for the specific process of also concatenating the result when they're strings. (Also note that I've made each term a list, even if it's a list of one element, rather than alternating between lists (['a','b']) and scalars ('c')).
This is a common enough process that many languages have built-in support, e.g. Python:
>>> from itertools import product
>>> source = [['a','b'], ['c'], ['d', 'e', 'f'], ['g', 'h'], ['k'], ['l']]
>>> outputs = product(*source)
>>> for out in outputs:
... print(out)
...
('a', 'c', 'd', 'g', 'k', 'l')
('a', 'c', 'd', 'h', 'k', 'l')
('a', 'c', 'e', 'g', 'k', 'l')
('a', 'c', 'e', 'h', 'k', 'l')
('a', 'c', 'f', 'g', 'k', 'l')
('a', 'c', 'f', 'h', 'k', 'l')
('b', 'c', 'd', 'g', 'k', 'l')
('b', 'c', 'd', 'h', 'k', 'l')
('b', 'c', 'e', 'g', 'k', 'l')
('b', 'c', 'e', 'h', 'k', 'l')
('b', 'c', 'f', 'g', 'k', 'l')
('b', 'c', 'f', 'h', 'k', 'l')
Even if the language you're using doesn't support this natively, it's straightforward to implement.
Believe you are looking for Permutation and Combination.
Try using nested loops.
I can't comment on the comment of DSM (as I don't have enough points)
But if you want to concatenate the lists and print them as strings use
print ''.join(out)
instead of
print

How to make every combinaison of element from a list of n lists with m elements? (Python)

From a list of n lists, each having different numbers of element, I want to get all possible combinaison.
I make an example to help understand my problem:
If I have a list of lists like this:
a = [['a','b'], ['c','d'],['e','f','g']]
I would like to get something like this:
[[('a', 'c', 'e')],
[('a', 'c', 'f')],
[('a', 'c', 'g')],
[('a', 'd', 'e')],
[('a', 'd', 'f')],
[('a', 'd', 'g')],
[('b', 'c', 'e')],
[('b', 'c', 'f')],
[('b', 'c', 'g')],
[('b', 'd', 'e')],
[('b', 'd', 'f')],
[('b', 'd', 'g')]]
I get that with this:
list((zip(x,y,z) for x in a[0] for y in a [1] for z in a[2]))
Now I would like a function to do the same thing with any list of lists I pass too it. (No list is empty)
Something recursive like that can maybe work, but I have a hard time figure it out and something less complex and faster is maybe possible.
I found a solution in java here, but I don't know java and I can't translate it.
There's an itertools.product function for this. Just unpack your list as arguments:
>>> from itertools import product
>>> list(product(*a))
[('a', 'c', 'e'), ('a', 'c', 'f'), ('a', 'c', 'g'), ('a', 'd', 'e'), ('a', 'd', 'f'), ('a', 'd', 'g'), ('b', 'c', 'e'), ('b', 'c', 'f'), ('b', 'c', 'g'), ('b', 'd', 'e'), ('b', 'd', 'f'), ('b', 'd', 'g')]
from itertools import product
[[combo] for combo in product(*a)]
Yields:
[[('a', 'c', 'e')],
[('a', 'c', 'f')],
[('a', 'c', 'g')],
[('a', 'd', 'e')],
[('a', 'd', 'f')],
[('a', 'd', 'g')],
[('b', 'c', 'e')],
[('b', 'c', 'f')],
[('b', 'c', 'g')],
[('b', 'd', 'e')],
[('b', 'd', 'f')],
[('b', 'd', 'g')]]
So for a function, you just need:
def boxed_product(somelists):
return [[combo] for combo in product(*somelists)]

How to generate each possible combination of members from two lists (in Python)

I am a Python newbie and I've been trying to find the way to generate each possible combination of members from two lists:
left = ['a', 'b', 'c', 'd', 'e']
right = ['f', 'g', 'h', 'i', 'j']
The resulting list should be something like:
af ag ah ai aj bf bg bh bi bj cf cg ch ci cj etc...
I made several experiments with loops but I can't get it right:
The zip function but it wasn't useful since it just pairs 1 to 1 members:
for x in zip(left,right):
print x
and looping one list for the other just returns the members of one list repeated as many times as the number of members of the second list :(
Any help will be appreciated. Thanks in advance.
You can use for example list comprehension:
left = ['a', 'b', 'c', 'd', 'e']
right = ['f', 'g', 'h', 'i', 'j']
result = [lc + rc for lc in left for rc in right]
print result
The result will look like:
['af', 'ag', 'ah', 'ai', 'aj', 'bf', 'bg', 'bh', 'bi', 'bj', 'cf', 'cg', 'ch', 'ci', 'cj', 'df', 'dg', 'dh', 'di', 'dj', 'ef', 'eg', 'eh', 'ei', 'ej']