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)
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
So I tried to do something like list.is_integer() but I don't think it's working.
Is there any method to determine if a list has for example 2 whole numbers in it?
If you know the number, simply use the in operator:
1 in [1, 2, 3]
# True
if you want to check the whole list consisting of integers, you'll need to use something different, for example:
all(isinstance(item, int) for item in [1, 2, 3])
# True
all() helps evaluating all of the values of an iterable and the condition for all items of a list being of int type can be checked by isinstance() function.
For specific count of the occurrences, simply switch all() to sum() which will add all of the truthy values that fulfill the condition:
sum(isinstance(item, int) for item in [1, 2, "3"])
# 2
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
How to select the previous number in list comprehension that starts from the second one?
The formula is [w(j-1)* 5 +w for t in y]?
w(j-1) is the first number in list.
Not exactly sure what you are trying to achieve. Therefore, my own interpretation of the problem. Assume you have a list of numbers:
my_list = range(1, 10)
Now we want to iterate over this list starting from the second entry and also access the previous entry:
new_list = [my_list[i - 1]*5 + my_list[i] for i, n in enumerate(my_list) if i > 0]
print(new_list)
This way you can get access to a list element and its predecessor.
Is this what you are trying to achieve?
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 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.
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]