list comprehension - how to write previous number? [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 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?

Related

Finding index of newly added element that is numerically ordered [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
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)

entry and the entry before the last [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 2 years ago.
Improve this question
Im new to python.i have absolutly no idea how to solve this:
Extends the list with a While loop or For loop, so that the next entry is the sum of the last entry and the previous entry:
myListe = [1,2,3]
Deducing from your given values, you are starting out from
myListe = [1, 2, 3]
# ^ ^
# index -3 -1
and are supposed to add the last element and the one two before that.
This, you can achieve via:
while True:
next_value = myListe[-3] + myListe[-1]
if next_value > 1000:
break
myListe.append(next_value)
You can verify that the last value in the list is then in deed 872.
myListe[-3:]
# [406, 595, 872]
Using some knowledge about built-in utils, slices and named assignments (Python >= 3.8), you can simplify the while loop:
while (next_value := sum(myListe[-3::2])) <= 1000:
myListe.append(next_value)

Please explain the following inline python code? index = [n for n, value in enumerate(self.Variable[i]) if value == 1] [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 2 years ago.
Improve this question
I need to understand the following code.
The code is from a class method.
Code snippet
index = [n for n, value in enumerate(self.Variable[i]) if value == 1]
The above code can be rewritten as:
indices = []
for n, value in enumerate(self.BUSES[i]):
if value==1:
indices.append(n)
enumerate returns a pair of (index, value at that index) for a given list. So you are testing if value at a given index is 1, and if that is true, you add the index to indices.

Python - How to keep smallest number (converting from string to int) in list? [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 3 years ago.
Improve this question
If I have a list of elements such as:
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
I want to keep the elements with the smallest number after the second " - ". However, they must be compared with the elements which have the same first two numbers in the string.
For e.g. the strings which start with 058529-08704, the smallest number is 058529-08704-140200 and for 058529-08705, the smallest number is 058529-08705-140200
So the final list must end up with ["058529-08704-140200", "058529-08705-140200"].
What is the most pythonic way to achieve this instead of having to write multiple ifs or using string manipulation?
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
lst_3th_num = []
for item in items:
lst_3th_num.append(int(item.split('-')[2]))
result = []
for item in items:
if int(item.split('-')[2]) == min(int(s) for s in lst_3th_num):
result.append(item)
print(result)

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