This question already has answers here:
How do I sort a dictionary by key?
(32 answers)
Closed 10 months ago.
How can i sort this to get the expected sequence?
My program:
product_pairs = [[(1, 2), (1, 3), (1, 6), (1, 8), (1, 9), (2, 3), (2, 6), (2, 8), (2, 9), (3, 6), (3, 8), (3, 9), (6, 8), (6, 9), (8, 9)], [(0, 5), (0, 9), (5, 9)], [(0, 9)], [(0, 4), (0, 5), (0, 7), (0, 9), (4, 5), (4, 7), (4, 9), (5, 7), (5, 9), (7, 9)], [(3, 8)], [(1, 3), (1, 6), (1, 8), (3, 6), (3, 8), (6, 8)], [(0, 5), (0, 9), (5, 9)], [(3, 8)], [(0, 4), (0, 5), (0, 7), (4, 5), (4, 7), (5, 7)], [(0, 1), (0, 2), (0, 3), (0, 6), (1, 2), (1, 3), (1, 6), (2, 3), (2, 6), (3, 6)]]
emptydic={}
cooccurences={}
for i in product_pairs:
for y in i:
if y not in emptydic:
emptydic[y]=1
else:
emptydic[y]+=1
print(emptydic)
my result:
{(1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1, (2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1, (3, 6): 3, (3, 8): 4, (3, 9): 1, (6, 8): 2, (6, 9): 1, (8, 9): 1, (0, 5): 4, (0, 9): 4, (5, 9): 3, (0, 4): 2, (0, 7): 2, (4, 5): 2, (4, 7): 2, (4, 9): 1, (5, 7): 2, (7, 9): 1, (0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 6): 1}
expected sequence:
{(0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 4): 2, (0, 5): 4, (0, 6): 1, (0, 7): 2, (0, 9): 4,
(1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1,
(2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1,
(3, 6): 3, (3, 8): 4, (3, 9): 1,
(4, 5): 2, (4, 7): 2, (4, 9): 1,
(5, 7): 2, (5, 9): 3,
(6, 8): 2, (6, 9): 1,
(7, 9): 1,
(8, 9): 1}
Thank you!
For Python 3.7 or higher:
dict(sorted(emptydic.items()))
{(0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 4): 2, (0, 5): 4, (0, 6): 1, (0, 7): 2, (0, 9): 4, (1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1, (2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1, (3, 6): 3, (3, 8): 4, (3, 9): 1, (4, 5): 2, (4, 7): 2, (4, 9): 1, (5, 7): 2, (5, 9): 3, (6, 8): 2, (6, 9): 1, (7, 9): 1, (8, 9): 1}
I have the numbers [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16].
I want to find all permutations of numbers of exactly 9 characters. For example, if I have 8 single digit numbers, the last one can't be a double digit, because it would exceed the character limit.
Clarifications:
The same number can be reused, so for example the number 1 nine times is valid.
There can be any number of double digit numbers, as long as the total character length of the digits is exactly 9. For example, 1, 10, 12, 15, 16 is valid.
I tried itertools.permutations, but I couldn't get it to work with two-digit numbers.
I would use a mixture of combination and permutations.
First find all of the combinations of the data which add up to the desired length. Then for each unique combination, find their permutations. There's probably some work to be done here which can limit the amount of incorrect combinations checked:
import itertools
def perm_of_length(data, length):
for i in range(len(data)):
for comb in itertools.combinations(data, i + 1):
if sum(map(len, (map(str, comb)))) == length:
for perm in itertools.permutations(comb):
yield perm
for perm in perm_of_length([0, 1, 2, 3, 10, 12], 4):
print(perm)
Outputs:
(10, 12)
(12, 10)
(0, 1, 10)
(0, 10, 1)
(1, 0, 10)
(1, 10, 0)
(10, 0, 1)
(10, 1, 0)
(0, 1, 12)
(0, 12, 1)
(1, 0, 12)
(1, 12, 0)
(12, 0, 1)
(12, 1, 0)
(0, 2, 10)
(0, 10, 2)
(2, 0, 10)
(2, 10, 0)
(10, 0, 2)
(10, 2, 0)
(0, 2, 12)
(0, 12, 2)
(2, 0, 12)
(2, 12, 0)
(12, 0, 2)
(12, 2, 0)
(0, 3, 10)
(0, 10, 3)
(3, 0, 10)
(3, 10, 0)
(10, 0, 3)
(10, 3, 0)
(0, 3, 12)
(0, 12, 3)
(3, 0, 12)
(3, 12, 0)
(12, 0, 3)
(12, 3, 0)
(1, 2, 10)
(1, 10, 2)
(2, 1, 10)
(2, 10, 1)
(10, 1, 2)
(10, 2, 1)
(1, 2, 12)
(1, 12, 2)
(2, 1, 12)
(2, 12, 1)
(12, 1, 2)
(12, 2, 1)
(1, 3, 10)
(1, 10, 3)
(3, 1, 10)
(3, 10, 1)
(10, 1, 3)
(10, 3, 1)
(1, 3, 12)
(1, 12, 3)
(3, 1, 12)
(3, 12, 1)
(12, 1, 3)
(12, 3, 1)
(2, 3, 10)
(2, 10, 3)
(3, 2, 10)
(3, 10, 2)
(10, 2, 3)
(10, 3, 2)
(2, 3, 12)
(2, 12, 3)
(3, 2, 12)
(3, 12, 2)
(12, 2, 3)
(12, 3, 2)
(0, 1, 2, 3)
(0, 1, 3, 2)
(0, 2, 1, 3)
(0, 2, 3, 1)
(0, 3, 1, 2)
(0, 3, 2, 1)
(1, 0, 2, 3)
(1, 0, 3, 2)
(1, 2, 0, 3)
(1, 2, 3, 0)
(1, 3, 0, 2)
(1, 3, 2, 0)
(2, 0, 1, 3)
(2, 0, 3, 1)
(2, 1, 0, 3)
(2, 1, 3, 0)
(2, 3, 0, 1)
(2, 3, 1, 0)
(3, 0, 1, 2)
(3, 0, 2, 1)
(3, 1, 0, 2)
(3, 1, 2, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)
Proof this works:
for perm in perm_of_length([0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16], 9):
assert sum(map(len, map(str, perm))) == 9
NOTE: this is much slower than it needs to be.
A simple, non-performant, brute-force approach with filtering:
symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16]
symbols = [str(x) for x in symbols]
perms = itertools.chain.from_iterable(
itertools.permutations(symbols, i) for i in range(10)
)
perms = ("".join(x) for x in perms)
perms = [x for x in perms if len(x) <= 9]
>>> len(perms)
13600046
>>> perms[:4]
['', '0', '1', '2']
>>> perms[-4:]
['987643102', '987643120', '987643201', '987643210']
One can drop the empty string by simply skipping the first item in perms.
A similar approach which does not use strings:
symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16]
lengths = {k: int(math.log10(k)) + 1 if k != 0 else 1 for k in symbols}
perms = itertools.chain.from_iterable(
itertools.permutations(symbols, i) for i in range(10)
)
perms = [x for x in perms if sum(lengths[k] for k in x) <= 9]
>>> len(perms)
13600046
If numbers can be reused (as mentioned in the comments), I would consider itertools.product instead of itertools.permutations:
symbols = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 15, 16]
lengths = {k: int(math.log10(k)) + 1 if k != 0 else 1 for k in symbols}
perms = itertools.product(symbols, repeat=9)
perms = [x for x in perms if sum(lengths[k] for k in x) <= 9]
I am trying to print my dataframe as a matrix. To do so, I want to use an array. To be clear:
I have a dictionary, Y, which is like this:
{(0, 0): {(0, 0): 0, (1, 0): 1, (0, 1): 1, (0, 2): 2, (0, 3): 3, (1, 3): 4, (0, 4): 10, (1, 4): 9, (0, 5): 11, (1, 1): 2, (1, 2): 5, (2, 2): 6, (2, 4): 8, (1, 5): 10, (2, 0): 10, (3, 0): 9, (2, 1): 7, (3, 1): 8, (3, 2): 7, (2, 3): 7, (3, 4): 9, (2, 5): 9, (3, 5): 10, (3, 3): 8}, (1, 0): {(1, 0): 0, (0, 0): 1, (1, 1): 1, (0, 1): 2, (0, 2): 3, (0, 3): 4, (1, 3): 5, (0, 4): 11, (1, 4): 10, (0, 5): 12, (1, 2): 6, (2, 2): 7, (2, 4): 9, (1, 5): 11, (2, 0): 11, (3, 0): 10, (2, 1): 8, (3, 1): 9, (3, 2): 8, (2, 3): 8, (3, 4): 10, (2, 5): 10, (3, 5): 11, (3, 3): 9}, (0, 1): {(0, 1): 0, (0, 0): 1, (0, 2): 1, (1, 0): 2, (0, 3): 2, (1, 3): 3, (0, 4): 9, (1, 4): 8, (0, 5): 10, (1, 1): 3, (1, 2): 4, (2, 2): 5, (2, 4): 7, (1, 5): 9, (2, 0): 9, (3, 0): 8, (2, 1): 6, (3, 1): 7, (3, 2): 6, (2, 3): 6, (3, 4): 8, (2, 5): 8, (3, 5): 9, (3, 3): 7}, (0, 2): {(0, 2): 0, (0, 1): 1, (0, 3): 1, (0, 0): 2, (1, 0): 3, (1, 3): 2, (0, 4): 8, (1, 4): 7, (0, 5): 9, (1, 1): 4, (1, 2): 3, (2, 2): 4, (2, 4): 6, (1, 5): 8, (2, 0): 8, (3, 0): 7, (2, 1): 5, (3, 1): 6, (3, 2): 5, (2, 3): 5, (3, 4): 7, (2, 5): 7, (3, 5): 8, (3, 3): 6}, (0, 3): {(0, 3): 0, (0, 2): 1, (1, 3): 1, (0, 0): 3, (1, 0): 4, (0, 1): 2, (0, 4): 7, (1, 4): 6, (0, 5): 8, (1, 1): 5, (1, 2): 2, (2, 2): 3, (2, 4): 5, (1, 5): 7, (2, 0): 7, (3, 0): 6, (2, 1): 4, (3, 1): 5, (3, 2): 4, (2, 3): 4, (3, 4): 6, (2, 5): 6, (3, 5): 7, (3, 3): 5}, (1, 3): {(1, 3): 0, (0, 3): 1, (1, 2): 1, (0, 0): 4, (1, 0): 5, (0, 1): 3, (0, 2): 2, (0, 4): 6, (1, 4): 5, (0, 5): 7, (1, 1): 6, (2, 2): 2, (2, 4): 4, (1, 5): 6, (2, 0): 6, (3, 0): 5, (2, 1): 3, (3, 1): 4, (3, 2): 3, (2, 3): 3, (3, 4): 5, (2, 5): 5, (3, 5): 6, (3, 3): 4}, (0, 4): {(0, 4): 0, (1, 4): 1, (0, 5): 1, (0, 0): 10, (1, 0): 11, (0, 1): 9, (0, 2): 8, (0, 3): 7, (1, 3): 6, (1, 1): 12, (1, 2): 5, (2, 2): 4, (2, 4): 2, (1, 5): 2, (2, 0): 8, (3, 0): 7, (2, 1): 5, (3, 1): 6, (3, 2): 5, (2, 3): 3, (3, 4): 3, (2, 5): 3, (3, 5): 4, (3, 3): 6}, (1, 4): {(1, 4): 0, (0, 4): 1, (2, 4): 1, (1, 5): 1, (0, 0): 9, (1, 0): 10, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 5): 2, (1, 1): 11, (1, 2): 4, (2, 2): 3, (2, 0): 7, (3, 0): 6, (2, 1): 4, (3, 1): 5, (3, 2): 4, (2, 3): 2, (3, 4): 2, (2, 5): 2, (3, 5): 3, (3, 3): 5}, (0, 5): {(0, 5): 0, (0, 4): 1, (0, 0): 11, (1, 0): 12, (0, 1): 10, (0, 2): 9, (0, 3): 8, (1, 3): 7, (1, 4): 2, (1, 1): 13, (1, 2): 6, (2, 2): 5, (2, 4): 3, (1, 5): 3, (2, 0): 9, (3, 0): 8, (2, 1): 6, (3, 1): 7, (3, 2): 6, (2, 3): 4, (3, 4): 4, (2, 5): 4, (3, 5): 5, (3, 3): 7}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 0): 2, (0, 1): 3, (0, 2): 4, (0, 3): 5, (1, 3): 6, (0, 4): 12, (1, 4): 11, (0, 5): 13, (1, 2): 7, (2, 2): 8, (2, 4): 10, (1, 5): 12, (2, 0): 12, (3, 0): 11, (2, 1): 9, (3, 1): 10, (3, 2): 9, (2, 3): 9, (3, 4): 11, (2, 5): 11, (3, 5): 12, (3, 3): 10}, (1, 2): {(1, 2): 0, (1, 3): 1, (2, 2): 1, (0, 0): 5, (1, 0): 6, (0, 1): 4, (0, 2): 3, (0, 3): 2, (0, 4): 5, (1, 4): 4, (0, 5): 6, (1, 1): 7, (2, 4): 3, (1, 5): 5, (2, 0): 5, (3, 0): 4, (2, 1): 2, (3, 1): 3, (3, 2): 2, (2, 3): 2, (3, 4): 4, (2, 5): 4, (3, 5): 5, (3, 3): 3}, (2, 2): {(2, 2): 0, (1, 2): 1, (2, 1): 1, (3, 2): 1, (2, 3): 1, (0, 0): 6, (1, 0): 7, (0, 1): 5, (0, 2): 4, (0, 3): 3, (1, 3): 2, (0, 4): 4, (1, 4): 3, (0, 5): 5, (1, 1): 8, (2, 4): 2, (1, 5): 4, (2, 0): 4, (3, 0): 3, (3, 1): 2, (3, 4): 3, (2, 5): 3, (3, 5): 4, (3, 3): 2}, (2, 4): {(2, 4): 0, (1, 4): 1, (2, 3): 1, (3, 4): 1, (2, 5): 1, (0, 0): 8, (1, 0): 9, (0, 1): 7, (0, 2): 6, (0, 3): 5, (1, 3): 4, (0, 4): 2, (0, 5): 3, (1, 1): 10, (1, 2): 3, (2, 2): 2, (1, 5): 2, (2, 0): 6, (3, 0): 5, (2, 1): 3, (3, 1): 4, (3, 2): 3, (3, 5): 2, (3, 3): 4}, (1, 5): {(1, 5): 0, (1, 4): 1, (0, 0): 10, (1, 0): 11, (0, 1): 9, (0, 2): 8, (0, 3): 7, (1, 3): 6, (0, 4): 2, (0, 5): 3, (1, 1): 12, (1, 2): 5, (2, 2): 4, (2, 4): 2, (2, 0): 8, (3, 0): 7, (2, 1): 5, (3, 1): 6, (3, 2): 5, (2, 3): 3, (3, 4): 3, (2, 5): 3, (3, 5): 4, (3, 3): 6}, (2, 0): {(2, 0): 0, (3, 0): 1, (0, 0): 10, (1, 0): 11, (0, 1): 9, (0, 2): 8, (0, 3): 7, (1, 3): 6, (0, 4): 8, (1, 4): 7, (0, 5): 9, (1, 1): 12, (1, 2): 5, (2, 2): 4, (2, 4): 6, (1, 5): 8, (2, 1): 3, (3, 1): 2, (3, 2): 5, (2, 3): 5, (3, 4): 7, (2, 5): 7, (3, 5): 8, (3, 3): 6}, (3, 0): {(3, 0): 0, (2, 0): 1, (3, 1): 1, (0, 0): 9, (1, 0): 10, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 4): 7, (1, 4): 6, (0, 5): 8, (1, 1): 11, (1, 2): 4, (2, 2): 3, (2, 4): 5, (1, 5): 7, (2, 1): 2, (3, 2): 4, (2, 3): 4, (3, 4): 6, (2, 5): 6, (3, 5): 7, (3, 3): 5}, (2, 1): {(2, 1): 0, (2, 2): 1, (3, 1): 1, (0, 0): 7, (1, 0): 8, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 5, (1, 4): 4, (0, 5): 6, (1, 1): 9, (1, 2): 2, (2, 4): 3, (1, 5): 5, (2, 0): 3, (3, 0): 2, (3, 2): 2, (2, 3): 2, (3, 4): 4, (2, 5): 4, (3, 5): 5, (3, 3): 3}, (3, 1): {(3, 1): 0, (3, 0): 1, (2, 1): 1, (0, 0): 8, (1, 0): 9, (0, 1): 7, (0, 2): 6, (0, 3): 5, (1, 3): 4, (0, 4): 6, (1, 4): 5, (0, 5): 7, (1, 1): 10, (1, 2): 3, (2, 2): 2, (2, 4): 4, (1, 5): 6, (2, 0): 2, (3, 2): 3, (2, 3): 3, (3, 4): 5, (2, 5): 5, (3, 5): 6, (3, 3): 4}, (3, 2): {(3, 2): 0, (2, 2): 1, (3, 3): 1, (0, 0): 7, (1, 0): 8, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 5, (1, 4): 4, (0, 5): 6, (1, 1): 9, (1, 2): 2, (2, 4): 3, (1, 5): 5, (2, 0): 5, (3, 0): 4, (2, 1): 2, (3, 1): 3, (2, 3): 2, (3, 4): 4, (2, 5): 4, (3, 5): 5}, (2, 3): {(2, 3): 0, (2, 2): 1, (2, 4): 1, (0, 0): 7, (1, 0): 8, (0, 1): 6, (0, 2): 5, (0, 3): 4, (1, 3): 3, (0, 4): 3, (1, 4): 2, (0, 5): 4, (1, 1): 9, (1, 2): 2, (1, 5): 3, (2, 0): 5, (3, 0): 4, (2, 1): 2, (3, 1): 3, (3, 2): 2, (3, 4): 2, (2, 5): 2, (3, 5): 3, (3, 3): 3}, (3, 4): {(3, 4): 0, (2, 4): 1, (0, 0): 9, (1, 0): 10, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 4): 3, (1, 4): 2, (0, 5): 4, (1, 1): 11, (1, 2): 4, (2, 2): 3, (1, 5): 3, (2, 0): 7, (3, 0): 6, (2, 1): 4, (3, 1): 5, (3, 2): 4, (2, 3): 2, (2, 5): 2, (3, 5): 3, (3, 3): 5}, (2, 5): {(2, 5): 0, (2, 4): 1, (3, 5): 1, (0, 0): 9, (1, 0): 10, (0, 1): 8, (0, 2): 7, (0, 3): 6, (1, 3): 5, (0, 4): 3, (1, 4): 2, (0, 5): 4, (1, 1): 11, (1, 2): 4, (2, 2): 3, (1, 5): 3, (2, 0): 7, (3, 0): 6, (2, 1): 4, (3, 1): 5, (3, 2): 4, (2, 3): 2, (3, 4): 2, (3, 3): 5}, (3, 5): {(3, 5): 0, (2, 5): 1, (0, 0): 10, (1, 0): 11, (0, 1): 9, (0, 2): 8, (0, 3): 7, (1, 3): 6, (0, 4): 4, (1, 4): 3, (0, 5): 5, (1, 1): 12, (1, 2): 5, (2, 2): 4, (2, 4): 2, (1, 5): 4, (2, 0): 8, (3, 0): 7, (2, 1): 5, (3, 1): 6, (3, 2): 5, (2, 3): 3, (3, 4): 3, (3, 3): 6}, (3, 3): {(3, 3): 0, (3, 2): 1, (0, 0): 8, (1, 0): 9, (0, 1): 7, (0, 2): 6, (0, 3): 5, (1, 3): 4, (0, 4): 6, (1, 4): 5, (0, 5): 7, (1, 1): 10, (1, 2): 3, (2, 2): 2, (2, 4): 4, (1, 5): 6, (2, 0): 6, (3, 0): 5, (2, 1): 3, (3, 1): 4, (2, 3): 3, (3, 4): 5, (2, 5): 5, (3, 5): 6}}
Using pandas I converted the dictionary to a dataframe:
df = pd.DataFrame(Y)
df.index = [*df.index]
df.columns = [*df.columns]
arraydf = df.to_numpy()
This is the dataframe I get:
(0, 0) (1, 0) (0, 1) (0, 2) ... (3, 4) (2, 5) (3, 5) (3, 3)
(0, 0) 0 1 1 2 ... 9 9 10 8
(1, 0) 1 0 2 3 ... 10 10 11 9
(0, 1) 1 2 0 1 ... 8 8 9 7
(0, 2) 2 3 1 0 ... 7 7 8 6
(0, 3) 3 4 2 1 ... 6 6 7 5
(1, 3) 4 5 3 2 ... 5 5 6 4
(0, 4) 10 11 9 8 ... 3 3 4 6
(1, 4) 9 10 8 7 ... 2 2 3 5
(0, 5) 11 12 10 9 ... 4 4 5 7
(1, 1) 2 1 3 4 ... 11 11 12 10
(1, 2) 5 6 4 3 ... 4 4 5 3
(2, 2) 6 7 5 4 ... 3 3 4 2
(2, 4) 8 9 7 6 ... 1 1 2 4
(1, 5) 10 11 9 8 ... 3 3 4 6
(2, 0) 10 11 9 8 ... 7 7 8 6
(3, 0) 9 10 8 7 ... 6 6 7 5
(2, 1) 7 8 6 5 ... 4 4 5 3
(3, 1) 8 9 7 6 ... 5 5 6 4
(3, 2) 7 8 6 5 ... 4 4 5 1
(2, 3) 7 8 6 5 ... 2 2 3 3
(3, 4) 9 10 8 7 ... 0 2 3 5
(2, 5) 9 10 8 7 ... 2 0 1 5
(3, 5) 10 11 9 8 ... 3 1 0 6
(3, 3) 8 9 7 6 ... 5 5 6 0
Then, I convert the df to an array:
arraydf = df.to_numpy()
This is my output now:
[ 0 1 1 2 3 4 10 9 11 2 5 6 8 10 10 9 7 8 7 7 9 9 10 8]
[ 1 0 2 3 4 5 11 10 12 1 6 7 9 11 11 10 8 9 8 8 10 10 11 9]
[ 1 2 0 1 2 3 9 8 10 3 4 5 7 9 9 8 6 7 6 6 8 8 9 7]
[2 3 1 0 1 2 8 7 9 4 3 4 6 8 8 7 5 6 5 5 7 7 8 6]
[3 4 2 1 0 1 7 6 8 5 2 3 5 7 7 6 4 5 4 4 6 6 7 5]
[4 5 3 2 1 0 6 5 7 6 1 2 4 6 6 5 3 4 3 3 5 5 6 4]
[10 11 9 8 7 6 0 1 1 12 5 4 2 2 8 7 5 6 5 3 3 3 4 6]
[ 9 10 8 7 6 5 1 0 2 11 4 3 1 1 7 6 4 5 4 2 2 2 3 5]
[11 12 10 9 8 7 1 2 0 13 6 5 3 3 9 8 6 7 6 4 4 4 5 7]
[ 2 1 3 4 5 6 12 11 13 0 7 8 10 12 12 11 9 10 9 9 11 11 12 10]
[5 6 4 3 2 1 5 4 6 7 0 1 3 5 5 4 2 3 2 2 4 4 5 3]
[6 7 5 4 3 2 4 3 5 8 1 0 2 4 4 3 1 2 1 1 3 3 4 2]
[ 8 9 7 6 5 4 2 1 3 10 3 2 0 2 6 5 3 4 3 1 1 1 2 4]
[10 11 9 8 7 6 2 1 3 12 5 4 2 0 8 7 5 6 5 3 3 3 4 6]
[10 11 9 8 7 6 8 7 9 12 5 4 6 8 0 1 3 2 5 5 7 7 8 6]
[ 9 10 8 7 6 5 7 6 8 11 4 3 5 7 1 0 2 1 4 4 6 6 7 5]
[7 8 6 5 4 3 5 4 6 9 2 1 3 5 3 2 0 1 2 2 4 4 5 3]
[ 8 9 7 6 5 4 6 5 7 10 3 2 4 6 2 1 1 0 3 3 5 5 6 4]
[7 8 6 5 4 3 5 4 6 9 2 1 3 5 5 4 2 3 0 2 4 4 5 1]
[7 8 6 5 4 3 3 2 4 9 2 1 1 3 5 4 2 3 2 0 2 2 3 3]
[ 9 10 8 7 6 5 3 2 4 11 4 3 1 3 7 6 4 5 4 2 0 2 3 5]
[ 9 10 8 7 6 5 3 2 4 11 4 3 1 3 7 6 4 5 4 2 2 0 1 5]
[10 11 9 8 7 6 4 3 5 12 5 4 2 4 8 7 5 6 5 3 3 1 0 6]
[ 8 9 7 6 5 4 6 5 7 10 3 2 4 6 6 5 3 4 1 3 5 5 6 0]
My question is: How can I get the final array to seem a matrix? I want all the lines of the same lenghts and to be in the right order (have "nice" readable columns also)
EDIT:
asked infos:
arraydf.shape
(24, 24)
arraydf.dtype
int64
df.dtypes
(0, 0) int64
(0, 1) int64
(0, 2) int64
(1, 2) int64
(0, 3) int64
(0, 4) int64
(1, 4) int64
(0, 5) int64
(1, 5) int64
(1, 0) int64
(2, 0) int64
(1, 1) int64
(1, 3) int64
(2, 3) int64
(3, 0) int64
(2, 1) int64
(2, 2) int64
(2, 4) int64
(2, 5) int64
(3, 5) int64
(3, 1) int64
(3, 2) int64
(3, 3) int64
(3, 4) int64
dtype: object
df.info
<bound method DataFrame.info of (0, 0) (0, 1) (0, 2) (1, 2) ... (3, 1) (3, 2) (3, 3) (3, 4)
(0, 0) 0 1 2 3 ... 8 9 10 11
(0, 1) 1 0 1 2 ... 7 8 9 10
(0, 2) 2 1 0 1 ... 6 7 8 9
(1, 2) 3 2 1 0 ... 5 6 7 8
(0, 3) 3 2 1 2 ... 7 8 9 10
(0, 4) 4 3 2 3 ... 8 9 10 11
(1, 4) 5 4 3 4 ... 9 10 11 12
(0, 5) 5 4 3 4 ... 9 10 11 12
(1, 5) 6 5 4 5 ... 10 11 12 13
(1, 0) 5 4 3 2 ... 3 4 5 6
(2, 0) 6 5 4 3 ... 2 3 4 5
(1, 1) 4 3 2 1 ... 4 5 6 7
(1, 3) 4 3 2 1 ... 6 7 8 9
(2, 3) 5 4 3 2 ... 7 8 9 10
(3, 0) 7 6 5 4 ... 1 2 3 4
(2, 1) 7 6 5 4 ... 3 4 5 6
(2, 2) 8 7 6 5 ... 4 5 6 7
(2, 4) 14 13 12 11 ... 6 5 4 3
(2, 5) 13 12 11 10 ... 5 4 3 2
(3, 5) 12 11 10 9 ... 4 3 2 1
(3, 1) 8 7 6 5 ... 0 1 2 3
(3, 2) 9 8 7 6 ... 1 0 1 2
(3, 3) 10 9 8 7 ... 2 1 0 1
(3, 4) 11 10 9 8 ... 3 2 1 0
If you want to print line-by-line and still have things aligned you can do the following:
>>> for l in str(df.to_numpy()).split("\n"):
... print(l)
...
[[ 0 1 1 2 3 4 10 9 11 2 5 6 8 10 10 9 7 8 7 7 9 9 10 8]
[ 1 2 0 1 2 3 9 8 10 3 4 5 7 9 9 8 6 7 6 6 8 8 9 7]
[ 2 3 1 0 1 2 8 7 9 4 3 4 6 8 8 7 5 6 5 5 7 7 8 6]
[ 3 4 2 1 0 1 7 6 8 5 2 3 5 7 7 6 4 5 4 4 6 6 7 5]
...
If you are trying to make the array look nice, then pprint is your friend (see this nice article).
If you are just trying to see it then use
print(df)
Pandas will display it so you know how the data looks. If you are using jupyter it will display in a fancy table which is why I like using a jupyter notebook when working with data frames/matrices.
At first, I apologize because I am a newbie...
I have one 3d list of tuple:
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
I want to find duplicates and display like this, no matter the position of numbers ,only to have the same numbers :
{ (0,1,6): 2,
(4,5,1): 2,
(3,2,1): 1 }
I want to count the similar tuples.
Any suggestions?
You say you are initializing your list with:
mylist = [[[(x,y,z) for x in range(7)] for y in range(7)] for z in range(7)]
At this point, you can change how you initialize it:
from collections import Counter
# take off some square brackets
mylist = [(x,y,z) for x in range(7) for y in range(7) for z in range(7)]
dict(Counter(tuple(sorted(tup)) for tup in mylist))
Output:
{(0, 0, 0): 1, (0, 0, 1): 3, (0, 0, 2): 3, (0, 0, 3): 3, (0, 0, 4): 3, (0, 0, 5): 3, (0, 0, 6): 3, (0, 1, 1): 3, (0, 1, 2): 6, (0, 1, 3): 6, (0, 1, 4): 6, (0, 1, 5): 6, (0, 1, 6): 6, (0, 2, 2): 3, (0, 2, 3): 6, (0, 2, 4): 6, (0, 2, 5): 6, (0, 2, 6): 6, (0, 3, 3): 3, (0, 3, 4): 6, (0, 3, 5): 6, (0, 3, 6): 6, (0, 4, 4): 3, (0, 4, 5): 6, (0, 4, 6): 6, (0, 5, 5): 3, (0, 5, 6): 6, (0, 6, 6): 3, (1, 1, 1): 1, (1, 1, 2): 3, (1, 1, 3): 3, (1, 1, 4): 3, (1, 1, 5): 3, (1, 1, 6): 3, (1, 2, 2): 3, (1, 2, 3): 6, (1, 2, 4): 6, (1, 2, 5): 6, (1, 2, 6): 6, (1, 3, 3): 3, (1, 3, 4): 6, (1, 3, 5): 6, (1, 3, 6): 6, (1, 4, 4): 3, (1, 4, 5): 6, (1, 4, 6): 6, (1, 5, 5): 3, (1, 5, 6): 6, (1, 6, 6): 3, (2, 2, 2): 1, (2, 2, 3): 3, (2, 2, 4): 3, (2, 2, 5): 3, (2, 2, 6): 3, (2, 3, 3): 3, (2, 3, 4): 6, (2, 3, 5): 6, (2, 3, 6): 6, (2, 4, 4): 3, (2, 4, 5): 6, (2, 4, 6): 6, (2, 5, 5): 3, (2, 5, 6): 6, (2, 6, 6): 3, (3, 3, 3): 1, (3, 3, 4): 3, (3, 3, 5): 3, (3, 3, 6): 3, (3, 4, 4): 3, (3, 4, 5): 6, (3, 4, 6): 6, (3, 5, 5): 3, (3, 5, 6): 6, (3, 6, 6): 3, (4, 4, 4): 1, (4, 4, 5): 3, (4, 4, 6): 3, (4, 5, 5): 3, (4, 5, 6): 6, (4, 6, 6): 3, (5, 5, 5): 1, (5, 5, 6): 3, (5, 6, 6): 3, (6, 6, 6): 1}
If you don't want to change how you initialized it:
def flatten(alist):
for item in alist:
if isinstance(item, list):
yield from flatten(item)
else:
yield item
dict(Counter(tuple(sorted(tup)) for tup in flatten(mylist)))
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
#first please make a list of sorted tuples to ignore the sequence of numbers
sorted_list = [sorted(x) for x in list]
#then count the tuple in sorted list and ignore count for duplicated items.
list = [ { x: sorted_list.count(sorted(x)) } for i, x in enumerate(list) if sorted(x) not in sorted_list[:i]]
print(list);
import pandas as pd
import numpy as np
# make a dataframe out of the list
df = pd.DataFrame(data=ll)
# sort columns and find duplicates
df_dup = df[pd.DataFrame(np.sort(df.values), columns=df.columns, index=df.index)
.duplicated(keep=False)]
# return duplicates as dictionary of tuples
result = df_dup.T.apply(tuple).to_dict()
# return number of duplicates found
n = len(result)
Use Counter from collections:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter(map(lambda x: tuple(sorted(x)),list)))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
Or a-style-of list comprehension:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter([tuple(sorted(i)) for i in list]))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
Also, list is a replica of a python keyword, after making that a variable, you would't have any access to the keyword anymore, so it's better to rename that variable, and so rename it in my code.
Edit:
Use:
dict(Counter([tuple(sorted(x)) for i in list for x in i]))
as suggestion, not to use keyword in python as a variable, here is the keywords in python:
import keyword
keyword_list = keyword.kwlist
for your question, here is my answer :
from collections import Counter
result = dict(Counter([tuple(sorted(item)) for item in your_list ]))