Av = [32,5,3,1,3,4,5]
A = Av[0:int(len(Av)/2)]
B = Av[int(len(Av)/2):-1]
print(A,B)
When I run this block of code, I get
[32, 5, 3] [1, 3, 4]
The last value of Av is 5. But it is not showing up on the list B..
It's because you have sliced the list till -1 which means the last element of the array and is excluded from the sliced array.
To get an array sliced till the end leave the end part of the slice code empty. Like this -
B = Av[int(len(Av)/2):]
In python when you use index slicing Av[a:b] you get the elements from position a (included) to position b (excluded). Because -1 refers to the last position, if you do Av[a:-1], the last element won't be included.
If you want to include the last element, just omit the final index -1, that is use Av[a:]. Your code should be like this:
Av = [32,5,3,1,3,4,5]
A = Av[0:int(len(Av)/2)]
B = Av[int(len(Av)/2):]
print(A,B)
Take a look at https://stackoverflow.com/a/509295/7558835 answer. It explains very clearly how does index slicing work.
When you use the slice [x:-1], it doesn't include the value at index -1 (last position), because it is non-inclusive.
Instead, use [x:], which will give a slice that includes x and all values to its right:
B = Av[int(len(Av)/2):]
Output:
>>>B
>>>[1, 3, 4, 5]
Related
I am quite new to python and programming in general so I am still trying to understand the details in practice. This is a problem I found online so I can practice nested loops more. If my question is missing anything or you do not understand my question, please let me know. I would like to get better at asking good questions as well.
list =[[1, 2], [3,4]]
m = 1
print(list)
for i in range(0, 2):
m *= 10
for j in range(0, 2):
list[i][j] *= m # This part right here.
print(list)
This is what prints on the terminal:
[[1, 2], [3, 4]]
[[10, 20], [300, 400]]
I was trying to go through this block of code step by step to make sure I understand it but this part is stumping me. I understand that the whole function of this nested for loop is to multiply the items in the 1st list with 10 and the 2nd list with 100. I also know what the *= m part is, the part that's confusing to me is the code right before that on the same line.
So far I tried to just copy this specific part in google and see if anything came up. I could not find anything that would make sense. I also tried to just run this whole line and see what printed (list[i][j] *= m)(I changed the variable to numbers obviously). That only came up with a type error... There are no type variables left in list[2]. I was trying to isolate it to see what just this part does but it apparently doesn't work like that. I guess i need to think outside the box a little more maybe.
If we take i to be 1 and j to be 1, list[i][j] would be 4. list[i] = [3,4] so what you're doing is finding index 1 of the list [3, 4]
list is a list containing nested lists.
list[0] is the list [1, 2]. list[1] is the list [3, 4].
When you use two indexes like list[i][j], it first gets the nested list list[i], then accesses the [j] element of that. So when i == 0 and j == 1, this accesses the list element containing 2.
*= m then multiplies that list element. So when i == 0 and m == 10, it multiplies the values in the first sublist by 10. Then when i == 1 and m == 100 it multiplies the values in the second sublist by 100.
list = [[1,2], [3,4]] is an array of arrays.
To get a single element, you have to subscript list twice, which is exactly what list[i][j] is.
list[0] returns [1,2], the 0-th element of the list, which is a sublist
list[0][0] returns 1, the 0-th element of the 0-th sublist
list[0][1] returns 2, the 1st element of the 0-th sublist
.
list[1] returns [3,4], the 1st element of the list, which is a sublist
list[1][0] returns 3, the 0-th element of the 1st sublist
list[1][1] returns 4, the 1st element of the 1st sublist
.
Also, the reason why list[2] doesn't return anything is because list only has two elements, which have the index 0 and 1, so trying to get the element at index 2 will not work
Let's say I've got a nested list like this:
mylist = [[],[],[]]
and I want to insert elements at the end of the second nested list:
mylist[1].insert(-1, 1)
mylist[1].insert(-1, 2)
The output i expected was:
[[], [1, 2], []]
but instead I got:
[[], [2, 1], []]
Can somebody explain this to me? I thought the index -1 always pointed to the last position of a list.
according to documentation (https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), the first argument of the insert method is the index of the element before which to insert...and -1 designates the last element of a list: so by calling insert(-1,...) the element you insert will always become the next to last element of your list.
this is easy to veryfy. if you insert yet another element
mylist[1].insert(-1, 3)
you will notice the resulting list becomes
[[], [2, 3, 1], []]
so should probably use append instead. or calculate the index dynamically like
mylist[1].insert(len(mylist[1]), 3)
If a list has for example 3 elements, they are numbered from 0 to 2 (i.e. 0, 1, 2).
If you want use negative indices, they are numbered from -3 to -1 (i.e. -3, -2, -1).
Now, if you want insert a new element at position -1, it is the same, as inserting at position 2, i.e. the inserted element will become element[2].
But element[2] will be then an element of the 4-element list, so its current position in the negative notation is not -1 but -2:
element[-4], element[-3], element[-2], element[-1]
From this page,
list.insert(index, element) means you insert element at index index. .insert(-1, value) means inserting value at the last element of the list (index=len(lst)-1). So
mylist[1].insert(1, 1)
mylist[1].insert(1, 2)
should solve the problem.
Another approach is to use append as the other one said.
mylist[1].append(1)
mylist[1].append(2)
I am trying to use:
num_new = num[i,-1]
to get the last segment of the array. However, it returns [] when i = 0, and num = [3]
Do I have to use like num[i, len(num)]?
Is there any other attention need to pay when using -1 to retrieve array elements?
I think this is not a matter of -1, but python list slicing.
some_list[-n] means nth element of list from end of list, so you will get 5 as a result in following example:
some_list = [1, 3, 5]
last_elem = some_list[-1] # 5
And this is not a core issue of your question.
List slicing in python works with this args:
some_list[_start_:_end_:_step_]
And end th element is exclusive.
So if you are trying to [3][0:-1], this excludes last element and returns empty list.
If you want to get last segment of list, you should slice like this:
some_list = [1, 2, 3, 4, 5]
sliced_list = some_list[3:] # [4, 5]
neg_1_list = some_list[3:-1] # [4]
This will help you.
This line of code
print [0, 1, 2, 3, 4][0:1:1]
returns [0].
However, the following line of code:
print [0, 1, 2, 3, 4][0:0:1]
returns [].
Why is this? Based on this Explain Python's slice notation, my understanding is that the format should be:
a[start:end:step] # start through not past end, by step
So shouldn't [0, 1, 2, 3, 4][0:0:1] start and end at the 0th value, thus returning [0]?
The "end" index of a slice is always excluded from the result; i.e., listy[start:end] returns all listy[i] where start <= i < end (note use of < instead of <=). As there is no number i such that 0 <= i < 0, listy[0:0:anything] will always be an empty list (or error).
The end index in Python's slice notation is exclusive. A slice of [n:m] will return every element whose index is >= n and < m.
To simplify things a bit, try it without the step (which isn't necessary when the step value is 1):
>>> a = [0, 1, 2, 3, 4]
>>> a[0:1]
[0]
>>> a[0:0]
[]
As a general rule, the number of elements in a slice is equal to the slice's start index minus the slice's end index. I.e., the slice [n:m] will return m-n elements. This agrees with the one element (1-0) returned by [0:1] and zero elements (0-0) returned by [0:0].
(Note that this is not true if either of the slice indices is outside of the size of the array.)
For a nice visualization of how slice indices work, search for "One way to remember how slices work" at http://docs.python.org/2/tutorial/introduction.html
Note that is [0:0:1] not [0:1:1]
So:
start = 0
end = 0
step = 1
The slice [start:end:step] means it will return values that are between start and end - 1 with a certain step, so for your example:
...[0:0:1]
Values between 0 and -1, so it doesn't return anything.
Very basic question here (I've just started with Python).
I have a list object. It contains five numbers [3,6,2,3,1]
I want find the sum of the first, third and fourth numbers in the list.
What is the syntax?
You can for instance sum elements #1, #3, and #4 with the flexible expression
sum(my_list[i] for i in (0, 2, 3))
The index of the first element is 0 [not 1], etc., i.e. my_list[0] is the first element (with value 3, in the original question), etc.
The items of a list are numbered like this:
a = [3, 6, 2, 3, 1]
^ ^ ^ ^ ^
index 0 1 2 3 4
To access the item with the index i, use a[i]. From here, you should be able to figure out how to sum the desired items.
Just write the index in brackets. Note that the index starts with zero:
lst[0] + lst[2] + lst[3]
In some cases, you can use the sum function and select a slice of the list. For example, to get the sum of the first, third, and fifth element, use:
sum(lst[::2])
You can access an element of a Python list by index by appending [list_index] to the list object (replace list_index with the index you want). For example:
my_list_object = [3,6,2,3,1]
my_sum = my_list_object[0]+my_list_object[2]+my_list_object[3]