iPython 3.5.2 lists [duplicate] - python

This question already has answers here:
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 6 years ago.
I have created a list size 12, ex: V[0 1 2 3 4 5 6 7 8 9 10 11]
How do I return only the last 10 digits [2:11]? Also what if the list had n variables, I tried V[2,:], but I get TypeError: list indices must be integers or slices, not tuple.

If you want to get the last x elements of a list, you need to use a negative index in your slice.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[-5:]
[5, 6, 7, 8, 9]
As for the error you mentioned, it looks like you have a stray comma after the 2, which makes 2 the first element in a tuple. Take out the comma and it should be a valid slice.

Related

Negative number in python list [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 1 year ago.
items = [10, 5, -2, 23, 5, 6,7]
For i in items:
if i <= 5:
items.remove(i)
print(items)
Does anybody know why -2 is not taken into account in for loop? It works if the condition is i < 5 but -2 is passed over when the condition is <=
The answer is quite simple. If you change the object you are working with, it will restructure automatically. What is happening is the following:
Your starting list -> [10, 5, -2, 23, 5, 6, 7]
Step 0. First iteration of the loop (checks the first element of the list) -> 10 <= 5 False
Step 1. Second iteration of the loop (checks the second item in the list) -> 5 <= 5 True
Your current list -> [10, -2, 23, 5, 6, 7]
Step 2. Third iteration of the loop (checks the third item in the list) -> 23 <= 5 False
As you can see, when you removed the number 5, the -2 became the second item in the list. However, the for loop does not know this, and it will continue to search with the next values ​​thinking that it has already checked for -2, since it is now the second value in the list.
This is why it is not recommended to change objects while iterating with them.
Try to filter the list and create a new one.
numbers= [10, 5, -2, 23, 5, 6,7]
positive_numbers = [x for x in numbers if x > 0]

How do i print a list of string and integers vertically in python [duplicate]

This question already has answers here:
String formatting: Columns in line
(4 answers)
Closed 1 year ago.
i am trying to print out a list of string and integers vertically.
map = [["SG", 8], ["MY", 8], ["PH", 8], ["ID", 8], ["TH", 8]]
the print should return:
SG 8
MY 8
PH 8
ID 8
TH 8
this is what i have :
for element in map:
print(element)
the failed output:
['SG', 8]
['MY', 8]
etc....
May note be the best solution, but try this.
for element in map:
print("{}\t{}".format(element[0],element[1]))

Remove every third element from a list [duplicate]

This question already has answers here:
How to remove/delete every n-th element from list?
(5 answers)
Closed 4 years ago.
In this code piece I print the index numbers in a list.
x = '1 2 3 4 67 8'
x = list(map(int, x.split()))
# something here with modulo??
print(x)
Output:
[1, 2, 3, 4, 67, 8]
I want to output
[1, 2, 4, 67]
So the 3th element, 6th, 9th etc...
Iterate the list and keep index value which is not multiple of 3
[value for key,value in enumerate(x,1) if key%3!=0 ]

Get ascending order of numbers in array in python [duplicate]

This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]

how to identify duplicate integers within a list, than minus each integer following the duplicate by one?

I am trying to solve an issue that I am currently running into. I want to have to have a list that is made up of only random integers. Then if i find a duplicate integer within this list i want to minus the rest of the list by one, after the second time the duplicate number appeared. Furthermore if a second pair of duplicate numbers are encountered, it should then minus the rest of the list by two, than if a third by three and etc.
But it should not affect the same duplicate number or any other duplicated number (that differs from the first) that is in the sequence.
For example
mylist = [0 1 2 3 4 5 6 2 8 5 10 11 12 1 14 15 16 17]
I want the end result to look like;
mylist = [0 1 2 3 4 5 6 2 7 5 9 10 11 1 12 13 14 15]
I have some rough code that I created to attempt this, but it will always minus the whole list including duplicated integers (the first pairs and any further pairs).
If someone can shed some light on how to deal with this problem i will be highly grateful!
Sorry forgot to add my code
a = [49, 51, 53, 56, 49, 54, 53, 48]
dupes = list()
number = 1
print (dupes)
while True:
#move integers from a to dupes (one by one)
for i in a[:]:
if i >= 2:
dupes.append(i)
a.remove(i)
if dupes in a:
a = [x - number for x in a]
print (dupes)
print(dupes)
if dupes in a:
a = [x - number for x in a]
number = number+1
break
Forgot to mention earlier, me and friend are currently working on this problem and the code i supplied is our rough outline of what is should look like and now the end result, I know that it does now work so i decided to ask for help for the issue
You need to iterate through your list and when you encounter a duplicate(can use list slicing) then decrement the next item!
List slicing - example,
>>> L=[2,4,6,8,10]
>>> L[1:5] # all elements from index 1 to 5
[4, 6, 8, 10]
>>> L[3:] # all elements from index 3 till the end of list
[8, 10]
>>> L[:2] # all elements from index beginning of list to second element
[2, 4]
>>> L[:-2] # all elements from index beginning of list to last second element
[2, 4, 6]
>>> L[::-1] # reverse the list
[10, 8, 6, 4, 2]
And enumerate
returns a tuple containing a count (from start which defaults to 0)
and the values obtained from iterating over sequence
Therefore,
mylist=[0, 1, 2, 3, 4, 5, 6, 2, 8, 5, 10, 11, 12, 1, 14, 15, 16, 17]
dup=0
for index,i in enumerate(mylist):
if i in mylist[:index]:
dup+=1
else:
mylist[index]-=dup
print mylist
Output:
[0, 1, 2, 3, 4, 5, 6, 2, 7, 5, 8, 9, 10, 1, 11, 12, 13, 14]

Categories