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
Related
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))
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 7 months ago.
Improve this question
Say I have
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
and I want to cast all of those numbers into just ints to have
A = [[1,2,1],[2,1,3]]
How do we do that in python?
Try list comprehension*2:
print([[int(x) for x in i] for i in A])
Or list comprehension + map:
print([list(map(int,i)) for i in A])
Or map+map:
print(list(map(lambda x: list(map(int,x)),A)))
Simple ways all return:
[[1,2,1],[2,1,3]]
Here's an approach using a list comprehension and map:
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
print([list(map(int, i)) for i in A])
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)
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 8 years ago.
Improve this question
How would I create a script it in python late the values from the recursive sequence:
$x_1 = \sqrt{2}$, $x_{n+1} = \sqrt{2x_n}$ http://www.sciweavers.org/upload/Tex2Img_1392861864/render.png
X = [sqrt(2)]
for i in range(1,10):
X.append(sqrt(2*X[i-1]))
Here is a slow solution. Assuming n is >=1
import math
def recursive(n):
if n = 1:
math.sqrt(2)
return math.sqrt(2*recursive(n-1))
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 8 years ago.
Improve this question
My code is :
L=['my', 'my']
and I want to split the items of this list so that the output becomes:
['my'],['my']
each item in a new list
Something like this, use a list comprehension:
In [109]: L=['my', 'my']
In [110]: [[x] for x in L]
Out[110]: [['my'], ['my']]
or may you wanted this:
In [129]: print ",".join(str(x) for x in [[x] for x in L] )
['my'],['my']
In [130]: print ",".join("[{0}]".format(x) for x in L)
[my],[my]