Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. list1=["a","b",1,2,3]
  2.  
  3.  
  4. list_number=False
  5. while list_number==True:
  6. try:
  7. float(list1[0])
  8. list_number=True
  9. except:
  10. list1=list1[1:]
  11. list_number=False
  12.  
  13. print(list1)
  14.  
  15. while list_number==True
  16.  
  17. while list_number==False
  18.  
  19. #数字が出るまで先頭の要素を消す
  20. list1 = ["a","b",1,2,3,"c",0.5]
  21. for v in list1:
  22. try:
  23. float(v)
  24. break #数字が出てきた時点でループ終了
  25. except:
  26. list1 = list1[1:]
  27.  
  28. print(list1) #数字がない場合空要素[]になる
  29.  
  30. #数字以外の要素を除外する
  31. def is_num(s):
  32. try:
  33. float(s)
  34. except ValueError:
  35. return False
  36. else:
  37. return True
  38.  
  39. list2 = ["a","b",1,2,3,"c",0.5]
  40. list2 = [f for f in list2 if is_num(f)]
  41. print(list2)
Add Comment
Please, Sign In to add comment