Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a sequence of integers is in a list

Tags:

python

list

I want the program to continue executing until every element on the list is a string.
That is

li = [1, 2, 6, 'h', 'y', 'h', 'y', 4]

should stop when

li = [all elements type are strings]
li = [1, 2, 6, 'h', 'y', 'h', 'y', 4]

while '''there are still numbers in the list''':
    #code keeps on executing and updating li[] with string input
else:
    # output the list with no numbers

This is what I have tried, but if the first[0] and and last[7] element becomes a string, the while loop goes to the last else condition even with int type present in the list. if done sequentially, it works fine.

li = [8, 2, 6, 'h', 'y', 'h', 'y', 4]

for a in li:
    while a in li:
        if type(a) == int:
            x = int(input('Position: '))
            entry = input('Enter: ')
            li.pop(x)
            li.insert(x, entry)
            print(li) # to see what's happening
            li = li
    else:
        print('Board is full')

print(li)

But, I don't want it sequentially.
Therefore, it should continue if

li = [c, 2, 6, 'h', 'y', 'h', 'y', f]

and stop when

li = [a, b, c, 'h', 'y', 'h', 'y', d]

all strings

like image 486
Chris Josh Avatar asked Dec 09 '25 22:12

Chris Josh


1 Answers

You can use all or any in conjunction with string.isnumeric() to check this

li = ['a', 1, 2]
while any(str(ele).isnumeric() for ele in li):
    modify li ...
like image 60
geckos Avatar answered Dec 12 '25 10:12

geckos