Multiply factors from nested lists - python

I am trying to multiply factors from the first nested list with the second nested list. The result I get is [0,0,0,0]. Help appreciated.
Faktorer = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]
res = []
for Faktorer[0][i] in Faktorer:
for Faktorer[1][j] in Faktorer:
res.append(i*j)
print(res)

Using a nested loop is right, but your syntax is all mixed up.
>>> Faktorer = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]
>>> res = []
>>> for i in Faktorer[0]:
... for j in Faktorer[1]:
... res.append(i * j)
...
>>> res
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
You can also do this as a list comprehension like this:
>>> [i * j for i in Faktorer[0] for j in Faktorer[1]]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
If Faktorer had an arbitrary number of sublists (making it impossible to do this with a fixed number of nested loops), or if you just didn't want to use nested loops, you could use product to generate the combinations of all the factors and reduce to multiply them:
>>> from functools import reduce
>>> from itertools import product
>>> [reduce(int.__mul__, f, 1) for f in product(*Faktorer)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

What you described in a Kronecker product. If you are allowed to use libraries, you could use numpy.kron:
import numpy as np
res = list(np.kron(Faktorer[0], Faktorer[1]))
print(res)
OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Related

Divide Dataframe into 2 dataframe using index

I need to divide my dataframe into 2 dataframe based on their index
Df1 with this index:[5, 15, 22, 23, 24]
Df2 with this index:[0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
Unable to find solution! Any help would be appreciated
If input is list of index values is possible use Index.isin in boolean indexing (if not exist some values in original index also working correct):
idx = [5, 15, 22, 23, 24]
mask = df.index.isin(idx)
df1 = df[mask]
df2 = df[~mask]
Solution with DataFrame.loc is possible without : and is necessary all values exist in original index:
L1 = [5, 15, 22, 23, 24]
L2 = [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20,
21, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
df1 = df.loc[L1]
df2 = df.loc[L2]
You can use .loc:
df_1 = df.loc[[5, 15, 22, 23, 24], :]
df_2 = df.loc[[0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54], :]
Here is the documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

How to sort a dictionary by value if the value is a sorted list?

I am trying to sort a dictionary based on the value (which is a sorted list).
for example, I have these dictionary items:
1:[2, 6, 10, 14, 20, 46]
2:[1, 2, 3, 14, 16, 18, 23, 37, 41]
3:[1, 2, 4, 6, 10, 27, 45, 290]
4:[3, 5, 13, 34, 39, 40, 47, 555]
I want the output to be like this:
2:[1, 2, 3, 14, 16, 18, 23, 37, 41]
3:[1, 2, 4, 6, 10, 27, 45, 290]
1:[2, 6, 10, 14, 20, 46]
4:[3, 5, 13, 34, 39, 40, 47, 555]
Something like this should do the trick.
x = {
1:[2, 6, 10, 14, 20, 46],
2:[1, 2, 3, 14, 16, 18, 23, 37, 41],
3:[1, 2, 4, 6, 10, 27, 45, 290],
4:[3, 5, 13, 34, 39, 40, 47, 555]
}
b = sorted(x.items(), key=lambda i: i[1])
print(b)
Output:
[(2, [1, 2, 3, 14, 16, 18, 23, 37, 41]), (3, [1, 2, 4, 6, 10, 27, 45, 290]), (1, [2, 6, 10, 14, 20, 46]), (4, [3, 5, 13, 34, 39, 40, 47, 555])]

Reading formatted array from file in Python

I have a file which contains some strings and then two formatted arrays. It looks something like this
megabuck
Hello world
[58, 50, 42, 34, 26, 18, 10, 2,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7]
[57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4]
I don't know the size of the arrays beforehand. Only thing I know is the delimiter for the array which is []. What can be an elegant way to read the arrays.
I am a newbie in python.
Using Regex. re.findall
Ex:
import re
import ast
with open(filename) as infile:
data = infile.read()
for i in re.findall(r"(\[.*?\])", data, flags=re.S):
print(ast.literal_eval(i))
Output:
[58, 50, 42, 34, 26, 18, 10, 2, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
[57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4]
I wouldn't call it elegant but it works
ars = """
megabuck
Hello world
[58, 50, 42, 34, 26, 18, 10, 2,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7]
[57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4]
"""
arrays = []
for a in ars.split("["):
if ']' in a:
arrays.append([i.strip() for i in a.replace("]",'').split(',')])

Trouble printing strings and percentages

Here's my Code:
from collections import Counter
Value = (3, 19, 23, 27, 36, 10, 25, 29, 39, 41, 9, 16, 20, 31, 34, 2, 8, 9, 33, 37, 4, 21, 27, 28, 35, 2, 12, 21, 23, 38, 4, 5, 12, 16, 20, 1, 8, 21, 33, 38, 1, 9, 12, 18, 36, 5, 17, 20, 22, 25, 14, 21, 27, 31, 41, 11, 12, 23, 28, 41, 9, 22, 25, 34, 35, 5, 12, 19, 22, 39, 5, 13, 14, 17, 33, 4, 16, 26, 27, 37, 9, 18, 31, 34, 39, 9, 17, 33, 35, 40, 16, 20, 22, 37, 40, 2, 14, 19, 22, 32, 4, 5, 21, 29, 36, 2, 9, 22, 32, 35, 9, 13, 24, 36, 40, 3, 7, 9, 10, 28, 4, 20, 21, 23, 41, 12, 18, 26, 28, 34, 3, 23, 24, 27, 37, 4, 5, 26, 34, 36, 3, 5, 15, 25, 34, 4, 7, 9, 23, 32, 5, 7, 19, 29, 34, 2, 4, 25, 34, 38, 1, 4, 24, 32, 33, 20, 23, 31, 37, 40, 1, 20, 31, 36, 37, 1, 18, 30, 32, 41, 4, 8, 24, 26, 33, 3, 12, 13, 27, 34, 3, 6, 16, 24, 28, 10, 17, 20, 30, 32, 8, 10, 27, 28, 29, 6, 21, 23, 30, 32, 1, 22, 29, 33, 36, 1, 6, 9, 28, 30, 9, 12, 14, 19, 31, 1, 2, 10, 29, 37, 2, 7, 19, 20, 27, 7, 9, 30, 35, 37, 14, 23, 25, 29, 30, 4, 11, 19, 27, 32, 11, 15, 23, 26, 35, 14, 18, 22, 26, 28, 4, 17, 18, 23, 40, 10, 11, 16, 37, 39, 2, 12, 15, 27, 29, 3, 11, 20, 21, 26, 11, 17, 29, 31, 38, 9, 14, 15, 31, 32, 14, 25, 29, 35, 40, 1, 5, 8, 17, 27, 6, 14, 16, 18, 20, 13, 15, 29, 33, 35, 10, 18, 26, 38, 41, 13, 16, 20, 26, 27, 4, 12, 27, 32, 33, 7, 10, 26, 30, 31, 5, 7, 17, 37, 38, 3, 6, 10, 27, 38, 5, 15, 23, 33, 36, 11, 15, 31, 34, 38, 8, 9, 11, 15, 25, 6, 7, 21, 30, 33, 4, 17, 21, 34, 35, 3, 8, 16, 22, 34, 5, 23, 28, 31, 39, 7, 18, 24, 33, 40, 5, 24, 30, 38, 41, 3, 11, 25, 27, 37, 19, 26, 34, 36, 40, 20, 27, 30, 32, 38, 1, 16, 17, 26, 30, 6, 11, 14, 17, 28, 11, 18, 27, 33, 38, 4, 6, 7, 8, 25, 7, 28, 30, 35, 39, 20, 27, 31, 38, 41, 18, 27, 35, 37, 38, 17, 30, 32, 38, 39, 11, 12, 24, 33, 36, 1, 3, 8, 25, 34, 2, 3, 7, 15, 35, 8, 18, 19, 27, 32, 1, 3, 11, 15, 26, 15, 17, 21, 29, 36, 4, 16, 22, 24, 25, 3, 4, 19, 31, 35, 1, 15, 26, 33, 35, 8, 15, 29, 39, 40, 14, 19, 27, 32, 34, 8, 12, 22, 23, 28, 13, 28, 29, 31, 40, 1, 4, 16, 18, 26, 5, 8, 25, 31, 39, 5, 8, 16, 32, 33, 6, 11, 15, 21, 37, 19, 27, 32, 36, 37, 3, 10, 16, 25, 34, 5, 12, 18, 23, 37, 1, 11, 20, 30, 37, 11, 16, 29, 31, 39, 4, 13, 23, 28, 39, 9, 15, 25, 28, 32, 1, 7, 18, 20, 25, 8, 9, 25, 28, 33, 4, 10, 22, 26, 33, 4, 8, 9, 11, 27, 13, 21, 26, 30, 41, 1, 15, 24, 33, 39, 8, 11, 27, 34, 41, 12, 13, 14, 25, 34, 14, 16, 21, 31, 39, 4, 17, 18, 27, 37, 5, 10, 11, 15, 32, 6, 14, 27, 35, 36, 4, 14, 28, 34, 38, 1, 11, 14, 22, 30, 4, 5, 8, 23, 33, 3, 6, 14, 19, 32, 2, 19, 20, 23, 34, 3, 4, 14, 18, 29, 7, 10, 26, 37, 41, 12, 15, 19, 26, 34, 9, 15, 26, 27, 36, 2, 5, 22, 27, 34, 2, 13, 14, 21, 26, 8, 22, 23, 29, 37, 2, 18, 23, 24, 36, 3, 13, 19, 26, 37, 15, 17, 18, 24, 41, 5, 8, 12, 13, 34, 4, 9, 13, 28, 39, 6, 13, 21, 28, 29, 1, 7, 17, 29, 38, 11, 18, 25, 31, 39, 1, 2, 18, 26, 33, 10, 14, 16, 17, 34, 13, 15, 28, 29, 30, 2, 10, 12, 13, 41, 11, 13, 26, 27, 32, 3, 4, 30, 33, 40, 1, 7, 15, 19, 26, 4, 5, 14, 18, 34, 3, 5, 6, 33, 40, 10, 20, 23, 25, 39, 11, 13, 21, 36, 41, 18, 22, 30, 33, 36, 1, 7, 10, 12, 17, 9, 15, 23, 32, 38, 6, 8, 32, 33, 40, 14, 21, 31, 32, 36, 11, 23, 24, 33, 34, 8, 13, 17, 20, 36, 10, 16, 27, 30, 33, 1, 3, 4, 6, 23, 2, 4, 22, 32, 38, 2, 15, 18, 19, 21, 2, 4, 5, 20, 21, 21, 22, 29, 38, 40, 10, 27, 28, 32, 37, 5, 6, 8, 37, 40, 2, 9, 11, 40, 41, 10, 11, 17, 28, 34, 13, 15, 17, 18, 37, 9, 14, 21, 34, 35, 20, 22, 26, 33, 37, 3, 16, 18, 28, 31, 3, 24, 31, 33, 37, 1, 27, 28, 38, 40, 4, 20, 25, 33, 40, 7, 23, 25, 27, 32, 5, 6, 7, 13, 28, 3, 7, 17, 30, 36, 9, 18, 20, 37, 40, 1, 14, 21, 27, 37, 7, 18, 22, 23, 34, 10, 12, 25, 26, 40, 4, 9, 32, 34, 38, 2, 13, 16, 24, 33, 4, 18, 25, 30, 40, 7, 10, 27, 28, 33, 13, 16, 21, 27, 29, 10, 21, 32, 34, 35, 6, 16, 17, 22, 27, 16, 21, 24, 40, 41, 1, 2, 5, 9, 37, 1, 10, 17, 19, 37, 11, 20, 21, 30, 36, 1, 5, 22, 26, 30, 9, 12, 25, 34, 38, 2, 3, 6, 18, 28, 3, 6, 20, 22, 26, 3, 4, 9, 24, 27, 4, 7, 29, 32, 36, 4, 11, 18, 37, 40, 13, 15, 23, 34, 41, 17, 18, 34, 35, 36, 2, 14, 26, 33, 38, 1, 4, 22, 23, 32, 6, 13, 19, 33, 39, 8, 11, 12, 29, 40, 1, 4, 9, 11, 16, 5, 6, 13, 15, 28, 3, 10, 18, 19, 40, 1, 15, 25, 28, 38, 4, 19, 23, 26, 28, 1, 5, 11, 12, 36, 3, 14, 19, 23, 38, 1, 5, 9, 11, 35, 6, 10, 19, 23, 30, 14, 16, 23, 35, 40, 3, 5, 10, 15, 35, 4, 28, 33, 38, 41, 2, 17, 20, 22, 30, 7, 19, 20, 26, 31, 11, 13, 16, 17, 25, 3, 10, 16, 28, 39, 9, 18, 29, 37, 40, 2, 3, 25, 34, 41, 11, 28, 33, 38, 40, 4, 5, 9, 23, 41, 2, 9, 10, 22, 41, 1, 4, 9, 20, 27, 4, 7, 17, 24, 27, 3, 14, 15, 24, 29, 9, 13, 15, 27, 37, 17, 19, 26, 30, 34, 12, 16, 19, 21, 41, 1, 16, 24, 29, 39, 8, 21, 23, 25, 32, 2, 3, 22, 24, 40, 13, 15, 17, 28, 33, 1, 6, 19, 38, 41, 7, 17, 18, 21, 26, 3, 8, 21, 30, 35, 2, 24, 29, 33, 38, 7, 17, 18, 31, 36, 24, 31, 33, 38, 39, 21, 24, 27, 35, 41, 5, 9, 17, 23, 40, 17, 22, 29, 32, 34, 13, 14, 25, 36, 39, 2, 10, 22, 30, 32, 2, 16, 23, 28, 38, 5, 27, 30, 36, 41, 8, 23, 26, 29, 38, 6, 11, 22, 37, 38, 9, 26, 30, 35, 36, 10, 19, 29, 32, 40, 6, 11, 26, 27, 40, 1, 16, 21, 30, 33, 13, 21, 22, 33, 34, 5, 13, 23, 26, 37, 2, 11, 21, 24, 37, 4, 5, 20, 31, 34, 1, 20, 22, 27, 33, 1, 6, 11, 12, 14, 5, 10, 11, 36, 40, 5, 15, 36, 38, 39, 3, 6, 9, 17, 28, 13, 21, 24, 26, 37, 6, 10, 14, 17, 37, 5, 18, 23, 36, 38, 11, 13, 28, 36, 38, 5, 8, 17, 29, 41, 2, 12, 14, 27, 39, 4, 11, 20, 21, 39, 2, 6, 14, 31, 38, 2, 25, 26, 32, 33, 4, 10, 26, 32, 37, 5, 13, 14, 32, 41, 1, 4, 20, 21, 27, 2, 7, 14, 32, 34, 9, 12, 23, 24, 29, 9, 19, 22, 30, 33, 7, 13, 19, 35, 41, 1, 18, 19, 21, 24, 15, 18, 29, 31, 33, 14, 15, 16, 29, 35, 5, 16, 21, 33, 36, 12, 17, 23, 32, 34, 17, 20, 23, 33, 40, 5, 9, 24, 30, 34, 9, 14, 21, 26, 28, 1, 7, 19, 21, 24, 1, 5, 22, 33, 37, 1, 5, 17, 23, 29, 3, 11, 17, 24, 30, 7, 12, 18, 22, 34, 5, 7, 13, 21, 38, 9, 24, 32, 40, 41, 3, 17, 26, 33, 38, 5, 6, 7, 14, 30, 8, 19, 20, 28, 30, 4, 15, 26, 29, 32, 5, 7, 25, 28, 35, 10, 23, 35, 37, 41, 3, 11, 17, 19, 41, 4, 24, 25, 29, 34, 8, 22, 25, 27, 33, 2, 16, 20, 30, 31, 14, 19, 20, 33, 35, 15, 16, 17, 20, 38, 14, 23, 28, 29, 34, 7, 21, 26, 29, 36, 8, 9, 10, 25, 30, 12, 17, 26, 28, 36, 6, 10, 11, 20, 40, 7, 8, 21, 40, 41, 9, 15, 23, 33, 36, 1, 9, 27, 35, 36, 9, 13, 17, 18, 41, 12, 13, 24, 25, 29, 16, 21, 23, 36, 39, 6, 9, 19, 26, 28, 11, 27, 30, 35, 37, 3, 6, 23, 34, 38, 9, 14, 19, 24, 35, 5, 6, 20, 33, 41, 9, 13, 33, 37, 38, 9, 13, 24, 25, 36, 11, 15, 20, 36, 38, 6, 19, 24, 25, 37, 8, 12, 27, 33, 34, 9, 12, 18, 28, 36, 12, 17, 29, 34, 40, 1, 2, 10, 24, 34, 7, 25, 31, 36, 39, 1, 6, 7, 21, 28, 4, 5, 6, 18, 39, 22, 24, 26, 29, 36, 12, 23, 30, 31, 39, 28, 34, 35, 37, 38, 4, 8, 11, 22, 25, 5, 10, 23, 33, 40, 9, 19, 21, 34, 38, 6, 8, 9, 19, 35, 16, 32, 33, 34, 37, 1, 3, 6, 12, 15, 4, 29, 30, 39, 40, 16, 21, 24, 32, 36, 8, 19, 25, 30, 34, 10, 18, 24, 25, 39, 20, 24, 27, 28, 38, 12, 16, 18, 20, 31, 1, 11, 13, 19, 36, 20, 22, 25, 34, 38, 2, 10, 17, 27, 35, 4, 6, 8, 26, 40, 9, 12, 21, 28, 30, 19, 21, 26, 28, 37, 2, 4, 5, 32, 36, 15, 22, 25, 31, 41, 7, 13, 16, 29, 39, 10, 21, 23, 25, 37, 18, 22, 30, 35, 38, 10, 12, 13, 18, 39, 14, 17, 20, 30, 36, 21, 24, 25, 29, 35, 4, 7, 22, 33, 36, 3, 7, 10, 23, 37, 8, 9, 14, 25, 31, 7, 11, 30, 38, 40, 12, 13, 18, 26, 40, 2, 12, 22, 26, 36, 2, 19, 20, 23, 24, 1, 5, 16, 17, 27, 8, 17, 22, 35, 38, 1, 3, 19, 36, 38, 1, 12, 29, 35, 38, 2, 3, 27, 39, 41, 7, 11, 26, 35, 36, 14, 16, 18, 31, 35, 17, 24, 35, 38, 41, 3, 11, 27, 31, 37, 3, 25, 26, 29, 32, 8, 17, 19, 30, 33, 3, 18, 28, 31, 32, 1, 9, 13, 25, 36, 4, 6, 8, 17, 29, 7, 11, 24, 32, 33, 2, 7, 20, 26, 41, 5, 7, 14, 28, 38, 3, 8, 9, 21, 26, 5, 11, 25, 40, 41, 10, 13, 14, 29, 34, 3, 5, 15, 31, 37, 16, 24, 27, 31, 34, 4, 13, 16, 27, 40, 24, 26, 27, 32, 39, 10, 11, 12, 15, 23, 15, 31, 34, 36, 38, 5, 9, 21, 29, 35, 9, 13, 17, 22, 40, 13, 22, 28, 36, 39, 5, 16, 17, 19, 30, 13, 18, 25, 26, 38, 1, 5, 9, 27, 28, 3, 5, 19, 37, 39, 10, 13, 24, 32, 40, 3, 7, 8, 17, 24, 1, 10, 11, 12, 20, 4, 11, 15, 19, 23, 17, 26, 30, 38, 39, 6, 7, 10, 31, 41, 18, 21, 24, 32, 39, 4, 8, 12, 17, 24, 3, 4, 7, 23, 38, 11, 17, 23, 29, 34, 1, 10, 25, 30, 32, 2, 18, 20, 24, 27, 16, 24, 27, 31, 38, 1, 13, 17, 18, 36, 6, 8, 13, 20, 24, 5, 13, 16, 25, 29, 7, 13, 32, 33, 34, 15, 19, 26, 31, 35, 5, 9, 14, 21, 26, 1, 10, 16, 20, 31, 1, 6, 25, 33, 35, 3, 13, 14, 17, 30, 2, 5, 9, 38, 39, 22, 24, 27, 35, 36, 10, 18, 20, 27, 36, 5, 7, 20, 33, 35, 3, 8, 13, 21, 37, 13, 19, 22, 25, 39, 3, 14, 21, 23, 38, 15, 20, 30, 32, 35, 8, 9, 12, 24, 32, 10, 25, 35, 36, 37, 1, 11, 14, 18, 36, 7, 9, 25, 31, 38, 4, 5, 6, 16, 34, 5, 8, 9, 14, 28, 12, 17, 20, 23, 33, 1, 4, 8, 20, 25, 1, 8, 15, 27, 34, 10, 14, 15, 18, 23, 3, 18, 29, 31, 33, 6, 10, 16, 18, 21, 1, 4, 11, 14, 25, 7, 11, 19, 28, 37, 8, 13, 22, 28, 33, 10, 11, 14, 20, 37, 13, 15, 17, 19, 36, 10, 22, 26, 33, 36, 10, 15, 18, 19, 35, 8, 9, 20, 31, 33, 3, 8, 18, 31, 34, 2, 14, 24, 27, 31, 9, 11, 12, 15, 22, 13, 14, 15, 37, 39, 3, 4, 7, 27, 34, 6, 14, 24, 30, 38, 2, 3, 19, 21, 30, 5, 6, 15, 31, 32, 1, 6, 24, 26, 30, 9, 11, 25, 29, 30, 1, 16, 22, 30, 34, 1, 2, 12, 19, 34, 4, 10, 13, 20, 31, 18, 28, 33, 35, 39, 22, 23, 25, 27, 36, 1, 7, 28, 33, 34, 1, 12, 14, 32, 39, 16, 25, 28, 30, 32, 12, 19, 28, 31, 39, 12, 19, 25, 27, 32, 8, 14, 20, 31, 38, 10, 19, 21, 23, 32, 2, 3, 24, 30, 36, 3, 11, 22, 26, 39, 4, 13, 29, 33, 37, 9, 22, 30, 36, 39, 2, 8, 9, 33, 34, 2, 5, 15, 23, 28, 11, 23, 26, 29, 31, 10, 13, 15, 29, 34, 13, 18, 21, 22, 32, 4, 5, 7, 20, 24, 1, 15, 17, 29, 38, 2, 7, 20, 35, 38, 26, 33, 36, 37, 38, 3, 5, 14, 24, 39, 7, 8, 9, 24, 28, 8, 18, 29, 36, 37, 7, 10, 21, 25, 29, 3, 8, 26, 35, 36, 1, 21, 22, 24, 36, 5, 7, 19, 33, 37, 8, 22, 29, 30, 37, 5, 7, 14, 17, 25, 11, 22, 26, 33, 38, 7, 19, 20, 27, 34, 11, 18, 19, 30, 37, 5, 11, 14, 34, 38, 6, 9, 23, 29, 39, 2, 16, 22, 24, 37, 1, 8, 9, 29, 35, 15, 18, 20, 23, 28, 10, 14, 26, 30, 37, 3, 4, 6, 30, 36, 2, 5, 8, 18, 27, 5, 9, 21, 35, 37, 6, 8, 14, 22, 38, 21, 22, 27, 29, 37, 3, 4, 11, 34, 38, 7, 12, 17, 21, 32, 4, 8, 15, 26, 38, 6, 8, 23, 33, 35, 7, 19, 27, 37, 39, 4, 10, 24, 25, 38, 3, 5, 21, 36, 37, 6, 20, 27, 30, 34, 10, 14, 19, 21, 29, 13, 15, 30, 36, 37, 2, 15, 17, 31, 32, 8, 17, 34, 36, 37, 1, 11, 26, 29, 35, 10, 16, 17, 35, 36, 3, 17, 22, 29, 32, 7, 9, 10, 18, 38, 1, 5, 15, 18, 24, 9, 11, 12, 14, 35, 7, 10, 28, 29, 34, 10, 21, 24, 26, 38, 7, 22, 26, 34, 39, 1, 8, 10, 25, 31, 2, 3, 9, 18, 28, 5, 10, 24, 32, 33, 2, 8, 13, 14, 37, 3, 11, 15, 33, 35, 11, 17, 23, 24, 28, 10, 14, 20, 35, 37, 1, 19, 24, 26, 38, 20, 30, 32, 36, 39, 6, 24, 28, 35, 38, 3, 9, 10, 19, 28, 18, 22, 25, 31, 33, 3, 6, 8, 20, 21, 7, 16, 19, 21, 39, 10, 19, 27, 31, 36, 13, 21, 26, 29, 37, 14, 15, 21, 24, 25, 5, 10, 17, 21, 29, 3, 12, 25, 26, 32, 12, 14, 21, 34, 36, 5, 9, 14, 15, 38, 2, 27, 28, 30, 39, 5, 9, 11, 18, 38, 7, 15, 27, 30, 38, 13, 20, 24, 27, 35, 14, 15, 19, 20, 22, 4, 10, 13, 16, 19, 1, 2, 16, 35, 38, 6, 10, 20, 27, 34, 5, 6, 11, 21, 34, 6, 15, 28, 30, 36, 2, 5, 26, 28, 33, 14, 19, 21, 23, 33, 9, 10, 20, 22, 30, 1, 2, 13, 29, 34, 1, 6, 13, 17, 18, 3, 8, 11, 21, 28, 4, 7, 10, 16, 22, 3, 5, 6, 15, 22, 22, 24, 32, 35, 38, 14, 18, 26, 27, 28, 14, 20, 24, 26, 35, 4, 24, 26, 33, 37, 2, 14, 16, 25, 29, 1, 3, 24, 26, 36, 8, 20, 30, 31, 36, 6, 9, 17, 24, 35, 14, 18, 27, 35, 39, 5, 15, 19, 37, 38, 4, 6, 11, 18, 26, 18, 21, 22, 26, 32, 3, 7, 19, 20, 21, 3, 5, 16, 21, 33, 6, 14, 20, 22, 35, 6, 11, 13, 25, 28, 8, 13, 16, 38, 39, 19, 20, 24, 27, 29, 5, 14, 22, 31, 38, 18, 20, 22, 32, 36, 16, 19, 25, 34, 39, 4, 16, 19, 33, 38, 19, 22, 29, 33, 39, 11, 22, 26, 30, 32, 5, 6, 25, 27, 36, 5, 20, 32, 37, 38, 6, 13, 31, 35, 39, 2, 6, 22, 37, 39, 8, 22, 27, 34, 35, 3, 10, 21, 24, 29, 7, 21, 24, 30, 35, 14, 16, 21, 36, 38, 5, 10, 25, 30, 33, 3, 5, 9, 17, 23, 2, 8, 26, 34, 38, 1, 7, 23, 31, 38, 1, 10, 16, 22, 38, 5, 8, 9, 18, 27, 7, 13, 22, 25, 34, 4, 6, 11, 30, 33, 16, 19, 20, 21, 39, 11, 19, 21, 31, 36, 2, 3, 6, 12, 24, 3, 12, 16, 20, 37, 11, 18, 27, 33, 39, 7, 21, 24, 27, 30, 15, 16, 18, 21, 31, 5, 10, 22, 25, 26, 11, 16, 18, 29, 35, 1, 9, 25, 31, 32, 6, 7, 10, 23, 31, 5, 22, 28, 29, 37, 15, 18, 19, 24, 39, 4, 9, 12, 25, 32, 5, 7, 30, 31, 37, 2, 4, 9, 31, 39, 8, 10, 15, 31, 39, 2, 9, 22, 24, 30, 6, 11, 21, 28, 29, 14, 19, 20, 32, 33, 12, 13, 15, 20, 33, 1, 2, 19, 22, 37, 2, 12, 30, 36, 39, 2, 3, 5, 7, 28, 2, 25, 28, 32, 33, 8, 16, 19, 25, 28, 19, 30, 31, 36, 39, 4, 12, 23, 26, 36, 10, 11, 21, 27, 29, 1, 2, 5, 8, 26, 1, 12, 16, 18, 25, 15, 20, 21, 24, 34, 7, 12, 19, 31, 32, 12, 17, 19, 26, 32, 9, 18, 23, 29, 36, 12, 16, 30, 37, 38, 1, 11, 18, 28, 31, 17, 23, 28, 31, 35, 7, 18, 23, 32, 33, 4, 13, 15, 35, 37, 3, 22, 24, 30, 34, 7, 9, 28, 30, 37, 5, 12, 24, 34, 39, 2, 4, 9, 10, 11, 1, 8, 15, 18, 20, 12, 15, 22, 23, 34, 6, 8, 14, 32, 33, 1, 7, 15, 22, 24, 9, 15, 16, 18, 38, 13, 17, 22, 28, 31, 12, 19, 22, 34, 35, 2, 7, 10, 15, 34, 13, 22, 25, 31, 37, 1, 16, 27, 30, 31, 12, 24, 28, 30, 32, 14, 17, 21, 30, 38, 2, 6, 12, 17, 27, 1, 2, 8, 18, 39, 6, 10, 12, 21, 28, 10, 20, 22, 29, 31, 1, 5, 12, 22, 23, 5, 7, 8, 21, 26, 4, 9, 14, 21, 24, 6, 31, 32, 34, 39, 7, 17, 24, 33, 34, 3, 16, 22, 24, 36, 1, 12, 20, 31, 35, 1, 2, 28, 34, 38, 5, 21, 27, 30, 36, 8, 10, 14, 31, 32, 1, 3, 16, 26, 35, 2, 4, 26, 30, 35, 2, 6, 24, 31, 36, 5, 11, 12, 15, 37, 2, 15, 16, 17, 20, 8, 10, 16, 38, 39, 2, 12, 14, 16, 39, 15, 17, 27, 31, 38, 1, 3, 16, 32, 35, 6, 14, 16, 23, 30, 12, 22, 31, 34, 37, 3, 10, 12, 22, 39, 4, 20, 28, 38, 39, 2, 7, 18, 35, 38, 6, 7, 10, 16, 31, 2, 7, 13, 34, 38, 17, 18, 24, 26, 29, 11, 17, 21, 26, 35, 2, 3, 12, 29, 31, 13, 15, 20, 25, 38, 15, 22, 30, 34, 39, 18, 22, 35, 37, 39, 3, 13, 14, 35, 37, 1, 5, 30, 31, 32, 6, 7, 12, 28, 34, 1, 3, 16, 23, 32, 1, 7, 8, 13, 22, 1, 4, 11, 20, 36, 6, 14, 27, 28, 29, 11, 14, 24, 25, 38, 1, 12, 26, 28, 29, 1, 6, 8, 22, 37, 5, 11, 22, 35, 37, 3, 7, 11, 15, 34, 4, 7, 22, 31, 33, 2, 7, 11, 19, 29, 18, 19, 21, 22, 25, 13, 17, 18, 19, 34, 1, 20, 24, 30, 38, 11, 13, 14, 35, 36, 3, 11, 28, 31, 34, 2, 6, 11, 27, 36, 1, 9, 11, 16, 25, 15, 16, 20, 26, 38, 4, 6, 31, 34, 36, 8, 14, 19, 23, 31, 11, 22, 23, 28, 39, 2, 16, 19, 36, 38, 8, 12, 33, 36, 37, 3, 11, 21, 30, 39, 3, 8, 9, 14, 29, 7, 9, 20, 21, 37, 8, 13, 14, 30, 35, 3, 11, 26, 37, 38, 2, 8, 15, 33, 34, 2, 9, 14, 17, 26, 2, 6, 15, 34, 35, 1, 7, 23, 24, 37, 5, 7, 10, 26, 30, 8, 10, 11, 22, 33, 5, 8, 20, 30, 33, 11, 15, 21, 28, 39, 15, 16, 17, 32, 33, 17, 23, 28, 29, 39, 4, 7, 14, 18, 39, 8, 17, 27, 35, 37, 5, 7, 18, 21, 37, 4, 17, 18, 25, 31, 20, 26, 28, 31, 35, 5, 11, 14, 19, 23, 4, 7, 17, 27, 30, 6, 26, 27, 33, 35, 22, 25, 35, 36, 38, 4, 13, 15, 23, 26, 7, 9, 28, 32, 39, 14, 22, 25, 35, 37, 10, 23, 25, 35, 38, 5, 16, 17, 20, 21, 6, 10, 27, 28, 35, 2, 9, 20, 31, 37, 1, 19, 26, 31, 36, 3, 12, 24, 27, 29, 10, 17, 18, 31, 37, 3, 13, 17, 26, 32, 2, 7, 19, 25, 31, 2, 8, 19, 23, 27, 9, 16, 28, 34, 35, 1, 12, 33, 35, 39, 11, 15, 24, 32, 36, 4, 8, 12, 13, 35, 8, 14, 18, 22, 28, 1, 20, 22, 31, 32, 2, 8, 16, 17, 35, 1, 10, 16, 20, 25, 6, 18, 23, 33, 38, 5, 10, 21, 22, 30, 13, 17, 18, 19, 35, 4, 10, 12, 20, 37, 20, 22, 24, 26, 32, 18, 26, 28, 30, 36, 1, 2, 21, 25, 33, 3, 4, 9, 16, 35, 5, 12, 14, 28, 29, 2, 10, 17, 21, 38, 1, 9, 11, 28, 34, 2, 33, 35, 37, 38, 2, 6, 10, 35, 38, 4, 13, 16, 19, 35, 4, 7, 9, 12, 15, 4, 7, 21, 30, 32, 6, 13, 22, 30, 32, 2, 9, 13, 18, 33, 3, 10, 29, 37, 38, 1, 13, 23, 27, 32, 14, 15, 17, 24, 27, 8, 10, 14, 26, 31, 5, 15, 17, 31, 35, 12, 27, 31, 33, 36, 9, 12, 13, 15, 38, 13, 16, 27, 29, 32, 8, 14, 20, 24, 26, 12, 19, 23, 24, 33, 2, 9, 14, 16, 19, 9, 24, 31, 38, 39, 13, 22, 25, 26, 35, 10, 14, 17, 24, 36, 8, 11, 16, 17, 32, 1, 11, 27, 28, 31, 15, 23, 34, 35, 36, 1, 7, 14, 17, 35, 1, 24, 36, 37, 39, 9, 13, 16, 23, 36, 12, 13, 23, 24, 37, 1, 7, 13, 31, 33, 8, 12, 16, 27, 28, 6, 13, 28, 29, 30, 9, 10, 22, 23, 39, 1, 3, 5, 27, 35, 6, 13, 16, 34, 39, 14, 23, 25, 34, 37, 9, 17, 18, 36, 37, 11, 16, 24, 30, 36, 9, 19, 22, 26, 34, 11, 20, 22, 26, 29, 5, 24, 28, 29, 32, 10, 23, 25, 30, 33, 6, 8, 22, 31, 33, 1, 8, 10, 23, 38, 5, 9, 12, 32, 33, 5, 29, 31, 33, 35, 1, 4, 19, 23, 37, 24, 29, 30, 32, 36, 5, 12, 13, 23, 33, 1, 17, 18, 25, 26, 6, 9, 20, 33, 39, 3, 25, 30, 34, 39, 1, 5, 11, 14, 26, 4, 12, 13, 31, 35, 19, 20, 23, 24, 36, 1, 2, 6, 24, 34, 15, 32, 35, 36, 37, 1, 8, 14, 25, 38, 5, 8, 21, 23, 34, 1, 4, 10, 34, 39, 6, 7, 24, 36, 37, 6, 8, 22, 26, 38, 11, 17, 24, 27, 35, 12, 13, 16, 21, 29, 17, 18, 19, 28, 38, 23, 25, 31, 32, 39, 22, 24, 30, 31, 37, 3, 17, 21, 27, 31, 4, 24, 27, 28, 33, 13, 16, 26, 27, 31, 6, 22, 24, 34, 39, 20, 27, 30, 35, 37, 1, 4, 7, 15, 26, 8, 14, 16, 32, 34, 5, 23, 27, 37, 38, 4, 7, 20, 29, 35, 2, 11, 18, 25, 36, 2, 4, 5, 6, 10, 8, 11, 21, 25, 31, 5, 12, 23, 24, 27, 16, 18, 19, 28, 39, 1, 11, 13, 18, 39, 4, 10, 12, 28, 30, 9, 13, 25, 26, 30, 9, 11, 16, 20, 38, 4, 7, 18, 33, 39, 4, 11, 17, 31, 37, 15, 31, 34, 36, 38, 9, 11, 29, 34, 37, 4, 12, 21, 23, 31, 11, 12, 13, 31, 38, 12, 18, 31, 33, 39, 5, 9, 14, 16, 30, 7, 9, 11, 17, 30, 2, 5, 7, 14, 22, 22, 24, 28, 29, 31, 1, 10, 20, 21, 32, 12, 29, 31, 32, 33, 8, 13, 20, 30, 33, 5, 6, 13, 23, 28, 6, 7, 18, 22, 25, 14, 18, 19, 22, 35, 4, 9, 11, 26, 31, 1, 21, 23, 24, 36, 12, 25, 26, 29, 34, 3, 11, 13, 20, 26, 12, 14, 23, 26, 33, 13, 22, 24, 29, 37, 1, 16, 17, 22, 29, 2, 12, 17, 32, 34, 2, 10, 21, 25, 29, 8, 25, 26, 34, 39, 2, 28, 31, 33, 34, 7, 11, 17, 18, 21, 3, 6, 8, 12, 37, 8, 10, 14, 17, 35, 20, 21, 25, 26, 38, 15, 19, 22, 23, 37, 9, 12, 15, 20, 25, 6, 9, 18, 23, 31, 19, 20, 27, 36, 38, 1, 5, 12, 28, 37, 1, 3, 6, 24, 25, 14, 23, 26, 32, 39, 8, 24, 29, 35, 36, 18, 21, 32, 36, 37)
V = Counter(Value)
print(V)
#For 'x' amount of number sets (from spreadsheet).
#List Size(Ex. 917) do 917x5
myInt = 4585
#Divide for percentage
percentdiv = [x / myInt for x in Value]
#Times new value by 100 and format 2 decimal places to get percentage
for i in percentdiv:
Percentage = ("%{:.2f}".format(i * 100))
for x in Value:
print (x, Percentage,)
I'm trying to figure out how to get python3 to print something like:
1 = %0.34, 2 = %0.21, 3 = %0.03
4 = %0.22, 5 = %0.13, 6 = %0.01
But for some reason I get:
1 %0.07
3 %0.07
8 %0.07
25 %0.07
34 %0.07
2 %0.07
3 %0.07
7 %0.07
15 %0.07
35 %0.07
8 %0.07
18 %0.07
19 %0.07
27 %0.07
32 %0.07
1 %0.07
I noticed too that the numbers were looping themselves.
I like what Counter does I like the formatting, but I need the percentage not just the number of times. Can someone help, please?
I think you need the percentage of each value's count in the Value list.Please correct if I am wrong,
for x,i in zip(Value,percentdiv):
print("{}={:.2f}".format(x,i*100),end='')
By default print function have the \n in a end so it should be,
import sys
for i in percentdiv:
Percentage = ("%{:.2f}".format(i * 100))
for x in Value:
sys.stdout.write(x, Percentage,)
sys.stdout.write("\n")
From here code copied

seaborn heatmap color scheme based on row values

I have a dataframe, reproduced partly as such:
import pandas as pd
import numpy as np
tab = pd.DataFrame(np.array([[ 46, 39, 25, 29, 21, 12, 33, 32, 70, 109, 144, 158, 161,
184, 163, 113, 117, 82, 76, 88, 77, 76, 64, 35],
[ 39, 33, 29, 29, 26, 14, 25, 33, 60, 83, 126, 117, 111,
148, 141, 104, 92, 75, 78, 74, 63, 67, 52, 39],
[ 30, 27, 14, 11, 20, 17, 21, 31, 48, 62, 83, 78, 88,
90, 80, 67, 53, 61, 47, 54, 50, 48, 35, 26],
[ 30, 24, 19, 15, 17, 10, 12, 18, 34, 69, 88, 79, 109,
95, 89, 82, 53, 46, 53, 57, 39, 41, 26, 29],
[ 37, 31, 18, 12, 30, 13, 15, 19, 51, 61, 74, 81, 77,
100, 96, 74, 60, 57, 42, 48, 43, 40, 29, 25],
[ 14, 8, 14, 11, 13, 7, 9, 15, 42, 49, 50, 44, 53,
42, 31, 31, 30, 27, 33, 25, 27, 17, 20, 17],
[ 10, 15, 6, 10, 15, 11, 7, 18, 28, 43, 49, 37, 41,
33, 37, 32, 26, 28, 19, 24, 19, 19, 13, 18],
[ 9, 9, 8, 12, 7, 11, 4, 8, 14, 15, 23, 30, 29,
34, 25, 39, 22, 20, 15, 23, 12, 19, 14, 13],
[ 0, 3, 4, 1, 1, 0, 3, 4, 4, 5, 3, 5, 6,
7, 3, 3, 6, 4, 2, 3, 3, 2, 2, 2],
[ 3, 0, 1, 0, 0, 0, 1, 1, 4, 8, 2, 4, 7,
2, 2, 9, 3, 5, 1, 5, 2, 0, 4, 1]]), index =
['Stadsdeel Zuid', 'Stadsdeel West', 'Stadsdeel Nieuw-West',
'Stadsdeel Centrum', 'Stadsdeel Oost', 'Stadsdeel Noord',
'Wijk 00 Amstelveen', 'Stadsdeel Zuidoost', 'Wijk 00',
'Wijk 00 Aalsmeer'])
and I created a heatmap as such
ax = sns.heatmap(tab, linewidths=.5 ,robust=True ,annot_kws = {'size':14})
ax.tick_params(labelsize=14)
ax.figure.set_size_inches((12, 10))
I would like though that values to anchor the colormap are based on min-max values per row so that also rows with lower values are well visible. (in reality the table contains many more rows with low values that the heatmap barely shows color-wise)
How to achieve this ?
I would normalize the tab rows by the maximum value in each row with:
tab_n = tab.div(tab.max(axis=1), axis=0)
where tab_n is the normalized tab having values in the range [0,1]. Hope that helps. Plotting tab_n should return an heatmap like this:

Categories