Python: Counting occurrences of List element within List - python

I'm trying to count the number of occurrences of elements within a list, if such elements are also lists. The order is also important.
[PSEUDOCODE]
lst = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
print( count(lst) )
> { ['a', 'b', 'c'] : 2, ['d', 'e', 'f']: 1, ['c', 'b', 'a']: 1 }
One important factor is that ['a', 'b', 'c'] != ['c', 'b', 'a']
I have tried:
from collections import counter
print( Counter([tuple(x) for x in lst]) )
print( [[x, list.count(x)] for x in set(lst)] )
Which both resulted in ['a', 'b', 'c'] = ['c', 'b', 'a'], one thing i didn't want
I also tried:
from collections import counter
print( Counter( lst ) )
Which only resulted in error; since lists can't be used as keys in dicts.
Is there a way to do this?

You can't have list as a key to the dict because dictionaries only allows immutable objects as it's key. Hence you need to firstly convert your objects to tuple. Then you may use collection.Counter to get the count of each tuple as:
>>> from collections import Counter
>>> my_list = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
# v to type-cast each sub-list to tuple
>>> Counter(tuple(item) for item in my_list)
Counter({('a', 'b', 'c'): 2, ('d', 'e', 'f'): 1, ('c', 'b', 'a'): 1})

just use collections.Counter on some equivalent type but hashable: the tuple:
import collections
lst = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
c = collections.Counter(tuple(x) for x in lst)
print(c)
result:
Counter({('a', 'b', 'c'): 2, ('d', 'e', 'f'): 1, ('c', 'b', 'a'): 1})

Lists are not hashable, but you can use tuples as a workaround:
l = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
new_l = list(map(tuple, l))
final_l = {a:new_l.count(a) for a in new_l}
Output:
{('a', 'b', 'c'): 2, ('d', 'e', 'f'): 1, ('c', 'b', 'a'): 1}
Or, if you really want to use lists, you can create a custom class to mimic the functionality of a dictionary hashing lists:
class List_Count:
def __init__(self, data):
new_data = list(map(tuple, data))
self.__data = {i:new_data.count(i) for i in new_data}
def __getitem__(self, val):
newval = [b for a, b in self.__data.items() if list(a) == val]
if not newval:
raise KeyError("{} not found".format(val))
return newval[0]
def __repr__(self):
return "{"+"{}".format(', '.join("{}:{}".format(list(a), b) for a, b in self.__data.items()))+"}"
l = List_Count([ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ])
print(l)
print(l[['a', 'b', 'c']])
Output:
{['a', 'b', 'c']:2, ['d', 'e', 'f']:1, ['c', 'b', 'a']:1}
2

Another implementation with lists
l1 = [["a", "b", "c"], ["b", "c", "d"], ["a", "b", "c"], ["c", "b", "a"]]
def unique(l1):
l2 = []
for element in l1:
if element not in l2:
l2.append(element)
return l2
l2 = unique(l1)
for element in l2:
print(element, l1.count(element))
and if you want an dictionary from that you can just change the last part to
output = {element:l1.count(element) for element in unique(l1)}

Don't Use list as variable name.
You can try this approach if you don't want to use any module :
list_1 = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
track={}
for i in list_1:
if tuple(i) not in track:
track[tuple(i)]=1
else:
track[tuple(i)]+=1
print(track)
outoput:
{('a', 'b', 'c'): 2, ('d', 'e', 'f'): 1, ('c', 'b', 'a'): 1}
You can also use default dict:
list_1 = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
track={}
import collections
d=collections.defaultdict(list)
for j,i in enumerate(list_1):
d[tuple(i)].append(j)
print(list(map(lambda x:{x:len(d[x])},d.keys())))

Related

Flatten array recursively in python

from permutations of array I have an array like this:
[['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
which means for 'a' I have 'b' and 'c' and for inner 'b' I have 'c' etc.
I am confused how to make a new array which is representing this logic (not only for three variables) and I need it to look like this:
[['a','b','c'],['a','c','b'],['b','a','c'],['b','c','a'],['c','a','b'],['c','b','a']]
Is it somehow possible? Thank you!
You can write a recursive function in order to flatten the list.
def flatten(permutations):
flattens = []
for permutation in permutations:
if len(permutation) == 2:
flattens.extend([[permutation[0], *j] for j in flatten(permutation[1])])
else:
flattens.extend(permutation)
return flattens
if __name__ == '__main__':
permutations = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
print(flatten(permutations))
Output:
[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]
Slightly shorter recursive solution with a generator:
data = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
def full_combos(d, c = []):
if all(not isinstance(j, list) for j in d):
yield from [c+[j] for j in d]
else:
yield from [j for a, b in d for j in full_combos(b, c+[a])]
print(list(full_combos(data)))
Output:
[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]
The answer by vikas soni is great if you start from your first array. But if you start from a list of elements then the task is much simpler:
from itertools import permutations
ls = ["a", "b", "c"]
list(permutations(ls, len(ls)))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
ls2 = ["a", "b", "c", "d", "e"]
len(list(permutations(ls, len(ls))))
120

Combining elements of two lists [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 2 years ago.
I have two lists:
lst1 = ['a', 'b']
lst2 = ['c', 'd', 'e']
I want to make combinations like this:
[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]
Kindly help me with this. Thanks
>>> import itertools
>>> lst1 = ['a', 'b']
>>> lst2 = ['c', 'd', 'e']
>>> itertools.product(lst1, lst2)
<itertools.product object at 0x7f3571488280>
>>> list(itertools.product(lst1, lst2))
[('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e')]
>>> x = list(itertools.product(lst1, lst2))
>>> [list(y) for y in x]
[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]
>>>
I leave you an example without using libraries too.
Script:
lst1 = ['a', 'b']
lst2 = ['c', 'd', 'e']
lst3 = []
for item_lst1 in lst1:
for item_lst2 in lst2:
lst3.append([item_lst1, item_lst2])
print(lst3)
Output:
[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]
This might do the job for you.
list1 = ['a', 'b']
list2 = ['c', 'd', 'e']
req_list = [[x, y] for x in list1 for y in list2]
This type of combining is called cartesian product or cross product.

Why does array content get wiped and reset to the first result for a recursive function?

The issues stems from the output.append(a) on the third line. This program would ideally output 6 unique permutations of the input string, but instead returns 6 of the first result in the recursive loop. I realize exiting the recursion may have something to do with the array being modified, but how can I circumvent this issue to be able to return an array of solutions?
def permute(a, l, r, output):
if l==r:
output.append(a)
else:
for i in range(l,r+1):
a[l], a[i] = a[i], a[l]
permute(a, l+1, r,output)
a[l], a[i] = a[i], a[l] # backtrack
Driver program to test the above function
string = "ABC"
output = []
n = len(string)
a = list(string)
permute(a, 0, n-1,output)
print(output)
For reference, this is what the output looks like:
[['A', 'C', 'B']]
[['B', 'A', 'C'], ['B', 'A', 'C']]
[['B', 'C', 'A'], ['B', 'C', 'A'], ['B', 'C', 'A']]
[['C', 'B', 'A'], ['C', 'B', 'A'], ['C', 'B', 'A'], ['C', 'B', 'A']]
[['C', 'A', 'B'], ['C', 'A', 'B'], ['C', 'A', 'B'], ['C', 'A', 'B'], ['C', 'A', 'B']]
[['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C']]
When the output should be:
['A', 'B', 'C']
['A', 'C', 'B']
['B', 'A', 'C']
['B', 'C', 'A']
['C', 'B', 'A']
['C', 'A', 'B']
The problem is in the line
output.append(a)
it looks fine, but later on the list a changes, and when you append it to output again, the previous a (that you already appended) changes.
To solve the problem, you can simply use shallow copy. Write this instead:
output.append(a[:])
Do you know there is an excisting function in python?
import itertools
listA = ["A", "B", "C"]
perm = itertools.permutations(listA)
for i in list(perm):
print(i)
Result:
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')

How can I duplicate element of a list in python?

I wonder if there is a more elegant way to do the following. For example with list comprehension.
Consider a simple list :
l = ["a", "b", "c", "d", "e"]
I want to duplicate each elements n times. Thus I did the following :
n = 3
duplic = list()
for li in l:
duplic += [li for i in range(n)]
At the end duplic is :
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e']
You can use
duplic = [li for li in l for _ in range(n)]
This does the same as your code. It adds each element of l (li for li in l) n times (for _ in range n).
You can use:
l = ["a", "b", "c", "d", "e"]
n=3
duplic = [ li for li in l for i in range(n)]
Everytime in python that you write
duplic = list()
for li in l:
duplic +=
there is a good chance that it can be done with a list comprehension.
Try this:
l = ["a", "b", "c", "d", "e"]
print sorted(l * 3)
Output:
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e']
from itertools import chain
n = 4
>>>list(chain.from_iterable(map(lambda x: [x]*n,l)))
['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'e', 'e', 'e', 'e']
In [12]: l
Out[12]: ['a', 'b', 'c', 'd', 'e']
In [13]: n
Out[13]: 3
In [14]: sum((n*[item] for item in l), [])
Out[14]: ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'e']

split list in sub lists by number of elements

In python, if I have the list of elements
l = ['a', 'b', 'c', 'd', 'e', 'f']
and a list of numbers
n = [2, 1, 3]
How I can split the list l by the numbers in n ?
And get this list of lists
[['a', 'b'], ['c'], ['d', 'e', 'f']]
You could use islice:
>>> from itertools import islice
>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>> n = [2, 1, 3]
>>> it = iter(l)
>>> out = [list(islice(it, size)) for size in n]
>>> out
[['a', 'b'], ['c'], ['d', 'e', 'f']]
It's a bit obfuscated, but still:
ll = [[l.pop(0) for _ in range(k)] for k in n]
Note that this traversal will not leave the list intact because of the pop() thingy.
You can create an iterator out of the list. Then call next the appropriate number of times.
>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>> n = [2, 1, 3]
>>> it = iter(l)
>>> [[next(it) for i in xrange(k)] for k in n]
[['a', 'b'], ['c'], ['d', 'e', 'f']]
Yet another way
if __name__ == '__main__':
l = ['a', 'b', 'c', 'd', 'e', 'f']
n = [2, 1, 3]
result = []
for i in n:
j = l[:i]
result.append(j)
l = l[i:]
print result
Gives
[['a', 'b'], ['c'], ['d', 'e', 'f']]
It's not as short as some other solutions, but it sure as hell is readable
cuts = [sum(n[:i]) for i in range(len(n) + 1)]
>>> [l[cuts[i]:cuts[i + 1]] for i in range(len(cuts) - 1)]
[['a', 'b'], ['c'], ['d', 'e', 'f']]
This leaves the list intact:
>>> l
['a', 'b', 'c', 'd', 'e', 'f']
I think this would be most optimized as it will only required len(n) number of iterations.
l = ['a', 'b', 'c', 'd', 'e', 'f']
n = [2, 1, 3]
res = []
temp = 0
for i in n:
res.append(l[temp:temp+i])
temp = temp+i
print res
Returns:
[['a', 'b'], ['c'], ['d', 'e', 'f']]
You can use numpy.split :
>>> np.split(l,[sum(n[:i]) for i in range(len(n))])
[array([], dtype=float64), array(['a', 'b'],
dtype='|S1'), array(['c'],
dtype='|S1'), array(['d', 'e', 'f'],
dtype='|S1')]

Categories

Resources