remove multiple occurrences of integers from list [duplicate] - python

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
So I have a problem where I need to remove the integers that occur more than n times from the list. This is giving me an error that remove(x) doesn't exist in the list. Help!
def answer1(data, n):
for i in range(len(data)):
item = 0
if data.count(i) > n:
item = i
for k in range(len(data)):
data.remove(item)
print(data)
data = [1, 2, 3, 1, 1, 2, 2, 3, 4 ,5]
answer1(data, 2)

Your code doesn't work properly because you're removing items from your list while iterating on it. This shortens the list, but the loop iterating on it isn't aware of that (because of its internal index counter)
I would use collections.Counter to count the occurrences (good performance, as opposed to count which scans the list at each iteration) then filter the list using a list comprehension (as a bonus, the original input isn't modified)
import collections
def answer1(data, n):
c = collections.Counter(data)
return [x for x in data if c[x]<n]
data = [1, 2, 3, 1, 1, 2, 2, 3, 4 ,5]
a = answer1(data, 2)
print(a)
result:
[4, 5]
(and the initial order is preserved)

Related

How to get index of spsecific values in list - python [duplicate]

This question already has answers here:
How to find all occurrences of an element in a list
(18 answers)
Closed 1 year ago.
If I have a list:
A = [1,1,1,0,0,1,0]
How can I return the index of any occurence of the number 1?
With this example, how to return the following:
[0, 1, 2, 5]
This is because the number 1 appears specifically in these parts of the list
You can use a simple list comprehension:
A = [1,1,1,0,0,1,0]
i = [i for i,v in enumerate(A) if v==1]
[0, 1, 2, 5]

How do I add indefinite number of list together and output a single list? [duplicate]

This question already has answers here:
What is the best way to iterate over multiple lists at once? [duplicate]
(2 answers)
Closed 4 years ago.
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list = []
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
You can use map with zip:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])

How to remove list element and return new list? [duplicate]

This question already has answers here:
List comprehension vs. lambda + filter
(17 answers)
How to remove an element from a list by index
(18 answers)
Closed 6 years ago.
I saw there are several topics on removing items from a list, including using remove(), pop(), and del. But none of these is what I am looking for, since I want to get a new list when the items are removed. For example, I want to do this:
a = [1, 2, 3, 4, 5, 6]
<fill in > # This step somehow removes the third item and get a new list b and let
b = [1, 2, 4, 5, 6]
How can I do this?
If you want to have a new list without the third element then:
b = a[:2] + a[3:]
If you want a list without value '3':
b = [n for n in a if n != 3]

Remove an item in list and get a new list? [duplicate]

This question already has answers here:
List comprehension vs. lambda + filter
(17 answers)
How to remove an element from a list by index
(18 answers)
Closed 6 years ago.
I saw there are several topics on removing items from a list, including using remove(), pop(), and del. But none of these is what I am looking for, since I want to get a new list when the items are removed. For example, I want to do this:
a = [1, 2, 3, 4, 5, 6]
<fill in > # This step somehow removes the third item and get a new list b and let
b = [1, 2, 4, 5, 6]
How can I do this?
If you want to have a new list without the third element then:
b = a[:2] + a[3:]
If you want a list without value '3':
b = [n for n in a if n != 3]

Making Lists out of a list? [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 9 years ago.
Hi I am a beginner to python and I have an exam tomorrow. I do not know how to do this question. I know that I have to use a nested for loop but, I cannot make it work syntactically. Here is the question, I apologize for any formatting errors.
(list of int, int) -> list of (list of int)
Return a list of lists of elements from 1st,
where each sublist is the next num elements from 1st.
If the length of 1st is not a multiple of num, the final sublist will have fewer than num elements.
ยป> make_rows([2. 4, 6, 8, 10, 12],3) #Function call
[[2, 4, 6], [8, 10, 12]] # expected output
do something like this:
def separate(lst, index):
new_list = [lst[i:i+index] for i in range(0, len(lst), index)]
return new_list
it will return like so:
>>> print separate([1,2,3,4,5,6],3)
[[1, 2, 3], [4, 5, 6]]
Here is an extremely verbose solution that's not very Pythonic, but shows in detail the steps one would take without List comprehensions or functional style.
The original poster mentioned for-loops, so I thought he may want an iterative approach.
def make_rows(list_of_int, num_per_row):
new_list = []
count = 0
new_inner_list = []
for n in list_of_int:
count += 1
new_inner_list.append(n)
if (count == num_per_row):
new_list.append(new_inner_list)
new_inner_list = []
count = 0
return new_list

Categories

Resources