max() function on list with an integrer [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list composed of integers, and I would like to do this :
freeSushi = max(sushiPrices <= sushiPrice)
sushiPrice being an integer and sushiPrices being a list.
Any idea of how I can do this ?

You can use filter before applying max
max(filter(lambda price: price <= sushiPrice, sushiPrices)

This would work, if "something" was a list of prices less than or equal to sushiPrice:
freeSushi = max(something)
So how can we create a list of prices less than sushiPrice? Comprehend?

Use a for loop like so:
for i in sushiPrices:
if i > sushiPrice:
del sushiPrices[sushiPrices.index(i)]
freeSushi = max(sushiPrices)

Related

Append the values if it is more than something [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I want to append the number in A to 5 if it is more than 5.
A = [1,2,3,4,5,6,7,8,9,10]
To something like this:
A = [1,2,3,4,5,5,5,5,5,5]
You can try using map
A = [1,2,3,4,5,6,7,8,9,10]
list(map(lambda x: x if x<5 else 5, A))
You can use the min function to take the smaller of each list item and 5:
[min(i, 5) for i in A]
Demo: https://replit.com/#blhsing/InsidiousDigitalIntegrationtesting

I am a python student, and I have a problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def thing():
_list = [1,1,5,6,8,3,4,6,10,23,5,12,67,3,25,2,6,5,4,3,2,1]
_list1 = [str(i) for i<=5 in lista]
return " ".join(_list1)
print(thing()))
I am new to this type of list managment, I am trying to put in _list1 only integers that are less then 5
so like the name of your function, it's created to calculate the sum of integers.
So basically, if your n=0 then your program will return 0 as result.
If not (else), it gonna give you a result of the calculation in return.
And here you want to print the interger sum of 451 which will give you the result of n % 10 + integer_sum(int(n / 10))

How can I find an element or his index in a list by a few characters of it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How can I find an element or his index in a list by a few characters of it?
For Example:
x = input()
list = ['alberto', 'kari', 'mino']
#now I want to find the element, which starts or contain x (for exampke x = 'albe')
#the next similir element should be found
#and then remove it
for item in list:
If 'alb' in item:
Item.pop()

Get maximum subset in multidimensional array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Given:
a=np.array([[-0.00365169, -1.96455717, 1.44163783, 0.52460176, 2.21493637],
[-1.05303533, -0.7106505, 0.47988974, 0.73436447, -0.87708389],
[-0.76841759, 0.8405524, 0.91184575, -0.70652033, 0.37646991]])
I would like to get the maximum subset (in this case, the first row):
[-0.00365169, -1.96455717, 1.44163783, 0.52460176, 2.21493637]
By using print(np.amax(a, axis=0)), I'm getting the wrong result:
[-0.00365169 0.8405524 1.44163783 0.73436447 2.21493637]
How can we get the correct maximum subset?
You can sum along columns and then find the index with the maximum value with argmax:
a[np.argmax(a.sum(axis=1))]
If you make some change:
a=np.array([[-0.00365169, -10.96455717, 1.44163783, 0.52460176, 2.21493637],
[-1.05303533, -0.7106505, 0.47988974, 0.73436447, -0.87708389],
[-0.76841759, 0.8405524, 0.91184575, -0.70652033, 0.37646991]])
The solution will be not right:
a[np.argmax(a.sum(axis=1))]
Try this:
arr = np.where(a == np.amax(a, axis=0))[0]
counts = np.unique(arr)
ind = np.argmax(counts)
print(a[arr[ind]])

How to print certain elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How do I print certain elements of a list for example:
list1=["0","0","0","0","0","0","Element1","0","0","0","0"]
Is there any simple way to print only Element1 that specifies that you should not print out anything that is equal to 0.
Use a list comprehension or (as in this example) a generator expression to filter out the "0" items, and loop through the filtered list:
for item in (x for x in list1 if x != "0"):
print(item)
This prints all items that are not "0".

Categories