DES implementation - python-3.x

I'm trying to programm the Data Encyption Standard on my own and I'm struggling to Programm the SBoxes. I know there is already a module to encrypt and decrypt with the DES but my teacher asked to programm it myself, so here is what i have:
import random
from re import findall
class DES:
def __init__(self):
self. Eingabe=""
self.Schluessel=""
self.NachrichtBinaer=""
self.Bitslinks=""
self.Bitsrechts=""
self.Teil1=""
self.Teil2=""
self.Subkey=""
self.pcschluessel=""
self.subkeyliste=[]
self.initialpermutation=""
self.liste1=[]
self.Bits48=""
self.ausgabesbox=[]
self.liste2=[]
def EingabeNachricht(self):
self.Eingabe=input("Geben Sie ein Wort ein:")
print ("Eingegebenes Wort: ",self.Eingabe)
def Bitumwandlung(self):
for i in range(0,len(self.Eingabe)):
self.NachrichtBinaer=self.NachrichtBinaer+bin(ord(self.Eingabe[i]))
self.NachrichtBinaer=self.NachrichtBinaer.replace("b","")
if len(self.NachrichtBinaer)<64:
self.NachrichtBinaer=self.NachrichtBinaer.rjust(64,"0")
self.NachrichtBinaer="0000000100100011010001010110011110001001101010111100110111101111"
print ("Nachricht in Binaer: ",self.NachrichtBinaer)
def Teilen(self):
self.Bitslinks=self.initialpermutation[:int(len(self.initialpermutation)/2)]
self.Bitsrechts=self.initialpermutation[int(len(self.initialpermutation)/2):]
print ("Teil links: ",self.Bitslinks)
print ("Teil rechts: ",self.Bitsrechts)
def SchluesselGenerieren(self):
#self.Schluessel= getrandbits(64)
self.Schluessel="0001001100110100010101110111100110011011101111001101111111110001"
def ippermutation(self):
ip = [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
]
for i in ip:
self.initialpermutation=self.initialpermutation+self.NachrichtBinaer[i]
print ("Erste Permutation: ",self.initialpermutation)
def Expandieren(self):
ExpandierenTabelle = [
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,
9,20,21,22,23,24,
3,24,25,26,27,28,
27,28,29,30,31,0]
for Elemente in ExpandierenTabelle:
self.Bits48 = self.Bits48 + self.Bitsrechts[Elemente]
print ("expandiert: ",self.Bits48)
def XOR(self,wert1,wert2):
antwort=wert1^wert2
return antwort
def SBox(self):
self.sbox=[
[[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]]
]
for z in range(len(self.sbox)):
for y in range(len(self.sbox[z])):
for x in range(len(self.sbox[z][y])):
ausserebits=self.liste2[x][z][0]+self.liste2[x][z][-1]
innerebits=self.liste2[x][z][1:5]
print ("innere: ",innerebits)
print ("aussere: ",ausserebits)
def pc1undteilen(self):
pc1 = [56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,19,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,2,4,27,19,11,3]
#Subkey = ""
for j in pc1:
self.Subkey = self.Subkey+self.Schluessel[j]
self.Teil1=self.Subkey[:int(len(self.Subkey)/2)]
self.Teil2=self.Subkey[int(len(self.Subkey)/2):]
print("Schluessel64 :",self.Subkey)
print("SchluesselTeil1 :",self.Teil1)
print("SchluesselTeil2 :",self.Teil2)
def rotationundpc2(self):
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]
rotation = [
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
k=0
#schluesselliste
self.Teil1=list(self.Teil1)
self.Teil2=list(self.Teil2)
while k<16:
#rotation
u=0
while u < rotation[k]:
self.Teil1.append(self.Teil1[0])
del self.Teil1[0]
self.Teil2.append(self.Teil2[0])
del self.Teil2[0]
self.Teil1="".join(self.Teil1)
self.Teil2="".join(self.Teil2)
self.subschluessel=self.Teil1+self.Teil2
print("Teil1: ",self.Teil1)
print("Teil2: ",self.Teil2)
print ("Subschluessel: ",self.subschluessel)
self.Teil1=list(self.Teil1)
self.Teil2=list(self.Teil2)
u+=1
#pc2 und erstellung der 16 subkeys
for index2 in pc2:
self.subkeyliste.append(self.subschluessel[index2])
k+=1
while len(self.subkeyliste)>0:
self.liste1.append("".join(self.subkeyliste[0:48]))
del self.subkeyliste[0:48]
print ("Subkeyliste: ",self.liste1)
def sechsbitunterteilung(self):
for l in range(0,16):
self.liste2.append(findall("......",listenachxor[l]))
print ("liste2: ",self.liste2)
#objekt der klasse DES wird erstellt
listenachxor=[]
Krypto=DES()
#Schluesselgenerieren
Krypto.SchluesselGenerieren()
Krypto.pc1undteilen()
Krypto.rotationundpc2()
Krypto.EingabeNachricht()
Krypto.Bitumwandlung()
Krypto.ippermutation()
Krypto.Teilen()
Krypto.Expandieren()
for p in range(0,16):
listenachxor.append(bin(Krypto.XOR(int(Krypto.liste1[p],2),int(Krypto.Bits48,2))))
listenachxor[p]=listenachxor[p].replace("b","")
print ("listenachxor: ",listenachxor)
Krypto.sechsbitunterteilung()
Krypto.SBox()
By the way the problem is on this part of the programm, the rest just works fine:
for z in range(len(self.sbox)):
for y in range(len(self.sbox[z])):
for x in range(len(self.sbox[z][y])):
ausserebits=self.liste2[x][z][0]+self.liste2[x][z][-1]
innerebits=self.liste2[x][z][1:5]
print ("innere: ",innerebits)
print ("aussere: ",ausserebits)

Related

realloc(): invalid next size with array lists and the "struct hack"

I'm trying to implement an arraylist in c.
typedef struct ArrayList {
size_t len;
size_t size;
int arr[1];
} ArrayList;
I have a function that reallocates it when the array gets full.
void array_list_push(ArrayList* list, int num) {
if (list->len == list->size) {
size_t size = list->size;
size *= 2;
printf("%ld\n", size); // It prints the size
list = realloc(list, size * 2 + (sizeof(ArrayList) - 1 * sizeof(int)));
list->size = size;
}
list->arr[list->len++] = num;
}
Here is the "testing" function.
#include <stdio.h>
#include "array_list.h"
int main() {
ArrayList *list = array_list_create(2);
unsigned i, j;
for(i = 0; i < 32; i++)
{
array_list_push(list, i); // side-effect: prints the size. for ""debugging""
printf("[");
for(j=0; j < array_list_get_length(list); j++)
printf("%d, ", array_list_get(list, j));
printf("]\n");
}
return 0;
}
And the output:
[0, ]
[0, 1, ]
4
[0, 1, 2, ]
[0, 1, 2, 3, ]
8
[0, 1, 2, 3, 4, ]
[0, 1, 2, 3, 4, 5, ]
[0, 1, 2, 3, 4, 5, 6, ]
[0, 1, 2, 3, 4, 5, 6, 7, ]
16
realloc(): invalid next size
Aborted (core dumped)
Why am i getting this error, and why don't the first 2 reallocations fail?
Here's array_list_create
ArrayList* array_list_create(size_t initial_size) {
ArrayList *list = malloc(sizeof(ArrayList) + sizeof(int) * (initial_size - 1));
list->len = 0;
list->size = initial_size;
return list;
}
After changing my realloc function according to Nate Eldredge's answer, i get the following output:
[0, ]
[0, 1, ]
4
[0, 1, 2, ]
[0, 1, 2, 3, ]
8
[]
[5, ]
[5, 6, ]
[5, 6, 7, ]
[5, 6, 7, 8, ]
[5, 6, 7, 8, 9, ]
[5, 6, 7, 8, 9, 10, ]
[5, 6, 7, 8, 9, 10, 11, ]
[5, 6, 7, 8, 9, 10, 11, 12, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, 539766833, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, 539766833, 539767089, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, 539766833, 539767089, 539767345, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, 539766833, 539767089, 539767345, 926495541, ]
[5, 6, 7, 8, 9, 10, 11, 12, 539768155, 924855350, 741875756, 539769120, 539766833, 539767089, 539767345, 926495541, 892418102, ]
And so on with scrambled numbers. I try to redirect it into a file with ./main > output and in the editor it shows weird ?s. Here's the output of cat output
?
[5, 6, 7, 8, 9, ]
[5, 6, 7, 8, 9, 10, ]
[5, 6, 7, 8, 9, 10, 11, ]
[5, 6, 7, 8, 9, 10, 11, 12, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, ]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, ]
[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, ]
[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, 30, ]
[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, 30, 31, ]
list = realloc(list, size * 2 + (sizeof(ArrayList) - 1 * sizeof(int)));
This calculation looks wrong. I don't know where the 2 came from but it makes no sense; it ought to be sizeof(int). I think it might be clearer to write it as
list = realloc(list, sizeof(ArrayList) + (size - 1)*sizeof(int));
The realloc function in array_list_push can move your whole list object. You deal with that internally in array_list_push, but the code in main keeps using a pointer to the old object.
Return the new pointer from array_list_push:
ArrayList* array_list_push(ArrayList* list, int num) {
if (list->len == list->size) {
size_t size = list->size;
size *= 2;
printf("%ld\n", size); // It prints the size
list = realloc(list, size * 2 + (sizeof(ArrayList) - 1 * sizeof(int)));
list->size = size;
}
list->arr[list->len++] = num;
return list;
}
and use that new pointer in main:
int main() {
ArrayList *list = array_list_create(2);
unsigned i, j;
for(i = 0; i < 32; i++)
{
list = array_list_push(list, i); // side-effect: prints the size. for ""debugging""
printf("[");
for(j=0; j < array_list_get_length(list); j++)
printf("%d, ", array_list_get(list, j));
printf("]\n");
}
return 0;
}
Tip: running code in valgrind, if available on your platform, can quickly find such problems and their causes.

Swap pair of elements along an axis

I have a 2d numpy array as such:
import numpy as np
a = np.arange(20).reshape((2,10))
# array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
I want to swap pairs of elements in each row. The desired output looks like this:
# array([[ 9, 0, 2, 1, 4, 3, 6, 5, 8, 7],
# [19, 10, 12, 11, 14, 13, 16, 15, 18, 17]])
I managed to find a solution in 1d:
a = np.arange(10)
# does the job for all pairs except the first
output = np.roll(np.flip(np.roll(a,-1).reshape((-1,2)),1).flatten(),2)
# first pair done manually
output[0] = a[-1]
output[1] = a[0]
Any ideas on a "numpy only" solution for the 2d case ?
Owing to the first pair not exactly subscribing to the usual pair swap, we can do that separately. For the rest, it would relatively straight-forward with reshaping to split axes and flip axis. Hence, it would be -
In [42]: a # 2D input array
Out[42]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
In [43]: b2 = a[:,1:-1].reshape(a.shape[0],-1,2)[...,::-1].reshape(a.shape[0],-1)
In [44]: np.hstack((a[:,[-1,0]],b2))
Out[44]:
array([[ 9, 0, 2, 1, 4, 3, 6, 5, 8, 7],
[19, 10, 12, 11, 14, 13, 16, 15, 18, 17]])
Alternatively, stack and then reshape+flip-axis -
In [50]: a1 = np.hstack((a[:,[0,-1]],a[:,1:-1]))
In [51]: a1.reshape(a.shape[0],-1,2)[...,::-1].reshape(a.shape[0],-1)
Out[51]:
array([[ 9, 0, 2, 1, 4, 3, 6, 5, 8, 7],
[19, 10, 12, 11, 14, 13, 16, 15, 18, 17]])

Description text in speech recognition

I'm trying to build my own speech recognition network. I understood how to pre-process audio. But I can't figure out the pre-processing of the text.
I have a alphabet:
alphabet = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14,'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
And I encode each letter of the sentence into a number (27 is a space):
array([list([27, 23, 8, 5, 14, 27, 8, 5, 27, 19, 16, 5, 1, 11, 19, 27, 9, 14, 27, 15, 21, 18, 27, 12, 1, 14, 7, 21, 1, 7, 5, 27, 9, 27, 3, 1, 14, 27, 9, 14, 20, 5, 18, 16, 18, 5, 20, 27, 23, 8, 1, 20, 27, 8, 5, 27, 8, 1, 19, 27, 19, 1, 9, 4, 27]),
list([27, 19, 15, 27, 14, 15, 23, 27, 9, 27, 6, 5, 1, 18, 27, 14, 15, 20, 8, 9, 14, 7, 27, 2, 5, 3, 1, 21, 19, 5, 27, 9, 20, 27, 23, 1, 19, 27, 20, 8, 15, 19, 5, 27, 15, 13, 5, 14, 19, 27, 20, 8, 1, 20, 27, 2, 18, 15, 21, 7, 8, 20, 27, 25, 15, 21, 27, 20, 15, 27, 13, 5, 27]),
list([27, 14, 9, 7, 8, 20, 27, 6, 5, 12, 12, 27, 1, 14, 4, 27, 1, 14, 27, 1, 19, 19, 15, 18, 20, 13, 5, 14, 20, 27, 15, 6, 27, 6, 9, 7, 8, 20, 9, 14, 7, 27, 13, 5, 14, 27, 1, 14, 4, 27, 13, 5, 18, 3, 8, 1, 14, 20, 19, 27, 5, 14, 20, 5, 18, 5, 4, 27, 1, 14, 4, 27, 5, 24, 9, 20, 5, 4, 27, 20, 8, 5, 27, 20, 5, 14, 20, 27]),
list([27, 9, 27, 8, 5, 1, 18, 4, 27, 1, 27, 6, 1, 9, 14, 20, 27, 13, 15, 22, 5, 13, 5, 14, 20, 27, 21, 14, 4, 5, 18, 27, 13, 25, 27, 6, 5, 5, 20, 27]),
list([27, 25, 15, 21, 27, 3, 1, 13, 5, 27, 19, 15, 27, 20, 8, 1, 20, 27, 25, 15, 21, 27, 3, 15, 21, 12, 4, 27, 12, 5, 1, 18, 14, 27, 1, 2, 15, 21, 20, 27, 25, 15, 21, 18, 27, 4, 18, 5, 1, 13, 19, 27, 19, 1, 9, 4, 27, 20, 8, 5, 27, 15, 12, 4, 27, 23, 15, 13, 1, 14, 27])],
dtype=object)
Here are 5 sentences.
I just create one network layer and try to transfer this data there in order to get a number corresponding to the letter.
model = Sequential()
model.add(Dense(27, input_shape=(20,), activation='softmax'))
model.compile(loss='mean_squared_error',optimizer='Adam', metrics=['accuracy'])
for X, y in batch(X_train, y_train, 5):
model.train_on_batch(X, y)
batch() just breaks X_train, y_train into batch.
5 is size of batch.
But when I try to start the network I get an error
Error when checking target: expected dense_25 to have shape (27,) but got array with shape (1,)
UPD:
I'm using MFCC for X
audio, sr = librosa.load(pathTrain+"\\"+str(file), mono=True, sr=None)
fileMFCC = librosa.feature.mfcc(audio)
mean_scale = np.mean(fileMFCC, axis=0)
std_scale = np.std(fileMFCC, axis=0)
fileMFCC = (fileMFCC - mean_scale[np.newaxis, :]) / std_scale[np.newaxis, :]
X is
[array([[-4.35889894, -4.35889894, -4.35455134, ..., -3.95851777,
-3.99308173, -4.05261022],
[ 0.22941573, 0.22941573, 0.31913073, ..., 1.87189324,
1.7987301 , 1.66804349],
[ 0.22941573, 0.22941573, 0.31165866, ..., -0.27962786,
-0.19009062, -0.13788484],
...,
[ 0.22941573, 0.22941573, 0.18657944, ..., 0.14699792,
0.12751924, 0.16724807],
[ 0.22941573, 0.22941573, 0.18478513, ..., 0.00674492,
-0.04570105, 0.01231168],
[ 0.22941573, 0.22941573, 0.18232521, ..., 0.2571599 ,
0.22477036, 0.09153304]])
etc.

Python 3.6 / my function changes my global variables

I am trying really hard for my function not to mess my global 'b' value because I wish to re-use that list in another similar function using sets but it seems that even not using the same name (y), they (b and y) are still bound together...
most of the print lines are for debugging only as I was not understanding what was happening.
What am I doing wrong?
import random
a = random.sample(range(1,25),8)
b = random.sample(range(1,25),11)
a.sort()
b.sort()
def list_rdup(x,y):
print('Loop remove duplicates:')
print('x:',x)
print('y:',y)
for i in x:
y.append(i)
y.sort()
print('y modified:',y)
c = []
for i in y:
if i in c:
pass
else:
c.append(i)
return c
print('a:',a)
print('b:',b)
print(list_rdup(a,b))
print('a:',a)
print('b:',b)
Output: we see a and b in their original state.. then I run the function and
print a and b again to show that b was modified in the process...
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Loop remove duplicates:
x: [1, 6, 10, 11, 12, 13, 17, 22]
y: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
y modified: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
[1, 2, 3, 6, 7, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 24]
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
The call list_rdup(a,b) simply passes the reference of a and b which are stored in x and y. So, any change in x and y will change a and b. If you do not want a and b to change make a copy by using b_copy = b.copy().
To avoid altering the elements, copy the array.
import random
a=[1, 6, 10, 11, 12, 13, 17, 22]
b=[1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
#a.sort()
#b.sort()
def list_rdup(x,y):
print('Loop remove duplicates:')
print('x:',x)
print('y:',y)
for i in x:
y.append(i)
y.sort()
print('y modified:',y)
c = []
for i in y:
if i in c:
pass
else:
c.append(i)
return c
print('a:',a)
print('b:',b)
print(list_rdup(a[:], b[:]))
print('a:',a)
print('b:',b)
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Loop remove duplicates:
x: [1, 6, 10, 11, 12, 13, 17, 22]
y: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
y modified: [1, 1, 2, 3, 6, 7, 10, 11, 12, 13, 13, 16, 17, 17, 19, 20, 21, 22, 24]
[1, 2, 3, 6, 7, 10, 11, 12, 13, 16, 17, 19, 20, 21, 22, 24]
a: [1, 6, 10, 11, 12, 13, 17, 22]
b: [1, 2, 3, 7, 13, 16, 17, 19, 20, 21, 24]
Also consider deep copying if you have non-primitive values in the array.

How to get non repeating random integers

I am trying to get numbers between 0 and 25 assigned to 26 things on a list but cannot be repeated I am assuming that you would use and if and else statement but this is what I have so far
def f():
a=[0]*26
for x in a:
b=randrange(0,26)
a[b]=randrange(0,26)
return(a)
print(f())
Make a list of numbers 0..25 and shuffle it:
>>> import random
>>> a = list(range(26))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 2
2, 23, 24, 25]
>>> random.shuffle(a)
>>> a
[11, 3, 17, 0, 20, 13, 24, 21, 4, 12, 14, 1, 22, 18, 5, 8, 6, 10, 9, 25, 23, 19,
16, 7, 2, 15]

Resources