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 list values which are tuples.
list = [ (0,0,1,0,1), (0,0,1,0,1), (0,0,1,0,1) ]
How can I achieve that value
list = [0,0,3,0,3]
You could zip the elements of the tuples and map the result with sum:
result = map(sum, zip(*lst))
Don't forget to tack on a list(...) if you're running python3.
Use list comprehension with sum.
In [4]: [sum(i) for i in zip(*_list)]
Out[4]: [0, 0, 3, 0, 3]
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 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
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
I'm making a program that gets new numerical values to put into a list in numerical order and I need to know what element is before it in the list or the index before it.
How do I find the index of a newly added element in a list that is ordered numerically?
If you have a list that is sorted, you can use the list.index function to find the index of an element in the list. You then just need to get the element at that index minus one. Assuming the index isn't zero.
>>> lst = [0, 2, 5, 9]
>>> x = 3
>>> lst.append(x)
>>> lst.sort()
>>> n = lst.index(x)
>>> (n-1, lst[n-1])
(1, 2)
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 2 years ago.
Improve this question
I would like to sort a give list like shown below
input :-[0,0,1,0,2]
after sorting i want a list like this
[1,2,4,3,5]
As we can see if the list have same values it will compare according to the index.
An Another example of my question, input:[5,3,6,4,6] the output must be [3,1,4,2,5]
What is the best way to obtain the result ?? Thank you in advance.
Create a pair from [0,0,1,0,1] with index starting from 1 like [(1,0),(2,0), (3,1)...], after that sort by the seconds element ex: 0 in (1,0), then lastly take the first element.
l = [0,0,1,0,2]
[i[0] for i in sorted(enumerate(l,1), key=lambda x: x[1])]
[1, 2, 4, 3, 5]
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 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 4 years ago.
Improve this question
For example, if I have the number 14731, I found how to convert it to a list of integers and how to find the highest integer. From here, how would I split the list [1,4,7,3,1] to be [1,4,7] and [3,1]?
x = 14731
ls = [int(i) for i in str(x)]
maxIndex = ls.index(max(ls))
ls1, ls2 = ls[:maxIndex+1], ls[maxIndex+1:]
print(ls1, ls2)
# [1, 4, 7] [3, 1]
This example uses slice notation to cut the list into two halves, based on the index of the maximum value.