In a list of lists:
list=[['3','4','5'],['6','3','5'],['hello','goodbye','something56']]
I want to get rid of the one that has letters. My attempt is:
for i in sub_list:
if '.*[a-z] .*' in i:
continue
else:
print(i)
However, this is not working.
CodePudding user response:
Try to use str.isalpha() to check if string contains alphabet
list_of_lists = [['3','4','5'], ['6','3','5'], ['hello','goodbye','something56']]
for sub_list in list_of_lists:
if any(x.isalpha() for x in sub_list):
continue #this is what you are looking for
else:
print(sub_list)
Output
['3', '4', '5']
['6', '3', '5']
CodePudding user response:
- Using just basic Python (no libraries)
- It's Bad form to name a variable list since it hides the built-in function list.
Code
numbers = "0123456789" # list of digits
lst = [['3','4','5'],['6','3','5'],['hello','goodbye','something56'], ['2', '4', 'today']]
new_lst = []
for sublist in lst:
new_sublist = []
for item in sublist:
for c in item:
if not c in numbers:
break
else:
# no break encountered so only numbers in for c in item
continue
break # break in for c initem, so issue break in for item in sublist
else:
# no break, so all items where numbers
new_lst.append(sublist) # sublist only had numbers
print(new_lst)
# Output: [['3', '4', '5'], ['6', '3', '5']]