What does this piece of python indexing code do [duplicate] - python

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
Please explain me what does this piece of code do.
h should be 32Byte result from sha256 calculation.
I am rewriting parts of this code for my project in C++ and I'm not sure if this switches byte order per 4byte chunk or change byte order on whole 32byte number.
def reverse_hash(h):
return struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
And, how does this array index work ?
[::-1]
Thanks for any and all info

[::-1] creates a new list with reversed order of elements

[::-1] reverse order of elements in a list, so this script change order of each 4-bytes subsequence (in order to change endiannes, I suppose):
>>> h = ''.join(map(str, range(0,21)))
>>> h
'01234567891011121314151617181920'
>>> struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
'32107654019821114131615181710291'
Equivalent expression:
>>> struct.pack('<IIIIIIII', *struct.unpack('>IIIIIIII', h))
'32107654019821114131615181710291'

Related

Meaning of the second bracket [] in the print statement in python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Below is the code.
I don't understand the meaning of the last bracket in [::-1][:-1], and how come can you write two brackets at the same time. I understand that the first slice bracket reverses the order of the string, but what does the second do?
for i in range(n,0,-1):
temp = list(alphabets[:n][i-1:])
print('-'.join(temp[::-1][:-1]+temp).center(4*n-3,'-'))
Thanks for cooperation!
To answer your question I will use an example:
list = [1,2,3,4,5]
As you said, the first brackets will reverse your list.
Then, you will get as a result: [5,4,3,2,1]
On that list you will do the slicing: [:-1]. That will give you as a result [5,4,3,2].
The brackets meaning is the same:
[<startIndex>:<endIndex>:<how to go through>]
For more information about the <how to go through> part, please read here: https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

Python (int) and (int,) [duplicate]

This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 3 years ago.
Why type((1)) is int and not a tuple? Whereas type((1,)) gives tuple.
That's also an answer to the question why we should use commas while defining a tuple with one value. Because tuples are not like lists which is unique in a way that we define it (using squared brackets) we have to add the comma to the value. In the first one type((1)) inner paranthesis have no effect, so it's just a basic integer nothing else. Like when you define expressions in paranthesis to give them priority. Hope it helps :)
Python compiler treated (1) as 1 because of that it is showing as int. that is inbuilt behavior of python compiler.
>>> a = (1)
>>> print(a)
1
>>> a = (1,)
>>> print(a)
(1,)

Python min function tuples with arbitrary tuples [duplicate]

This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.

Why am I getting float is not iterable in this python snippet? [duplicate]

This question already has answers here:
How to declare and add items to an array in Python?
(8 answers)
Closed 5 years ago.
dist=[a,b,c,d,e]
spset=[1,3]
k=[]
for m in range(1,self.n+1):
if m not in spset:
k+=dist[m]
I'm trying to make a list k that contains the all elements of dist except the ones with whose indexes are in spset[]. What am I doing wrong? The error is:
k+=dist[m]
TypeError: 'int' object is not iterable
The problem might be the one stated by #SuperSaiyan. Also, here you have another possible solution more compact and simple:
[x for i,x in enumerate(dist) if i not in spset]
Because dist[m] is probably an int. You are trying to "Extend" a list through the += operation. You probably want k.append(dist[m]).

Algorithm for sorting a list in python does not work properly [duplicate]

This question already has answers here:
How to sort python list of strings of numbers
(4 answers)
Closed 8 years ago.
I am using the following function in order to sort a list in an increasing order. However, while my function works for lists such as: [1,5,6,9,3] or [56,43,16,97,45], it does not work for lists of the form: [20,10,1,3,50].
In such cases, the computer seems to consider that 3>20 and 3>10 and 3 ends up right before 50 (second to last) in the "sorted" list I get. More precisely the result I get is: [1,10,20,3,50].
Here is my code:
def function_sort(L):
for j in range(len(L)):
min=j
for i in range(j+1,len(L)):
if L[i]<L[min]:
min = i
if(min != j):
L[j],L[min] = L[min],L[j]
print L
return L
Could anyone please explain me what is going on?
It sounds like your list consists of strings rather than integers, and you end up getting the elements sorted lexicographically.
By way of illustration, consider the following:
>>> 10 < 2
False
>>> '10' < '2'
True
To fix the issue, convert the elements to integers before sorting:
L = map(int, L)
P.S. I recommend against using min as a variable name since it shadows the built-in function min().

Categories