Matplotlib color assignment error? - python

I'm trying to make a colorful scatter plot based on data in an array:
plt.scatter(150, 93, c=y_pred)
Here, y_pred is:
array([ 5, 6, 8, 16, 21, 12, 12, 13, 6, 6, 17, 11, 6, 12, 12, 23, 6,
6, 15, 6, 6, 6, 6, 6, 6, 23, 22, 6, 12, 17, 6, 20, 0, 6,
6, 12, 12, 0, 6, 6, 6, 6, 6, 6, 5, 17, 6, 6, 11, 10, 13,
6, 22, 24, 23, 6, 6, 13, 6, 6, 6, 12, 9, 15, 13, 14, 6, 18,
1, 6, 9, 6, 6, 11, 6, 5, 16, 9, 23, 2, 14, 24, 9, 5, 9,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 6, 19, 6, 23, 3,
20, 10, 4, 8, 9, 6, 6, 9, 22, 23, 6, 6, 11, 6, 6, 6, 22,
24, 14, 4, 7, 12, 6, 19, 6, 12, 3, 22, 6, 11, 6, 21, 23, 4,
6, 6, 6, 4, 10, 22, 15, 6, 6, 18, 6, 14, 4, 5], dtype=int32)
This gives me an error:
ValueError: Invalid RGBA argument: 17
I don't understand why. The same solution works for others. Could you help me understand the error?

You only add one scatter point at x=150 ; y=93 but you try to assign 150 colors for this one value.
plt.scatter(150, 93)
It works if you pass in x and y that have the same shape as c:
plt.scatter(np.random.random(150), np.random.random(150), c=y_pred)

Related

Prevent IPython from displaying long lists one element per line

In Jupyter notebooks, or in IPython, long lists are displayed one element per line. How do I display them on a single line? I don't mind if the line wraps.
In the following example, I'd like the 3rd list to be shown as a "row", not as a "column".
In [1]: [list(range(n)) for n in range(10,40,10)]
Out[1]:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[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,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29]]
The output I am looking for is the following or similar:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[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, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
My goal is to make the output easier to read for humans.
I would simply use
A = [list(range(n)) for n in range(10,40,10)]
for i in A:
print(i)

Update list in python

I'm a fresher in python. I have some troubles with list.
After creating the list, i update it. it's right when I print each element of list in loop wrong , but wrong when I return list: all elements of list are same
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
for i in range(16):
array_shift.append([])
array_shift[0] = [shift(array[0],1),shift(array[1],1)]
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(array_shift[j-1][0],1)
a2 = shift(array_shift[j-1][1],1)
array_shift[j] = [a1,a2]
print (array_shift[j])
else:
a1 = shift(array_shift[j-1][0],2)
a2 = shift(array_shift[j-1][1],2)
array_shift[j] = [a1,a2]
print (array_shift[j])
return array_shift
shiftKey([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])
and this's the result expected
[[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]] [[3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 1, 2], [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2]] [[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2,
3, 4], [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4]] [[7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6]] [[9, 10, 11, 12, 13, 14, 15,
16, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3,
4, 5, 6, 7, 8]] [[11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10], [11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] [[13,
14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]] [[15, 16, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14], [15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14]] [[16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]] [[2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 1]] [[4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1, 2, 3]] [[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5]] [[8, 9, 10,
11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13,
14, 15, 16, 1, 2, 3, 4, 5, 6, 7]] [[10, 11, 12, 13, 14, 15, 16, 1, 2,
3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6,
7, 8, 9]] [[12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]] [[13, 14, 15,
16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12]]]
but it's print like below when i print list
[[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12]], [[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12], [13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
[[13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14,
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]], [[13, 14, 15, 16, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12]]]
You need to use deepcopy functionality so you pass a copy of list instead of reference of the list.
Also, you can use the following easy function for rotation:
def shift(input_list, rotate):
return input_list[rotate:] + input_list[:rotate]
I will suggest you read about defaultlist.
I corrected my code and gave an alternate solution, please try this one.
import copy
def shift(array,n):
for i in range(n):
array.append(array[0])
array.pop(0)
return array
def shiftKey(array):
array_shift = []
array_shift.append([shift(array[0],1),shift(array[1],1)])
print (array_shift[0])
for j in range(1,16):
if j == 1 or j == 8 or j == 15:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),1)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),1)
array_shift.append([a1,a2])
print (array_shift[j])
else:
a1 = shift(copy.deepcopy(array_shift[j-1][0]),2)
a2 = shift(copy.deepcopy(array_shift[j-1][1]),2)
array_shift.append([a1,a2])
print (array_shift[j])
return array_shift
shiftKey([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])

Adding each element in an array by each element in the same array in python

I need to add each element in an array by each element in the same array and then add the results to a new array. I have tried
my_list = []
for filename in [my_file]:
with open(filename) as f:
my_list += [int(i) for line in f for i in line.split()]
answer = []
for elem in my_list:
answer += [elem + elem]
print answer
When my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the program prints [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] which is incorrect. How can I fix this?
You can try this:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
answer = [i+a for i in my_list for a in my_list]
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

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.

conversion of list to bitarray in 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)

Categories