A
A
Alexander Pikeev2020-08-08 12:45:54
Python
Alexander Pikeev, 2020-08-08 12:45:54

Is it possible to check the next element in a loop?

How can this be implemented?

for i in info:
        if i.isdigit():
            if i.isdigit() and # Нужно проверить, является ли следующий элемент тоже числом
                digits += i
        else:
            letters += i

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2020-08-08
@anerev

Like so

for t, i in enumerate(info):
        if i.isdigit():
            if i.isdigit() and info[t + 1].isdigit()# Нужно проверить, является ли следующий элемент тоже числом
                digits += i
        else:
            letters += i

D
Dr. Bacon, 2020-08-08
@bacon

1. iterate over 2 elements, google about zip and slices
2. rewrite the algorithm to check not the next, but the previous one, which is stored in a separate variable
3. solve the problem in a different way, for example, using regex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question