Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list1=["a","b",1,2,3]
- list_number=False
- while list_number==True:
- try:
- float(list1[0])
- list_number=True
- except:
- list1=list1[1:]
- list_number=False
- print(list1)
- while list_number==True
- while list_number==False
- #数字が出るまで先頭の要素を消す
- list1 = ["a","b",1,2,3,"c",0.5]
- for v in list1:
- try:
- float(v)
- break #数字が出てきた時点でループ終了
- except:
- list1 = list1[1:]
- print(list1) #数字がない場合空要素[]になる
- #数字以外の要素を除外する
- def is_num(s):
- try:
- float(s)
- except ValueError:
- return False
- else:
- return True
- list2 = ["a","b",1,2,3,"c",0.5]
- list2 = [f for f in list2 if is_num(f)]
- print(list2)
Add Comment
Please, Sign In to add comment