How to make denial of the list of items? - python

I have the list with some values, i want to exclude not the values from my list and replace them with 0.
That is pseudocode:
for i in range(0,img.shape[0]):
for j in range(0,img.shape[1]):
if img[i][j]!=[some values]:
img[i][j]=0

Here is one way to do it:
In [1]: l = [1, 2, 1, 10, 2, 1, 3, 10]
In [2]: exclusions = [1, 2]
In [3]: [el if el not in exclusions else 0 for el in l]
Out[3]: [0, 0, 0, 10, 0, 0, 3, 10]
Depending on the size of exclusions, the code might be more performant if you turned it into a set.

Related

Pushing items in a list in Python

Let's say there's a list of [0, 3, 14, 0] and i want to 'push' the items of that list to the right corner. So the list will become [0, 0, 3, 14]. So basically the items with value > 0 will ignore the 0s and replace its position. The number could be anything (except 0) so sorting won't do it. Couple of examples:
[0, 4, 0, 4] => [0, 0, 4, 4]
[1, 0, 2, 2] => [0, 1, 2, 2]
[13, 0, 0, 6] => [0, 0, 13, 6]
Is there an efficient solution to this? I have been racking my brain for hours and i've found nothing near a solution.
If you want an efficient solution that is not inplace, and faster than sorting, you can do this:
lst = [0,1,2,0]
helper = [0 for i in len(lst)]
counter = 0
for i in range(len(lst)-1,-1,-1):
if lst[i]:
helper[len(lst)-1-counter] = lst[i]
counter += 1
print(helper)
This is probably not the most efficient solution:
dst_lst = [n for n in lst if n <= 0] + [n for n in lst if n > 0]
This will traverse the original list twice and create two temporary and one final list.
Hope this example helps!
Here we are using Python pre-defined data type queue
>>> import queue
>>> t = queue.deque()
>>> for each in [0, 4, 0, 4]:
... if each <= 0: t.appendleft(each)
... else: t.append(each)
...
>>> t
deque([0, 0, 4, 4])

Repeat different elements of an array different amounts of times

Say I have an array with longitudes, lonPorts
lonPort =np.loadtxt('LongPorts.txt',delimiter=',')
for example:
lonPort=[0,1,2,3,...]
And I want to repeat each element a different amount of times. How do I do this? This is what I tried:
Repeat =[5, 3, 2, 3,...]
lonPort1=[]
for i in range (0,len(lenDates)):
lonPort1[sum(Repeat[0:i])]=np.tile(lonPort[i],Repeat[i])
So the result would be:
lonPort1=[0,0,0,0,0,1,1,1,2,2,3,3,3,...]
The error I get is:
list assignment index out of range
How do I get rid of the error and make my array?
Thank you!
You can use np.repeat():
np.repeat(a, [5,3,2,3])
Example:
In [3]: a = np.array([0,1,2,3])
In [4]: np.repeat(a, [5,3,2,3])
Out[4]: array([0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3])
Without relying on numpy, you can create a generator that will consume your items one by one, and repeat them the desired amount of time.
x = [0, 1, 2, 3]
repeat = [4, 3, 2, 1]
def repeat_items(x, repeat):
for item, r in zip(x, repeat):
while r > 0:
yield item
r -= 1
for value in repeat_items(x, repeat):
print(value, end=' ')
displays 0 0 0 0 1 1 1 2 2 3.
Providing a numpy-free solution for future readers that might want to use lists.
>>> lst = [0,1,2,3]
>>> repeat = [5, 3, 2, 3]
>>> [x for sub in ([x]*y for x,y in zip(lst, repeat)) for x in sub]
[0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3]
If lst contains mutable objects, be aware of the pitfalls of sequence multiplication for sequences holding mutable elements.

Count frequency of words in multiple lists from a larger vocabulary?

I know how to count frequency of elements in a list but here's a lightly different question. I have a larger set of vocabulary and a few lists that only use part of the total vocabulary. Using numbers instead of words as an example:
vocab=[1,2,3,4,5,6,7]
list1=[1,2,3,4]
list2=[2,3,4,5,6,6,7]
list3=[3,2,4,4,1]
and I want the output to keep "0"s when a word is not used:
count1=[1,1,1,1,0,0,0]
count2=[0,1,1,1,1,2,1]
count3=[1,1,1,2,0,0,0]
I guess I need to sort the words, but how do I keep the "0" records?
This can be done using the list object's inbuilt count function, within a list comprehension.
>>> vocab = [1, 2, 3, 4, 5, 6, 7]
>>> list1 = [1, 2, 3, 4]
>>> list2 = [2, 3, 4, 5, 6, 6, 7]
>>> list3 = [3, 2, 4, 4, 1]
>>> [list1.count(v) for v in vocab]
[1, 1, 1, 1, 0, 0, 0]
>>> [list2.count(v) for v in vocab]
[0, 1, 1, 1, 1, 2, 1]
>>> [list3.count(v) for v in vocab]
[1, 1, 1, 2, 0, 0, 0]
Iterate over each value in vocab, accumulating the frequencies for them.
You could also achieve this with the follwing (Python 2):
map(lambda v: list1.count(v), vocab)

Split list into smaller lists based on values?

Say I have a list: [1, 2, 3, 4, -1, 5, 6, 7, -1, 0, 0, 3, 2, 1]
I want to split this list into using values -1 or 0, 0 as a delimiter:
[[1, 2, 3, 4], [5, 6, 7], [], [3, 2, 1]]
(Note the empty 3rd list)
How would I achieve this? My current solution is just a trivial loop but I'm wondering if theres a more pythonic solution (using list comprehensions) for delimiters of any length?
Current solution as requested (messy and hacky :P)
string = '|'.join([str(x) for x in data]).replace('-1', ',').replace('0|0', ',')
buckets = [x.split('|') if y for x in string.split(',')]
It's not trivial, but you can done it like this:
my_list = [1, 2, 3, 4, -1, 5, 6, 7, -1, 0, 0, 3, 2, 1]
my_list = ','.join(map(str, my_list))
my_list = [x.split(',') for y in my_list.split('0,0,') for x in y.split(',-1,')]
my_list = [map(int, x) if x != [''] else [] for x in my_list]
Using simple for loop and check conditions
Code:
lst = [1, 2, 3, 4, -1, 5, 6, 7, -1, 0, 0, 3, 2, 1,-1,0]
output_lst = []
check = 0
sample=[]
for val in lst:
if val == -1 :
output_lst.append(sample)
check = 0
sample = []
continue
if val == 0 :
if check ==1 :
output_lst.append(sample[:-1])
check = 0
sample = []
continue
check = 1
sample.append(val)
if check ==1:
output_lst.append(sample)
print output_lst
Output:
[[1, 2, 3, 4], [5, 6, 7], [], [3, 2, 1], [0]]
Try this
use one iteration for to do it.
ar = [1, 2, 3, 4, -1, 5, 6, 7, -1, 0, 0, 3, 2, 1]
data = ', '.join(map(str, ar))
import re
data = re.split(", -1,| 0, 0,",data)
bucket = []
import ast
for y in data:
hac = ast.literal_eval("["+y+"]")
bucket.append(hac)
print bucket

Python - How to remove similar elements from a list?

For example I have a list:
L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
And I want to remove all 1's from the list, so that I would get:
L = [2, 2, 3, 6, 10, 3]
I tried iterating over the list and then deleting the element if it equals the element I want to delete (1 in this case), but turns out you can't iterate and delete stuff from a list at the same time since it messes up the counting. The best thing I've come up with is just construct a new list L2 that doesn't contain any of the 1's and then put that into L, but is there a solution that only involves mutating L?
but is there a solution that only involves mutating L?
You can rather iterate over a copy of your List - L[:], and remove element from L. That won't mess up counting.
If you really don't want to create a new list, you would have to iterate in reverse using range(len(L) - 1, -1, -1), but that won't be 'Pythonic' anymore.
>>> for x in L[:]:
... if x == 1:
... L.remove(x)
...
>>> L
[2, 2, 3, 6, 10, 3]
However, you can also use List Comprehension:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L[:] = [x for x in L if x != 1]
>>> L
[2, 2, 3, 6, 10, 3]
Using the filter built-in:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> filter(lambda x: x is not 1, L)
[2, 2, 3, 6, 10, 3]
Or you can assign it back to L:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L = filter(lambda x: x is not 1, L)
>>> L
[2, 2, 3, 6, 10, 3]
You can also wrap this concept into methods, to be able to specify a list of items to include/exclude:
def exclude(collection, exclude_list):
return filter(lambda x: x not in exclude_list, collection)
def include(collection, include_list):
return filter(lambda x: x in include_list, collection)
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L = exclude(L, [1])
>>> L
[2, 2, 3, 6, 10, 3]
If you don't want to mutate the list or generate any new copy of it. You can loop from end to begin and use index:
>>> for i in range(len(L)-1, -1, -1):
... if L[i] == 1:
... del L[i]
This is awkward to do in python without making a copy of the list...
This will do it without making a copy.
a = range(6) # Some array [0,1,2,3,4,5]
i=0
while i < len(a):
if a[i] == 4:
del a[i]
else:
i += 1
OUTPUT
>>> a
[0, 1, 2, 3, 5]

Categories

Resources