I have a list created from a list(input())
, that contains letter and numbers,
like ['F', 'G', 'H', '1', '5', 'H']
I don't know the contents of the list before hand.
How would I create a new list that shows the positions of strings that fit a predefined parameter so that I could receive an output like number_list = [3, 4]
and letter_list = [0, 1, 2, 5]
?
I think my answer might involve index, enumerate()
, and a for
loop but I'm not sure how to go about filtering list contents in the way I want.
I want the positional values of the strings in the list so I can use min()
and max()
functions so that the leftmost letter cant appear after the rightmost number. And later prevent the first number being used from being a 0.
You can use a simple for loop with isdigit()
:
def _collect(L):
nums, alphas = [], []
for i, val in enumerate(L):
if val.isdigit():
nums.append(i)
else:
alphas.append(i)
return nums, alphas
print(_collect(['F', 'G', 'H', '1', '5', 'H']))
Prints
([3, 4], [0, 1, 2, 5])