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/
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
If I have a string of variable length and I want to return only first 3 characters of the string.
str[:3]
this works but I want to know if the string is of lesser length, suppose 2, "ab" or just "a", will this slicing work for that too?
Thanks in advance
Python will return at most :n characters:
'a'[:3] will simply return 'a'. ''[:3] returns ''.
You could have tested for yourself in less time than it would have taken to open your browser.
But yes.
This question already has answers here:
How to delete the very last character from every string in a list of strings
(5 answers)
Closed 5 years ago.
Suppose,
a = ["Power is nothing"]
How to remove last string in variable 'a' which is 'g'?
You can use list slicing:
a = a[0][:-1]
a[0] selects the first element in your list. in your case, a[0] is "Power is nothing". Then [:-1] returns a new string without the last letter.
You can check the documentation for any detail regarding list slicing in python
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I want to print all the characters in a string as a list but for each character to be printed once even if recurring. So far I have:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
symbolsx.append(i)
This prints every character, even if the character is repeated.
symbolsx = list(set(symbolsx))
First pass the list to set function to remove duplicates, then reverted that set back to list by passing it to list function.
How about:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
if i not in symbolsx:
symbolsx.append(i)
This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
Suppose I have a list [1,2,3,4,5]. Now I want to pass the elements of this list, starting from 3rd element to a method.
i.e. i want to invoke:
myfunction([3,4,5])
How can I do this in python. tried passing mylist[2], but it doesn't quite work this way it seems.
slicing.
mylist = range(1,6)
>> [1,2,3,4,5]
myfunction(mylist[2:])
>> [3,4,5]
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'