conversion of list to bitarray in python - python

I am trying to build a DES code using my humble python knowledge:
I get the following error: " xor_lf = l1 ^ Bn
TypeError: bitarray object expected for bitwise operation"
Do I need to convert Bn or l1 to bitarray? And how?
from bitarray import bitarray
iptable=[57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6
]
pc1=[56, 48, 40, 32, 24, 16, 8,
0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3
]
expTable=[31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8, 9, 10, 11, 12,
11, 12, 13, 14, 15, 16,
15, 16, 17, 18, 19, 20,
19, 20, 21, 22, 23, 24,
23, 24, 25, 26, 27, 28,
27, 28, 29, 30, 31, 0]
pc2 = [13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31]
# The (in)famous S-boxes
__sbox = [
# S1
[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
# S2
[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
# S3
[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
# S4
[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
# S5
[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
# S6
[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
# S7
[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
# S8
[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],
]
msg= bitarray(endian='little')
msg.frombytes(b'ABCDEFGH')
perm = bitarray(endian='little')
key= bitarray(endian='little')
key.frombytes(b'FFQQSSMM')
keyPc1 = bitarray(endian='little')
keyPc2 = bitarray(endian='little')
exp = bitarray(endian='little')
for z in pc1:
keyPc1.append(key[z])
c0 = keyPc1[0:28]
d0 = keyPc1[28:]
key0 = c0 + d0
#permutation of key
for k in pc2:
keyPc2.append(key0[k])
#permutation of message
for x in iptable:
perm.append(msg[x])
l1 = perm[0:32]
r1 = perm[32:]
#Expansion of R
for y in expTable:
exp.append(r1[y])
#XORing R & key
xor_rk = keyPc2 ^ exp
#Working with S-boxes!
B = [xor_rk[0:6], xor_rk[6:12], xor_rk[12:18], xor_rk[18:24], xor_rk[24:30], xor_rk[30:36], xor_rk[36:42], xor_rk[36:]]
j = 0
Bn = [0] * 32
pos = 0
while j < 8:
# Work out the offsets
m = (B[j][0] << 1) + B[j][5]
n = (B[j][1] << 3) + (B[j][2] << 2) + (B[j][3] << 1) + B[j][4]
# Find the permutation value
v = __sbox[j][(m << 4) + n]
# Turn value into bits, add it to result: Bn
Bn[pos] = (v & 8) >> 3
Bn[pos + 1] = (v & 4) >> 2
Bn[pos + 2] = (v & 2) >> 1
Bn[pos + 3] = v & 1
pos += 4
j += 1
print (Bn)
print (l1)
xor_lf = l1 ^ Bn

The problem here is that Bn is of type list and l1 is of type bitarray.
An easy fix is to convert Bn to a bitarray just after creating it.
After,
Bn = [0] * 32
add,
Bn = bitarray(Bn)

Related

matplotlib quiver() displaying double arrows

The code below is producing double arrows. This is most noticeable in the center and along the bottom row.
Am I missing something or is this a bug of some sort? The Googlebox has yielded nothing helpful.
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22]]
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2],
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4],
[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5],
[ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7],
[ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10],
[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12],
[13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13],
[14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14],
[15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16],
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17],
[18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18],
[19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20],
[21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21],
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22],
[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23]]
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01,
6.58423221e-01, 6.34788082e-01, 5.64001424e-01,
4.29552877e-01, 2.26181450e-01, -1.45749370e-02,
-2.33836090e-01, -3.97062982e-01, -5.07288787e-01,
-5.80344621e-01, -6.28652118e-01, -6.56798746e-01,
-6.59563028e-01, -6.18178096e-01, -5.00722193e-01,
-2.93582966e-01, -5.16521582e-02, 1.45799368e-01,
2.69658133e-01, 3.26500153e-01, 3.26768709e-01],
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01,
5.43266956e-01, 4.66312191e-01, 3.24661378e-01,
1.07385088e-01, -1.54908642e-01, -3.91341641e-01,
-5.56849441e-01, -6.55288824e-01, -7.07393105e-01,
-7.30393353e-01, -7.34785105e-01, -7.24202206e-01,
-6.92703347e-01, -6.16664334e-01, -4.51489078e-01,
-1.96144234e-01, 3.89095593e-02, 1.80222779e-01,
2.38107484e-01, 2.33390450e-01, 1.75753839e-01],
[ 5.55201554e-01, 5.36916267e-01, 4.95331092e-01,
4.14297782e-01, 2.72441471e-01, 5.30267270e-02,
-2.23958353e-01, -4.85686407e-01, -6.72063827e-01,
-7.80930667e-01, -8.34052999e-01, -8.49154520e-01,
-8.36753045e-01, -8.02912499e-01, -7.48984464e-01,
-6.66861836e-01, -5.28032125e-01, -2.83307017e-01,
2.22999217e-02, 2.17035214e-01, 2.78107345e-01,
2.57213982e-01, 1.80414526e-01, 5.58040480e-02],
[ 5.30514869e-01, 4.88045325e-01, 4.08507605e-01,
2.73305715e-01, 6.32261747e-02, -2.15499555e-01,
-5.00468423e-01, -7.16424820e-01, -8.46362641e-01,
-9.11763344e-01, -9.32463829e-01, -9.17913774e-01,
-8.71699781e-01, -7.94959450e-01, -6.84618047e-01,
-5.27977847e-01, -3.00601184e-01, 3.24725025e-03,
2.84572480e-01, 4.15678719e-01, 4.08817699e-01,
3.17903989e-01, 1.68926905e-01, -2.47734503e-02],
[ 5.02548139e-01, 4.33179379e-01, 3.10308116e-01,
1.15832201e-01, -1.53873192e-01, -4.53740654e-01,
-7.02597717e-01, -8.60992569e-01, -9.43881451e-01,
-9.75369646e-01, -9.68057700e-01, -9.24950942e-01,
-8.45413757e-01, -7.27664488e-01, -5.66705523e-01,
-3.53336189e-01, -8.54415234e-02, 2.02537484e-01,
4.30399404e-01, 5.35069052e-01, 5.14397103e-01,
3.91608994e-01, 1.87450108e-01, -6.90256334e-02],
[ 4.65201575e-01, 3.63695264e-01, 1.90407737e-01,
-6.27986111e-02, -3.66107539e-01, -6.43527995e-01,
-8.36008710e-01, -9.43058071e-01, -9.89396085e-01,
-9.93329532e-01, -9.61559805e-01, -8.93929627e-01,
-7.88224158e-01, -6.41454775e-01, -4.49066265e-01,
-2.09439702e-01, 6.10838713e-02, 3.17493844e-01,
5.06540308e-01, 5.97919655e-01, 5.83094325e-01,
4.57218933e-01, 2.21129548e-01, -8.56001665e-02],
[ 4.07605489e-01, 2.67483222e-01, 4.05687053e-02,
-2.54052862e-01, -5.49121397e-01, -7.74824152e-01,
-9.12683654e-01, -9.80602627e-01, -9.99708951e-01,
-9.81982007e-01, -9.30930539e-01, -8.45440583e-01,
-7.22549396e-01, -5.57873670e-01, -3.47081454e-01,
-9.50528305e-02, 1.70316784e-01, 4.02869279e-01,
5.66564552e-01, 6.47406192e-01, 6.37165072e-01,
5.16241932e-01, 2.63738110e-01, -8.10456144e-02],
[ 3.16982526e-01, 1.36224084e-01, -1.31317283e-01,
-4.28869103e-01, -6.80165940e-01, -8.50494619e-01,
-9.46930042e-01, -9.87780174e-01, -9.87413002e-01,
-9.53497923e-01, -8.88237499e-01, -7.90275139e-01,
-6.55616984e-01, -4.77864944e-01, -2.52458286e-01,
9.81222811e-03, 2.71633461e-01, 4.87237886e-01,
6.32706314e-01, 7.04459838e-01, 6.96319353e-01,
5.81475147e-01, 3.21122831e-01, -5.36729232e-02],
[ 1.89851174e-01, -1.85511352e-02, -2.92612142e-01,
-5.56206782e-01, -7.55569048e-01, -8.83561174e-01,
-9.52641527e-01, -9.75602026e-01, -9.61454612e-01,
-9.15325308e-01, -8.38796591e-01, -7.30116952e-01,
-5.83859882e-01, -3.91508312e-01, -1.49024508e-01,
1.25224816e-01, 3.84250312e-01, 5.84434393e-01,
7.13492616e-01, 7.76382012e-01, 7.69585148e-01,
6.63495229e-01, 4.05530281e-01, 8.00655892e-03],
[ 4.93393011e-02, -1.59077638e-01, -4.07446556e-01,
-6.27159359e-01, -7.86840781e-01, -8.87988595e-01,
-9.40149757e-01, -9.51231448e-01, -9.26802697e-01,
-8.70250066e-01, -7.82391229e-01, -6.60618012e-01,
-4.97916636e-01, -2.84557428e-01, -2.01313492e-02,
2.65144709e-01, 5.15478531e-01, 6.95092413e-01,
8.05054050e-01, 8.57357453e-01, 8.51220724e-01,
7.58939184e-01, 5.22126402e-01, 1.20841224e-01],
[-6.08161695e-02, -2.48600589e-01, -4.63433277e-01,
-6.50364713e-01, -7.86735731e-01, -8.73048406e-01,
-9.15192518e-01, -9.18037801e-01, -8.84901395e-01,
-8.17468539e-01, -7.15217876e-01, -5.74265632e-01,
-3.86590763e-01, -1.44298331e-01, 1.42640838e-01,
4.27841719e-01, 6.54596037e-01, 8.04151106e-01,
8.90457558e-01, 9.29136623e-01, 9.21288722e-01,
8.45538031e-01, 6.51467861e-01, 2.89354206e-01],
[-1.11855856e-01, -2.76262916e-01, -4.66494984e-01,
-6.36195917e-01, -7.62707033e-01, -8.42791922e-01,
-8.79618811e-01, -8.76344309e-01, -8.34510258e-01,
-7.53704299e-01, -6.31437753e-01, -4.62760383e-01,
-2.41283252e-01, 3.19831190e-02, 3.28392442e-01,
5.90402526e-01, 7.76991386e-01, 8.90488023e-01,
9.51679598e-01, 9.75365138e-01, 9.63233139e-01,
9.01562554e-01, 7.57051594e-01, 4.78793525e-01],
[-1.02763752e-01, -2.49208456e-01, -4.25698141e-01,
-5.90445663e-01, -7.17037110e-01, -7.97411218e-01,
-8.32440059e-01, -8.24121526e-01, -7.72259450e-01,
-6.73888301e-01, -5.24476588e-01, -3.20200623e-01,
-6.29603397e-02, 2.27897175e-01, 5.06403936e-01,
7.23466314e-01, 8.64328407e-01, 9.44675955e-01,
9.84485357e-01, 9.95247332e-01, 9.78583745e-01,
9.26839981e-01, 8.22017325e-01, 6.32198023e-01],
[-4.76297792e-02, -1.80235400e-01, -3.48835361e-01,
-5.15498990e-01, -6.48860867e-01, -7.34607790e-01,
-7.70573611e-01, -7.57371172e-01, -6.92888223e-01,
-5.71674412e-01, -3.88941513e-01, -1.47948226e-01,
1.31928638e-01, 4.12166625e-01, 6.47847253e-01,
8.14343231e-01, 9.16397002e-01, 9.71963635e-01,
9.96490008e-01, 9.98129614e-01, 9.78410399e-01,
9.33624383e-01, 8.55392368e-01, 7.29066548e-01],
[ 3.44453327e-02, -8.45500183e-02, -2.44567678e-01,
-4.13390951e-01, -5.55986626e-01, -6.50278759e-01,
-6.88905719e-01, -6.69887784e-01, -5.89049719e-01,
-4.40034407e-01, -2.23153205e-01, 4.24368019e-02,
3.17706147e-01, 5.60575371e-01, 7.45372080e-01,
8.69074149e-01, 9.43218293e-01, 9.82237250e-01,
9.96859104e-01, 9.92775230e-01, 9.71548772e-01,
9.31926675e-01, 8.70952635e-01, 7.84147337e-01],
[ 1.24531829e-01, 2.14951720e-02, -1.24142269e-01,
-2.88285785e-01, -4.36332137e-01, -5.38600278e-01,
-5.79503157e-01, -5.52137571e-01, -4.50556205e-01,
-2.72020914e-01, -3.08257107e-02, 2.33881288e-01,
4.76396240e-01, 6.69624907e-01, 8.08474102e-01,
9.00042790e-01, 9.55065954e-01, 9.83283570e-01,
9.91629328e-01, 9.84084754e-01, 9.62320925e-01,
9.26691302e-01, 8.77415997e-01, 8.15482847e-01],
[ 2.08997618e-01, 1.24559020e-01, 8.48684407e-04,
-1.46598137e-01, -2.88661918e-01, -3.91618296e-01,
-4.29961569e-01, -3.89309376e-01, -2.63943288e-01,
-6.30136416e-02, 1.78668276e-01, 4.12249396e-01,
6.04758495e-01, 7.48714864e-01, 8.50152685e-01,
9.17783980e-01, 9.59112226e-01, 9.79804775e-01,
9.83901943e-01, 9.74114572e-01, 9.52212374e-01,
9.19706766e-01, 8.78912089e-01, 8.33903997e-01],
[ 2.82249691e-01, 2.18162184e-01, 1.22747194e-01,
5.37550368e-03, -1.12692673e-01, -1.99604825e-01,
-2.22963823e-01, -1.62177004e-01, -1.79584651e-02,
1.81399471e-01, 3.89284568e-01, 5.68595908e-01,
7.06857534e-01, 8.07907267e-01, 8.79716132e-01,
9.28814854e-01, 9.59433518e-01, 9.74181247e-01,
9.74926863e-01, 9.63345918e-01, 9.41247109e-01,
9.11068122e-01, 8.76727946e-01, 8.44239721e-01],
[ 3.46009504e-01, 3.03334542e-01, 2.40609727e-01,
1.64792336e-01, 9.10650256e-02, 4.46918062e-02,
5.47680721e-02, 1.36260477e-01, 2.74393229e-01,
4.32734135e-01, 5.78349795e-01, 6.96327399e-01,
7.86097123e-01, 8.52735284e-01, 9.01501863e-01,
9.35893510e-01, 9.57589073e-01, 9.67107013e-01,
9.64733890e-01, 9.51264330e-01, 9.28488288e-01,
8.99692809e-01, 8.70252069e-01, 8.47561147e-01],
[ 4.06955125e-01, 3.86836289e-01, 3.60008319e-01,
3.33800629e-01, 3.19793639e-01, 3.32294319e-01,
3.80910459e-01, 4.60529276e-01, 5.54085368e-01,
6.45433976e-01, 7.25560042e-01, 7.91585217e-01,
8.44113209e-01, 8.85113897e-01, 9.16480279e-01,
9.39239758e-01, 9.53365969e-01, 9.58045905e-01,
9.52421824e-01, 9.36509635e-01, 9.12084965e-01,
8.83417478e-01, 8.57475919e-01, 8.42768229e-01],
[ 4.75102487e-01, 4.79444876e-01, 4.91103049e-01,
5.17028392e-01, 5.61580652e-01, 6.19364889e-01,
6.75394533e-01, 7.20462603e-01, 7.57762648e-01,
7.92301360e-01, 8.25331968e-01, 8.55937836e-01,
8.83019438e-01, 9.05962858e-01, 9.24460078e-01,
9.38025098e-01, 9.45636106e-01, 9.45638264e-01,
9.36251692e-01, 9.16741704e-01, 8.89028586e-01,
8.58882429e-01, 8.35359426e-01, 8.27878351e-01],
[ 5.62693753e-01, 5.93592734e-01, 6.42845488e-01,
7.09881932e-01, 7.82737342e-01, 8.39735853e-01,
8.68496463e-01, 8.76464004e-01, 8.77560033e-01,
8.80145097e-01, 8.86486243e-01, 8.95793855e-01,
9.06385360e-01, 9.16670844e-01, 9.25375659e-01,
9.31289596e-01, 9.32857010e-01, 9.27783629e-01,
9.13322352e-01, 8.87929386e-01, 8.54220866e-01,
8.20792210e-01, 7.99707058e-01, 8.00713493e-01],
[ 6.75223956e-01, 7.28299137e-01, 7.98132045e-01,
8.70828514e-01, 9.28100696e-01, 9.58357163e-01,
9.63664076e-01, 9.54511170e-01, 9.41109289e-01,
9.29563623e-01, 9.22166117e-01, 9.18823138e-01,
9.18280235e-01, 9.18945445e-01, 9.19301773e-01,
9.17852346e-01, 9.12687865e-01, 9.00755102e-01,
8.77918913e-01, 8.41778780e-01, 7.97438172e-01,
7.59412209e-01, 7.43766496e-01, 7.58426057e-01],
[ 7.48028585e-01, 8.00967465e-01, 8.60639839e-01,
9.17314355e-01, 9.62374525e-01, 9.89077372e-01,
9.96239777e-01, 9.88737117e-01, 9.73767736e-01,
9.57273913e-01, 9.42647501e-01, 9.30980153e-01,
9.21800247e-01, 9.13829662e-01, 9.05516974e-01,
8.95103386e-01, 8.80063037e-01, 8.55819040e-01,
8.15680432e-01, 7.57286053e-01, 6.94667063e-01,
6.54674360e-01, 6.55242773e-01, 6.95951505e-01],
[ 5.88142228e-01, 6.56524283e-01, 7.39388262e-01,
8.26540072e-01, 9.05382649e-01, 9.63084972e-01,
9.93428420e-01, 9.99593348e-01, 9.90003898e-01,
9.72940215e-01, 9.53944608e-01, 9.35599844e-01,
9.18217361e-01, 9.00665492e-01, 8.80954002e-01,
8.56210996e-01, 8.21553193e-01, 7.67628947e-01,
6.82523479e-01, 5.74145986e-01, 4.89341942e-01,
4.69017002e-01, 5.14459717e-01, 6.05048760e-01]]
V = [[-7.64071106e-01, -7.61939824e-01, -7.56999777e-01,
fig, ax = plt.subplots(figsize=(6, 6), dpi=300);
ax.quiver(X, Y, U, V, pivot='middle');
ax.set_aspect('equal');
Output:
I've tried the above. I expect single arrows as usual.
I don't know what additional details to add to get rid of the
Didn't you ask for exactly that, repeating values 1 and 12?
#(...)
X = [[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22],
#(...)
Y = [[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1],
#(...)
U = [[ 5.91106782e-01, 6.22366562e-01, 6.49723913e-01, (...)
[ 5.77152607e-01, 5.82746773e-01, 5.76183972e-01, (...)
You even have a quadruple arrow at coordinate (1,1). Just pair X and Y, and see that X=1 Y=1 occurs 4 times! But values of U in those 4 occurences of X=1 Y=1 are all different. So those 4 arrows that starts for (1,1) are not exactly identical.
So does X=11 Y=1.
X=2 Y=1 occurs twice.
Etc.
You said "especially in the center". No, not really. It occurs on line Y=1, on column X=1, and, indeed on line Y=12 and column X=11 that both pass not far from the center.

Plotting lognormal distribution with my data, instead of randomly generalized data

I am new to Python and statistics, and I have a problem.
I have a random variable X whose values fall under a three-parameter lognormal distribution. I would like to plot the PDF of my variable.
X contains 500 samples (N=500), which are the following:
X = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 27, 27, 27, 27, 27, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 36, 37, 38, 38, 39, 39, 40, 42, 42, 43, 43, 44, 45, 47, 48, 49, 49, 50, 50, 51, 52, 52, 54, 54, 55, 58, 62, 67, 67, 73, 80]
and the mean and standard deviation are:
Mean = 15.088
Stddev = 12.445
I have been doing some research, and I think the following code could be adapted to get the lognormal curve that I need with my data, but I do not understand very much how to do it, because I do not really comprehend how this distribution works.
The code is this:
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import math
def lognorm(mu,variance):
size = 500
sigma = math.sqrt(variance)
np.random.seed(1)
gaussianData = stats.norm.rvs(loc=mu, scale=sigma, size=size)
logData = np.exp(gaussianData)
shape, loc, scale = stats.lognorm.fit(logData, floc=0)
logData.sort()
return logData, stats.lognorm.pdf(logData, shape, loc, scale)
x, y = lognorm(37, 0.8)
plt.plot(x, y)
plt.grid()
plt.show()
Any help with be much appreciated.

How do I plot a convenient lognormal pdf with my data?

I have a random variable Words per sentence which contains those values:
words_per_sentence = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 27, 27, 27, 27, 27, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 36, 37, 38, 38, 39, 39, 40, 42, 42, 43, 43, 44, 45, 47, 48, 49, 49, 50, 50, 51, 52, 52, 54, 54, 55, 58, 62, 67, 67, 73, 80]
The observation space contains 500 values (N=500), and the mean and standard deviation are:
mu = 15.088
sigma = 12.445
I want to calculate a three-parameters-lognormal PDF for my variable and plot it in a graph but I do not get the result I want. This is my failing code up to now:
import math
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
data = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 27, 27, 27, 27, 27, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 36, 37, 38, 38, 39, 39, 40, 42, 42, 43, 43, 44, 45, 47, 48, 49, 49, 50, 50, 51, 52, 52, 54, 54, 55, 58, 62, 67, 67, 73, 80]
plt.hist(data, bins=50, color='c', alpha=0.75)
xmin = min(data)
xmax = max(data)
x = np.linspace(xmin, xmax, 200)
pdf = stats.lognorm.pdf(x, s=12.445, loc=1, scale=math.exp(15.088))
plt.plot(pdf, 'y')
The problem is that I do not get the PDF printed but a straight horizontal line in at the bottom of the graph. I cannot post any picture as I have not yet enough punctuation. Please help.
The character class you have there does not allow spaces. Add a space to it:
^[A-ZÑÁÉÍÓÚÜ ]+:
(Edited to add the Ñ that it needs)
Use
^[\p{Lu}\s]+:
See regex proof.
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[\p{Lu}\s]+ any character of: Unicode uppercase letter (\p{Lu}),
whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most amount possible))
--------------------------------------------------------------------------------
: ':'</pre>
If it does not work use
^[^:\r\n]+:
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[^:\r\n]+ any character except: ':', '\r' (carriage
return), '\n' (newline) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
: ':'

"list index out of range" when using random to pick item

I'm learning python and I wrote a program to pick a random office episode to watch and 75% of the time my program works but the other 25%, it gives me the error :
list index out of range.
I've tried messing with the min max values in the line that chooses the episode
import random as r
seasons = [
[1, 2, 3, 4, 5, 5, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
]
def choose_ep():
global seasons
s = r.randint(1,10) # 1 - 10, not including 10
e = r.randint(min(seasons[s - 1]), max(seasons[s - 1]))
print("Season: " + str(s) + ", Episode: " + str(e))
while True:
choose_ep()
if input() == "exit":
break
I want it to give me a random episode from a random season. I don't want it to give me an episode that doesn't exist like "season 1, episode 14"
https://docs.python.org/3/library/random.html#random.randint
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
So 10 is in fact included in the values of variable s and hence you get your error.
Alternative solution with no index generation:
import random as r
season_number, episode_list = r.choice(list(enumerate(seasons)))
season_number += 1
episode_number = r.choice(episode_list)
print(season_number, episode_number)

swap elements of list in recursive call python

I want to make simple function swap random element in list.
but it doesn't work in recursive call.
in first recursive call, element swapping work,
but nested recursive call(or nested recursive call in first recursive call) doesn't work.
I don't know why only swap in first recursive call works.
below are result.
Thank you all.
def change(lst):
if len(lst)>4:
a, b = np.random.randint(0, len(lst)), np.random.randint(0, len(lst))
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = int(len(lst)/2)
change(lst[:mid])
change(lst[mid:])
k = list(range(0, 20))
change(k)
print(k)
`
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4, 5, 6, 7, 8, 9]
[3, 19, 2, 0, 4]
[3, 0, 2, 19, 4]
[5, 6, 7, 8, 9]
[5, 6, 8, 7, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 1]
[10, 11, 12, 13, 14]
[10, 14, 12, 13, 11]
[15, 16, 17, 18, 1]
[15, 16, 17, 18, 1]
[0, 19, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1] <= result.
The problem is that in your recursive calls:
change(lst[:mid])
change(lst[mid:])
you use a slicing operator. The slicing operator constructs a new list, so your changes are made on a new list and are not reflected on the original list (since it is a copy).
What you can do is use indices instead:
def change(lst,frm=0,to=None):
if to is None: # set the default to the end of the list
to = len(lst)
if to-frm > 4:
a, b = np.random.randint(frm,to), np.random.randint(frm,to)
print(lst)
lst[a], lst[b] = lst[b], lst[a]
print(lst)
mid = (frm+to)//2
change(lst,frm,mid)
change(lst,mid,to)
Then we obtain:
>>> k = list(range(0, 20))
>>> change(k)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 12, 6, 7, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 10, 11, 5, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
>>> print(k)
[0, 1, 4, 3, 2, 7, 6, 12, 8, 9, 5, 11, 10, 13, 14, 15, 16, 17, 18, 19]
That's because you create copies of the original list by lst[:mid], lst[mid:]. A solution is to pass to change() the same list and (separately) the range to process.

Categories