Related
I'm asking a user to input a number from the keyboard. For example users input 190, How can I compare 190 that user input to the value of the dictionary with the same value? is it equal or not?
This is my code:
my_dict = {'apple': [900, 190], 'orange': [1300, 90], 'pineapple': [550, 13], 'carrot': [600, 60], 'cucumber': [900, 30], 'egg plant': [1100, 20], 'zucchini': [1300, 10], 'garlic': [300, 70]}
for key, value in my_dict:
usernumber = input('Input your number: ')
if usernumber == value:
print("Equal")
else:
print("Not equal")
For example, if users input the number 190, how can I check this value is in dictionary or not
The best way to do this is to get all values in my_dict with my_dict.values(), then flatten that into one list of all the values (see this question for a variety of ways on how to flatten a list). Once you have a list of all the values in the dictionary, you can use a simply in comparison to check if the user's input is in that list.
Here's what I did:
my_dict = {'apple': [900, 190], 'orange': [1300, 90], 'pineapple': [550, 13], 'carrot': [600, 60], 'cucumber': [900, 30], 'egg plant': [1100, 20], 'zucchini': [1300, 10], 'garlic': [300, 70]}
usernumber = int(input('Input your number: '))
if usernumber in [num for elem in my_dict.values() for num in elem]:
print("Equal")
else:
print("Not equal")
Unlike the other answer, this will only output a single line, instead of many lines.
Your code is correct, but since your values are list so instead of == you should do in check
my_dict = {'apple': [900, 190], 'orange': [1300, 90], 'pineapple': [550, 13], 'carrot': [600, 60], 'cucumber': [900, 30], 'egg plant': [1100, 20], 'zucchini': [1300, 10], 'garlic': [300, 70]}
usernumber = int(input('Input your number: ')) # Take input before and convert to int as well
if any(usernumber in (k:=val) for val in my_dict.values()):
print("equal", k.index(usernumber))
else:
print("unequal")
# output: equal 1
My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
my code
import os
import numpy as np
import sys
b=np.array([[13,14,15],
[22,23,24],
[31,32,33]])
#print(b)
d=np.array([100,200,300,400,500])
b[-1,:] = d[:b.shape[1]] # last row
b[:-1,-1] = d[b.shape[1]:]
val1 = np.hstack(b[::-1])
val2 = np.hstack([d[i:i+b.shape[1]] for i in range(b.shape[0])])
res = zip(val1, val2)
for i, j in res:
l=[i, j]
print(l)
my output
[100, 100]
[200, 200]
[300, 300]
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
I would need to remove matrices in my output that contain the same numbers. As you can see in the output below
The matrices do not always have to be the same and do not have to match the same iterations
required output
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
Find where the values are different and only concatenate those values.
>>> # using val1 and val2 from the question
>>> mask = np.where(val1!=val2)
>>> mask
(array([3, 4, 5, 6, 7, 8], dtype=int64),)
>>> np.vstack((val1[mask],val2[mask]))
array([[ 22, 23, 500, 13, 14, 400],
[200, 300, 400, 300, 400, 500]])
>>> np.vstack((val1[mask],val2[mask])).T
array([[ 22, 200],
[ 23, 300],
[500, 400],
[ 13, 300],
[ 14, 400],
[400, 500]])
>>>
It is as simple as comparing the two arrays and using the result as a boolean index:
np.stack([val1, val2], axis=1)[val1 != val2]
>>> aa = [10, 20, 30]
>>> aa[1:2] = 100, 200
[10, 100, 200, 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = [100, 200]
[10, 100, 200, 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = (100, 200)
[10, 100, 200, 30]
I'm a beginner to Python. I tried to change 20 into 100, 200, so I tried 3 ways of inserting these 2 numbers: ints, a list, and a tuple. Why is the result the same when I insert ints or a list or a tuple in aa[1:2]?
By using aa[1:2], you are modifying a list item using slice assignment. Slice assignment replaces the specified item (or items), in this case the second item in aa, with whatever new item(s) that is specified. I'll go through each type to clarify what is happening
Ints - aa[1:2] = 100, 200: this example is the most clear. We are replacing aa[1:2] with two ints that go into the list in that spot.
List/Tuple: this example of how slice assignment works -- instead of adding a list to the list, it extends the new list into the old list. The tuple works the same way. To replace the item and add a list or tuple, wrap the list or tuple in another list first:
>>> aa = [10, 20, 30]
>>> aa[1:2] = [[100, 200]]
[10, [100, 200], 30]
>>> aa = [10, 20, 30]
>>> aa[1:2] = [(100, 200)]
[10, (100, 200), 30]
I have a matrix M:
M = [[10, 1000],
[11, 200],
[15, 800],
[20, 5000],
[28, 100],
[32, 3000],
[35, 3500],
[38, 100],
[50, 5000],
[51, 100],
[55, 2000],
[58, 3000],
[66, 4000],
[90, 5000]]
And a matrix R:
[[10 20]
[32 35]
[50 66]
[90 90]]
I want to use the values in column 0 of matrix R as start value of a slice and the value in column 1 as end of a slice.
I want to calculate the sum between and including the ranges of these slices from the right column in matrix M.
Basically doing
M[0:4][:,1].sum() # Upper index +1 as I need upper bound including
M[5:7][:,1].sum() # Upper index +1 as I need upper bound including
and so on. 0 is the index of 10 and 3 is the index of 20. 5 would be the index of 32, 6 the index of 35.
I'm stuck at how to get the start/end values from matrix R into indeces by column 0 of matrix M. And then calculate the sum between the index range including upper/lower bound.
Expected output:
[[10, 20, 7000], # 7000 = 1000+200+800+5000
[32, 35, 6500], # 6500 = 3000+3500
[50, 66, 14100], # 14100 = 5000+100+2000+3000+4000
[90, 90, 5000]] # 5000 = just 5000 as upper=lower boundary
Update, I can get the indices now using searchsorted. Now I just need to use sum at column 1 of matrix M within the start and end.
start_indices = [0,5,8,13]
end_indices = [3,6,12,13]
Wondering if there is a more efficient way than applying a for loop?
EDIT: Found the answer here. Numpy sum of values in subarrays between pairs of indices
Use searchsorted to determine the correct indices and add.reduceat to perform the summation:
>>> idx = M[:, 0].searchsorted(R) + (0, 1)
>>> idx = idx.ravel()[:-1] if idx[-1, 1] == M.shape[0] else idx.ravel()
>>> result = np.add.reduceat(M[:, 1], idx)[::2]
>>> result
array([ 7000, 6500, 14100, 5000])
Details:
Since you want to include the upper boundaries but Python excludes them we have to add 1.
reduceat cannot handle len(arg0) as an index, we have to special case that
reduceat computes all stretches between consecutive boundaries, we have to discard every other one
I think it would be better to show an example of the output you are expecting. If what you want to calculate using M[0:4][:,1].sum() is the sum of 1000 + 200 + 800 + 5000. Then this code might help:
import numpy as np
M = np.matrix([[10, 1000],
[11, 200],
[15, 800],
[20, 5000],
[28, 100],
[32, 3000],
[35, 3500],
[38, 100],
[50, 5000],
[51, 100],
[55, 2000],
[58, 3000],
[66, 4000],
[90, 5000]])
print(M[0:4][:,1].sum())
I have two lists like this:
list_1 = [100,100,50,40,40,10,20]
list_2 = [5,25,50,120]
I want to take all the elements from list_2 and add it to the end of list_1, also making it a nested list. The output should be like this:
[[100,100,50,40,40,10,20,5],[100,100,50,40,40,10,20,25],[100,100,50,40,40,10,20,50],[100,100,50,40,40,10,20,5,120]]
Is there any possible way of doing this in Python3?
just create a list of lists with list_1 added to a single element list made of each element of list_2:
list_1 = [100,100,50,40,40,10,20]
list_2 = [5,25,50,120]
list_3 = [list_1+[x] for x in list_2]
print(list_3)
result:
[[100, 100, 50, 40, 40, 10, 20, 5], [100, 100, 50, 40, 40, 10, 20, 25], [100, 100, 50, 40, 40, 10, 20, 50], [100, 100, 50, 40, 40, 10, 20, 120]]