This question already has answers here:
Slice a string in groovy
(3 answers)
Closed 6 years ago.
Given the following list:
a = [0,1,2,3,4,5]
In python I can do this:
a[2:4] which will get me [2,3]
Given that same list in groovy, is there a similar slicing mechanism I can use?
The answer is:
a[2..3]
another example would be if you wanted [1,2,3,4]:
a[1..4]
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 8 months ago.
A=["asd","jkl","qwe"]
[A[1:3],A[0:1]]
gives
[['jkl', 'qwe'], ['asd']]
I wish it just gave
['jkl', 'qwe', 'asd']
How do I accomplish this seemingly elusive task?
edit: the version of python I must work with does not allow for * symbol.
A=["asd","jkl","qwe"]
A = [*A[1:3], *A[0:1]]
This question already has answers here:
Iterating over dictionaries using 'for' loops
(15 answers)
Closed 5 years ago.
I want to be able to print out just a single character from this dictionary but I haven't been able to figure out the syntax or find it anywhere. Is there a way to do this in vanilla Python 2.7.x given the code below?
dct = {"c":[["1","1","0"],["0","0","0"]], "d":[["1","1","0"],["1","0","0"]],}
for x in dct:
print [x][0][0]
I want the output to be: 11
Any help is much appreciated!
for x in dct:
print(dct[x][0][0])
This question already has answers here:
How do I prepend to a short python list?
(7 answers)
Closed 6 years ago.
Recently I was learning python,and I want to use the function that is opposite of append() function.How can I use the function insert an element in the first position of the list.Thank you!
Prepend doesn't exist, but you can simply use the insert method:
list.insert(0, element)
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
How to say in python "from the beginning of the array" and "all the array". For example if my code in Matlab is:
images(:, n) = img(:)
What is its equivalent in python?
It is
images[:,n] = img.ravel()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making a flat list out of list of lists in Python
Flatten (an irregular) list of lists in Python
This must me very easy, but I can't seem to find a one-line/efficiƫnt solution :
I want to convert [(1,2),(3,4),(5,6)] in [1,2,3,4,5,6]