Adding counter/index to a list of lists in Python - python

I have a list of lists of strings:
listBefore = [['4', '5', '1', '1'],
['4', '6', '1', '1'],
['4', '7', '8', '1'],
['1', '2', '1', '1'],
['2', '3', '1', '1'],
['7', '8', '1', '1'],
['7', '9', '1', '1'],
['2', '4', '3', '1']]
and I would like to add a counter/index in the middle of each list so that the list looks like:
listAfter = [['4', '5', '1', '1', '1'],
['4', '6', '2', '1', '1'],
['4', '7', '3', '8', '1'],
['1', '2', '4', '1', '1'],
['2', '3', '5', '1', '1'],
['7', '8', '6', '1', '1'],
['7', '9', '7', '1', '1'],
['2', '4', '8', '3', '1']]
What would be the easiest way to do so? I could probably loop over the lists and add the index, but is there a cleaner way to do so?
Cheers,
Kate
Edit:
The code I wrote works for me:
item = 1
for list in listBefore:
list.insert(2,str(item))
item = item + 1
print listBefore
I was wondering if there is another way to do so more efficiently or in one step.

Iterate over the parent list with enumerate() to get counter (used as i in the below example) along with list element. In the sublist, insert the i at the middle of sublist using list.insert(index, value) method.
Note: The value of counter i.e. i will be of int type, so you have to explicitly type cast it to str as str(i)before inserting. Below is the sample code:
for i, sub_list in enumerate(my_list, 1): # Here my_list is the list mentioned in question as 'listBefore'
sub_list.insert(len(sub_list)/2, str(i))
# Value of 'my_list'
# [['4', '5', '1', '1', '1'],
# ['4', '6', '2', '1', '1'],
# ['4', '7', '3', '8', '1'],
# ['1', '2', '4', '1', '1'],
# ['2', '3', '5', '1', '1'],
# ['7', '8', '6', '1', '1'],
# ['7', '9', '7', '1', '1'],
# ['2', '4', '8', '3', '1']]

You can perform this with a list comprehension
listAfter = [listBefore[i][:len(listBefore[i])/2] + [str(i+1)] + listBefore[i][len(listBefore[i])/2:] for i in range(len(listBefore))]

You should learn about enumerate, it lets you iterate over the list with two iterators - one (str_list in this case) holds the current item in the list, and the other (i`) holds it's index in the list.
for i,str_list in enumerate(listBefore):
listBefore[i] = str_list[:len(str_list)//2] + [str(i+1)] + str_list[len(str_list)//2:]

>>> data = [['4', '5', '1', '1'],
['4', '6', '1', '1'],
['4', '7', '8', '1'],
['1', '2', '1', '1'],
['2', '3', '1', '1'],
['7', '8', '1', '1'],
['7', '9', '1', '1'],
['2', '4', '3', '1']]
>>> print [row[:len(row)//2] + [str(i)] + row[len(row)//2:]
for i, row in enumerate(data, start=1)]
[['4', '5', '1', '1', '1'],
['4', '6', '2', '1', '1'],
['4', '7', '3', '8', '1'],
['1', '2', '4', '1', '1'],
['2', '3', '5', '1', '1'],
['7', '8', '6', '1', '1'],
['7', '9', '7', '1', '1'],
['2', '4', '8', '3', '1']]

Related

Converting the values of the dictionary in to a file in Python

I have a dictionary say : my_map_dict which looks something like below:
{2: [['1', '2', '4', '4', '0', '2', '0', '0.67'], ['5', '6', '3', '8', '0', '2', '1', '0.67'], ['6', '9', '4', '9', '0', '2', '2', '0.67'], ['4', '3', '6', '9', '0', '2', '3', '1.00']], 3: [['4', '6', '6', '1', '0', '3', '0', '0.67'], ['5', '9', '4', '8', '0', '3', '1', '0.67']], 4: [['1', '9', '4', '9', '0', '4', '0', '0.67']]}
Where 2,3,4 are the keys of the dictionary and those lists are the values of that.
Now I want to generate a file from this say outputfile.txt which will take all the values of the dictionary and make that a CSV file which looks like below (desired output):
1,2,4,4,0,2,0,0.67
5,6,3,8,0,2,1,0.67
6,9,4,9,0,2,2,0.67
4,3,6,9,0,2,3,1.00
4,6,6,1,0,3,0,0.67
5,9,4,8,0,3,1,0.67
1,9,4,9,0,4,0,0.67
How can I do it?
Flatten the values of your dictionary to a single list of lists, and use the csv module to write to file:
import csv
with open("output.csv","w",newline="") as f:
writer = csv.writer(f)
writer.writerows([item for sublist in d.values() for item in sublist])
output.csv
1,2,4,4,0,2,0,0.67
5,6,3,8,0,2,1,0.67
6,9,4,9,0,2,2,0.67
4,3,6,9,0,2,3,1.00
4,6,6,1,0,3,0,0.67
5,9,4,8,0,3,1,0.67
1,9,4,9,0,4,0,0.67

How to create new lists in python

I want to create new lists from one list. This the example list I am working on:
matrixlist = [['Matrix', '1'], ['1', '4', '6'], ['5', '2', '9'], ['Matrix', '2'], ['2', '6'], ['1', '3'], ['8', '6'], ['Matrix', '3'], ['5', '6', '7', '9'], ['1', '4', '2', '3'], ['8', '7', '3', '5'], ['9', '4', '5', '3'], ['Matrix', '4'], ['7', '8'], ['4', '6'], ['2', '3']]
I split them like this with for loop:
matrix1 = [['1', '4', '6'], ['5', '2', '9']]
matrix2 = [['2', '6'], ['1', '3'], ['8', '6']]
matrix3 = [['5', '6', '7', '9'], ['1', '4', '2', '3'], ['8', '7', '3', '5'], ['9', '4', '5', '3']]
matrix4 = [['7', '8'], ['4', '6'], ['2', '3']]
But I want to give the long list to program and it create lists and append the relevant elements in it. Like matrix 1 elements in matrix1 list.
Edit: I can't use any advanced built-in function. I can only use simple ones (like append, pop, reverse, range) and my functions in code.
You can use itertools.groupby:
from itertools import groupby
matrixlist = [['Matrix', '1'], ['1', '4', '6'], ['5', '2', '9'], ['Matrix', '2'], ['2', '6'], ['1', '3'], ['8', '6'], ['Matrix', '3'], ['5', '6', '7', '9'], ['1', '4', '2', '3'], ['8', '7', '3', '5'], ['9', '4', '5', '3'], ['Matrix', '4'], ['7', '8'], ['4', '6'], ['2', '3']]
result = [list(b) for a, b in groupby(matrixlist, key=lambda x:x[0] == 'Matrix') if not a]
Output:
[[['1', '4', '6'], ['5', '2', '9']],
[['2', '6'], ['1', '3'], ['8', '6']],
[['5', '6', '7', '9'], ['1', '4', '2', '3'], ['8', '7', '3', '5'], ['9', '4', '5', '3']],
[['7', '8'], ['4', '6'], ['2', '3']]]
you can do it using list comprehension like below
indx = [i for i, mat in enumerate(matrixlist )if mat[0]=='Matrix']
matrixes = {matrixlist[i][1]: matrixlist[i+1: j] for i, j in zip(indx, indx[1:])}
# access matrix with its id
matrixes["1"]

Python - Cryptographic Feistel function (AUT64) - Math/programming trouble in implementation

Don't know how to add math symbols, hence the image paste:
Based on this:
https://tches.iacr.org/index.php/TCHES/article/view/874/826 (page 52)
I managed, I think to implement compression function (1)
However, don't understand what 2,3 and 4 mean.
Can somebody with math/programming knowledge explain it? Ideally, how to implement it in Python.
Here is my code snippet:
aut64.py
import ctypes
def hexTobinary(hexdigits):
binarydigits = ""
for hexdigit in hexdigits:
binarydigits += bin(int(hexdigit, 16))[2:].zfill(4)
return binarydigits
t_offset = [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
['0', '2', '4', '6', '8', 'A', 'C', 'E', '3', '1', '7', '5', 'B', '9', 'F', 'D'],
['0', '3', '6', '5', 'C', 'F', 'A', '9', 'B', '8', 'D', 'E', '7', '4', '1', '2'],
['0', '4', '8', 'C', '3', '7', 'B', 'F', '6', '2', 'E', 'A', '5', '1', 'D', '9'],
['0', '5', 'A', 'F', '7', '2', 'D', '8', 'E', 'B', '4', '1', '9', 'C', '3', '6'],
['0', '6', 'C', 'A', 'B', 'D', '7', '1', '5', '3', '9', 'F', 'E', '8', '2', '4'],
['0', '7', 'E', '9', 'F', '8', '1', '6', 'D', 'A', '3', '4', '2', '5', 'C', 'B'],
['0', '8', '3', 'B', '6', 'E', '5', 'D', 'C', '4', 'F', '7', 'A', '2', '9', '1'],
['0', '9', '1', '8', '2', 'B', '3', 'A', '4', 'D', '5', 'C', '6', 'F', '7', 'E'],
['0', 'A', '7', 'D', 'E', '4', '9', '3', 'F', '5', '8', '2', '1', 'B', '6', 'C'],
['0', 'B', '5', 'E', 'A', '1', 'F', '4', '7', 'C', '2', '9', 'D', '6', '8', '3'],
['0', 'C', 'B', '7', '5', '9', 'E', '2', 'A', '6', '1', 'D', 'F', '3', '4', '8'],
['0', 'D', '9', '4', '1', 'C', '8', '5', '2', 'F', 'B', '6', '3', 'E', 'A', '7'],
['0', 'E', 'F', '1', 'D', '3', '2', 'C', '9', '7', '6', '8', '4', 'A', 'B', '5'],
['0', 'F', 'D', '2', '9', '6', '4', 'B', '1', 'E', 'C', '3', '8', '7', '5', 'A']]
tu = ['1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2']
tl = ['4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6']
w = 0xFFFFFF00
val = ctypes.c_uint32(~w).value
kg = [val]
kq = ['1', '2', '3', '0', '0', '0', '0', '0']
kt = ['1', '2', '3', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0']
AUT64_key = []
AUT64_key.extend(kg)
AUT64_key.extend(kq)
AUT64_key.extend(kt)
def apply_permutation_r(P_TABLE, PLAINTEXT):
permutated_M = ""
for index in P_TABLE:
permutated_M += PLAINTEXT[int(index) - 1]
return permutated_M
def apply_compression_g(o1, r_no, lk, uk):
ln = int(o1[0:4], 2)
un = int(o1[4:8], 2)
gl = int(t_offset[int(lk)][int(ln)], 16)
gu = int(t_offset[int(uk)][int(un)], 16)
g = gl >> 4 | gu
return g
def uk_round_key(i, r_no):
uk = int(tu[(r_no * 8) + i])
return uk
def lk_round_key(i, r_no):
lk = int(tl[(r_no * 8) + i])
return lk
def apply_fiestel_f(o1, r_no, i):
uk = uk_round_key(i, r_no)
lk = lk_round_key(i, r_no)
o2 = apply_compression_g(o1, r_no, lk, uk)
# o3 = apply_substitution_s(o2)
# o4 = apply_permutation_bitwise(o3)
# o5 = apply_permutation_substitution_s(o4)
# return o5
return o2
M = '01234567'
plaintext = hexTobinary(M)
enc = []
for r_no in range(8):
o1 = apply_permutation_r(kq, plaintext)
for i in range(8):
enc.append(apply_fiestel_f(o1, r_no, i))
print enc
Any help is greatly appreciate. I am a beginner, this above could be all wrong :)
Thanks in advance!

Variables scope for nested python for-loop [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 6 years ago.
I have two nested for loops and two lists, I want one of tle list to re-initialize after one iteration of inner loop.
def test():
i = ['1','5','9','3','6','4']
for x in xrange(0,len(i)):
j = ['6', '7', '9', '3']
newi = i
for y in xrange(0,len(j)):
newi[x] = j[y]
print "i", i
print "end of one iteration on finner loop"
print "newi", newi
test()
Its a dummy code, I want a clean new instance of newi to be that of i after one iteration of inner loop, currently it preserver the value of inner loop
current output:
i ['6', '5', '9', '3', '6', '4']
i ['7', '5', '9', '3', '6', '4']
i ['9', '5', '9', '3', '6', '4']
i ['3', '5', '9', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '5', '9', '3', '6', '4']
i ['3', '6', '9', '3', '6', '4']
i ['3', '7', '9', '3', '6', '4']
i ['3', '9', '9', '3', '6', '4']
i ['3', '3', '9', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '9', '3', '6', '4']
i ['3', '3', '6', '3', '6', '4']
i ['3', '3', '7', '3', '6', '4']
i ['3', '3', '9', '3', '6', '4']
i ['3', '3', '3', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '6', '6', '4']
i ['3', '3', '3', '7', '6', '4']
i ['3', '3', '3', '9', '6', '4']
i ['3', '3', '3', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '3', '7', '4']
i ['3', '3', '3', '3', '9', '4']
i ['3', '3', '3', '3', '3', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '3', '4']
i ['3', '3', '3', '3', '3', '6']
i ['3', '3', '3', '3', '3', '7']
i ['3', '3', '3', '3', '3', '9']
i ['3', '3', '3', '3', '3', '3']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '3', '3']
Instead of:
newi = i
replace with:
newi = list(i) # list(i) creates another copy i

Swap indexes using slices?

I know that you can swap 2 single indexes in Python
r = ['1', '2', '3', '4', '5', '6', '7', '8']
r[2], r[4] = r[4], r[2]
output:
['1', '2', '5', '4', '3', '6', '7', '8']
But why can't you swap 2 slices of indexes in python?
r = ['1', '2', '3', '4', '5', '6', '7', '8']
I want to swap the numbers 3 + 4 with 5 + 6 + 7 in r:
r[2:4], r[4:7] = r[4:7], r[2:4]
output:
['1', '2', '5', '6', '3', '4', '7', '8']
expected output:
['1', '2', '5', '6', '7', '3', '4', '8']
What did I wrong?
output:
The slicing is working as it should. You are replacing slices of different lengths. r[2:4] is two items, and r[4:7] is three items.
>>> r = ['1', '2', '3', '4', '5', '6', '7', '8']
>>> r[2:4]
['3', '4']
>>> r[4:7]
['5', '6', '7']
So when ['3', '4'] is replaced, it can only fit ['5', '6'], and when ['5', '6', '7'] is replaced, it only gets ['3', '4']. So you have ['1', '2',, then the next two elements are the first two elements from ['5', '6', '7'] which is just ['5', '6', then the two elements from ['3', '4' go next, then the remaining '7', '8'].
If you want to replace the slices, you have to start slices at the right places and allocate an appropriate size in the array for each slice:
>>> r = ['1', '2', '3', '4', '5', '6', '7', '8']
>>> r[2:5], r[5:7] = r[4:7], r[2:4]
>>> r
['1', '2', '5', '6', '7', '3', '4', '8']
old index: 4 5 6 2 3
new index: 2 3 4 5 6
Think of this:
r[2:4], r[4:7] = r[4:7], r[2:4]
as similar to this:
original_r = list(r)
r[2:4] = original_r[4:7]
r[4:7] = original_r[2:4]
So, by the time it gets to the third line of that, the 4th element isn't what you think it is anymore... You replaced '3', '4' with '5', '6', '7', and now the [4:7] slice starts with that '7'.
>>> r = ['1', '2', '3', '4', '5', '6', '7', '8']
>>> r[2:5], r[5:7] = r[4:7], r[2:4]
>>> r
['1', '2', '5', '6', '7', '3', '4', '8']
In your code:
>>> r[2:4], r[4:7] = r[4:7], r[2:4]
You are assigning r[4:7] which have 3 elements to r[2:4] which have only 2.
In the code I posted:
>>> >>> r[2:5], r[5:7] = r[4:7], r[2:4]
r[4:7] which is ['5', '6', '7'], replaces
r[2:5] which is ['3', '4', '5']
r resulting in ['1', '2', '5', '6', '7', '6', '7', '8']
and then:
r[2:4] which was ['3', '4'], replaces
r[5:7] which is ['6', '7']
So final result being:
['1', '2', '5', '6', '7', '3', '4', '8']

Categories

Resources