entry and the entry before the last [closed] - python

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)

Related

With python; Getting index of wrong item of 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 9 months ago.
Improve this question
I'm trying to get the indeces of the items of my list but on one item it returns a wrong index. There are two numbers who are the same in the list, maybe it confuses it with the first one.
The list is: [0,1,0,0,0,0,0,0,0,0,6,1,0]
for i in neu:
if (i > 0):
zahl = neu.index(i) + 7
print(zahl)
browser.find_element_by_xpath("//*[#id='tabelle_merkliste']/tbody/tr['zahl']/td[10]/img")
print("found")
"print(Zahl)" returns these sums:
8
17
8 (should be 18)
Maybe someone got an idea why this happens, thanks in advance.
Use enumerate:
for i, value in enumerate(neu):
if (value > 0):
zahl = i + 7
print(zahl)
browser.find_element_by_xpath("//*[#id='tabelle_merkliste']/tbody/tr['zahl']/td[10]/img")
print("found")

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

python: list of tuples search [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
Need some help here
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
if the last 6 digits are located to return the first digit.
example if finds 10,11,29,30,36,47 return 2
You can use next similair to user's approach:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]
print(next(n for n, *nums in num if nums == to_find))
2
You can use next with a conditional generator expression:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11
next(first for first, *rest in num if search in rest)
# 2

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.

Return an arbitary element from a set that satisfies a condition [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
Is there a way to retrieve any arbitary element from a set that satisfies a particular condition?
For example, if my set has {1,2,3,4,5,6,7,8,9,10} .
Is there a way to retrieve any arbitary element that is less than 5?
You can pass random.choice a list that filters your set to the choices you want:
from random import choice
s = set([1,2,3,4,5,6,7,8,9,10])
choice([n for n in s if n < 5])
Of course, if you want all the items less than 5, it's just the list:
[n for n in s if n < 5]

Categories