Minimizing the code using the enumerate function in Python - python-3.x

Down below I have a function that checks and outputs any common number within the list_1, list_2, list_3 is there a way that I could use the enumerate or any other function function that would minimize the middle part of the code.
Bit that need minimization:
for elem in l1:#loop to access l1elements
if elem in l2:#checking for element in l2
if elem in l3:#checking for element in l3
Full Code:
def intersect(l1, l2, l3) :#function
for elem in l1:#loop to access l1elements
if elem in l2:#checking for element in l2
if elem in l3:#checking for element in l3
print (element) #display element
list_1 =[27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
intersect(list_1, list_2, list_3) #calling function

You can use numpy intersect1d method to find the common values in the lists or array
def intersect(l1, l2, l3):
print(reduce(np.intersect1d, (l1, l2, l3)))
Result:
[10 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29]
Code:
import numpy as np
from functools import reduce
def intersect(l1, l2, l3):
print(reduce(np.intersect1d, (l1, l2, l3)))
list_1 = [27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
intersect(list_1, list_2, list_3) #calling functio

You should use set object instead.
set_1 = set([27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10])
set_2 = set([14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15])
set_3 = set([19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12])
set_1.intersection(set_2, set_3)

#tony selcuk - It seems that you've tried to loop 3 lists to find corresponding matching numbers? In that case, You could try this code snippet to see if it works as you want. It used the enumerate() to loop all 3 lists together and get their (index, num) as tuple to compare if there is a match. Just run it. Once it proves to work as expected, you can turn it into a function easily. This approach will find all matching numbers that appear in all three list and at the SAME position (index).
for i, j, k in zip(enumerate(list_1), enumerate(list_2), enumerate(list_3)):
#print(i, j, k)
if i == j == k:
print("number:{} order:{}".format(i[1], j[0]))

Related

In pytorch, torch.unique is returning repititions

I have this 2-D tensor:
tmp = torch.tensor([[ 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5,
5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11,
11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 17,
17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23,
23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29,
29, 29, 30, 30, 30, 31, 31, 31, 31],
[ 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5,
5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11,
11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 15, 0, 16, 16, 17,
17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23,
23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29,
29, 29, 30, 30, 30, 31, 31, 31, 31]])
So there is 0 in the 50th column of row 2. When I apply torch.unique along
dim=1, I get:
a,c = torch.unique(tmp,dim=1,return_counts=True)
a
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]])
It can be seen that the second row of the output has two 0s and the first row has two 16s. Am I doing something wrong here or this is suspicious?
It is because you specified dim=1. PyTorch is thus checking for unique pairs (which it correctly does). Like (0, 0), (1, 1), (16, 0): these are the unique pairs that it generated. In general the pair (temp[0,i], temp[1,i]) is unique for all i.
If you want all the elements to be unique, just throw away the dim: torch.unique(tmp).
If you need to maintain the two list structure, the output cannot be stored as a single tensor because their sizes might not match. You can do something like output1 = torch.unique(tmp[0]) and output2 = torch.unique(tmp[1]).

formatting the orders within a list that are greater than a digit

I use the function list_ .index(list_ ) to get the order of digits within list_ like how list_[0] = 14. I want a function to format the list_ and print the orders that are greater than 20. So the answer would be numbers = 1,2,3,4,5,7,8,10,11,13,18 within list_[] that are greater than 20.
list_ = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_ = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
for index,i in enumerate(list_):
if i >= 20:
print(index)
If you want it as a list
x = [index for index,i in enumerate(list_) if i >= 20]
print(x)
>>> [1, 2, 3, 4, 7, 8, 10, 11, 13, 18]

Formatting a list to print out common numbers

I want to write a function that goes through all 3 of the lists down below and would print out the numbers that are present on all 3. Like how the number 23 is present on all 3 lists.
list_1 =[27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
As #Heike said, you can use intersect1d
print(reduce(np.intersect1d, (list_1, list_2, list_3)))
Result:
[10 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29]
Code:
import numpy as np
from functools import reduce
list_1 = [27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
print(reduce(np.intersect1d, (list_1, list_2, list_3)))

Length of passed values is 1, index implies 260

output of predicted_classes
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 4, 4, 2, 4, 4, 4, 4, 5, 4, 4, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 7, 13, 7, 7, 8, 11, 8, 8, 8,
11, 8, 11, 11, 8, 11, 9, 9, 9, 9, 9, 9, 9, 9, 8, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8, 11, 11, 11,
11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 3, 13, 3,
3, 13, 13, 13, 14, 14, 14, 14, 14, 14, 2, 14, 14, 14, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 20, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 19, 19, 19, 19, 8, 19, 19, 19, 19, 19, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25])
output of y_true
0 0
1 0
2 0
3 0
4 0
..
255 25
256 25
257 25
258 25
259 25
Name: label, Length: 260, dtype: int64
I want to get the indices with this code, and getting this value error.
predicted_classes = model.predict_classes(X_test)
y_true = data_test.iloc[:, 0]
correct = np.nonzero(predicted_classes==y_true)[0]
incorrect = np.nonzero(predicted_classes!=y_true)[0]
trace of error
ValueError Traceback (most recent call last)
in
4 #get the indices to be plotted
5 y_true = data_test.iloc[:, 0]
----> 6 correct = np.nonzero(predicted_classes!=y_true)[0]
7 incorrect = np.nonzero(predicted_classes==y_true)[0]
in nonzero(*args, **kwargs)
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in nonzero(a)
1894
1895 """
-> 1896 return _wrapfunc(a, 'nonzero')
1897
1898
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56 bound = getattr(obj, method, None)
57 if bound is None:
---> 58 return _wrapit(obj, method, *args, **kwds)
59
60 try:
//anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
49 if not isinstance(result, mu.ndarray):
50 result = asarray(result)
---> 51 result = wrap(result)
52 return result
53
//anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __array_wrap__(self, result, context)
1916 return result
1917 d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
-> 1918 return self._constructor(result, **d).__finalize__(self)
1919
1920 # ideally we would define this to avoid the getattr checks, but
//anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
290 if len(index) != len(data):
291 raise ValueError(
--> 292 f"Length of passed values is {len(data)}, "
293 f"index implies {len(index)}."
294 )
ValueError: Length of passed values is 1, index implies 260.
Please let me know where I am going wrong.
A quick search reveals that an old version of the documentation advises to use .to_numpy().nonzero() as a replacement for Series.nonzero().

np.percentile does not seem to be giving the correct output

I have a below list.
33, 26, 24, 21, 19, 20, 18, 18, 52, 56, 27, 22, 18, 49, 22, 20, 23, 32, 20, 18
All I am trying is to find the the 25th Percentile.
I used simple numpy program to find it.
import numpy as np
arr = [33, 26, 24, 21, 19, 20, 18, 18, 52, 56, 27, 22, 18, 49, 22, 20, 23, 32, 20, 18]
np.percentile(arr,25)
Output is : 19.75
But If we count is manually or Use Excel the 25th percentile comes as 19.25.
I expect the output as 19.25 but the actual output from numpy is 19.75. Can someone please help what is wrong here?
You see, in excel there's two percentile function: PERCENTILE.EXC and PERCENTILE.INC and the difference is in "the Percentile.Inc function the value of k is is within the range 0 to 1 inclusive, and in the Percentile.Exc function, the value of k is within the range 0 to 1 exclusive." (source)
Numpy's percentile function computes the k'th percentile where k must be between 0 and 100 inclusive (docs)
Let's check that.
arr = [18, 18, 18, 18, 19, 20, 20, 20, 21, 22, 22, 23, 24, 26, 27, 32, 33, 49, 52, 56]
np.percentile(arr,25)
19.75
Hope that helps
Check your input values, and lookup what excel uses, since these are the options in numpy
t = ['linear', 'lower', 'higher', 'nearest', 'midpoint']
arr = np.array([33, 26, 24, 21, 19, 20, 18, 18, 52, 56, 27, 22, 18, 49, 22, 20, 23, 32, 20, 18])
for cnt, i in enumerate(t):
v = np.percentile(arr, 25., interpolation=i)
print("type: {} value: {}".format(i, v))
type: linear value: 19.75
type: lower value: 19
type: higher value: 20
type: nearest value: 20
type: midpoint value: 19.5

Resources