Code that counts the number of times two elements in an iterator type (list or tuple) appear at the same time. It is also sample code for a combination of iterator-type elements (ʻitertools.combination), an immutable set (
frozenset), and a keyed
sorted`.
count_colocation2.py
import itertools
import typing
from itertools import chain
def count_colocation2(
iteriter: typing.Iterable
) -> typing.Dict[frozenset, int]:
"""
Count the number of times two elements in an iterator type (list or tuple) appear at the same time.
"""
if not isinstance(iteriter, typing.Iterable):
raise TypeError()
#Prepare the combination
comb = itertools.combinations(frozenset(chain.from_iterable(iteriter)), 2)
comb = [frozenset(item) for item in comb]
#Check in order and add the number of combinations
d = {key: 0 for key in comb}
for entry in iteriter:
for pair in comb:
t = tuple(pair)
if t[0] in entry and t[1] in entry:
d[pair] += 1
return d
1.py
from pprint import pp
from count_colocation2 import count_colocation2
ll = [
["a", "b", "c"],
["d", "e", "f"],
["a", "d", "f"],
["b", "d", "f"],
]
coloc2 = count_colocation2(ll)
#Sort values in descending order
pp(sorted(coloc2.items(), key=lambda item: item[1], reverse=True))
"""
[(frozenset({'f', 'd'}), 3),
(frozenset({'f', 'e'}), 1),
(frozenset({'f', 'b'}), 1),
(frozenset({'f', 'a'}), 1),
(frozenset({'d', 'e'}), 1),
(frozenset({'d', 'b'}), 1),
(frozenset({'d', 'a'}), 1),
(frozenset({'b', 'a'}), 1),
(frozenset({'b', 'c'}), 1),
(frozenset({'a', 'c'}), 1),
(frozenset({'f', 'c'}), 0),
(frozenset({'b', 'e'}), 0),
(frozenset({'e', 'a'}), 0),
(frozenset({'e', 'c'}), 0),
(frozenset({'d', 'c'}), 0)]
"""
frozenset
frozenset
is an immutable version of set
. You can change the contents of set
with ʻadd or ʻupdate
, but you cannot change the contents of frozen set
. On the other hand, frozenset
can be used as a dictionary key because it can get a hash.
#Uncommenting the next line will result in an error.
# print(hash(set(["a", "b", "c"])))
# TypeError: unhashable type: 'set'
#It completes normally.
print(hash(frozenset(["a", "b", "c"])))
There is tuple
in the immutable type that can be the key of the same dictionary. tuple
distinguishes the order of the elements, but flozenset
does not.
typing.Sequence
、typing.Iterable
Python has classes like list
, tuple
, set
, frozenset
, but these can be categorized by common functionality. list
and tuple
are classified as sequence type (typing.Sequence
) because they can be accessed by index. Also, list
, tuple
, set
, and frozenset
are classified as typing.Iterable
because they provide iterators.
typing.Sequence
and typing.Iterable
are defined as types and can be used in type hints, as well as in ʻis instance and ʻis subclass
.
ʻItertools.combinations allows you to create a list of any number of combinatorial tuples from iterator type (
typing.Iterable`) elements.
set
/ frozen set
set
/ frozenset
is not accessible at index [index]
. You can access it by converting it to list
or tuple
once with list ()
or tuple ()
. Since it is an iterator type, for
can be used.
You can specify a lambda expression for key = ...
in sorted ()
to sort the dictionary non-destructively. Note that the dictionary key / value pairs are retrieved with dict.items ()
. If you pass dict
itself to sorted
, the key name will be the target of sorted ()
.
Sort by key
sorted(dict.items(), key=lambda item: item[0]) #ascending order
sorted(dict.items(), key=lambda item: item[0], reverse=True) #descending order
Sort by value
sorted(dict.items(), key=lambda item: item[1]) #ascending order
sorted(dict.items(), key=lambda item: item[1], reverse=True) #descending order
Recommended Posts