daily pastebin goal
12%
SHARE
TWEET

find indexes of all "$" elements in the list

furas Jul 18th, 2018 (edited) 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data = ['10,537.4', '$', '1,086.0', '$', '291.1', '$', '465.3', '$', '12,379.8']
  2.  
  3. result = []
  4. pos = 0
  5.  
  6. while True:
  7.     try:
  8.         pos = data.index('$', pos)
  9.     except ValueError:
  10.         break
  11.  
  12.     result.append(pos)
  13.     pos += 1 # start one place after previous element
  14.  
  15. print(result)
  16.  
  17. #----------------------------------------------------------------------
  18.  
  19. data = ['10,537.4', '$', '1,086.0', '$', '291.1', '$', '465.3', '$', '12,379.8']
  20.  
  21. result = []
  22.  
  23. for pos, value in enumerate(data):
  24.     if value == '$':
  25.         result.append(pos)
  26.  
  27. print(result)
  28.  
  29. #----------------------------------------------------------------------
  30.  
  31. data = ['10,537.4', '$', '1,086.0', '$', '291.1', '$', '465.3', '$', '12,379.8']
  32.  
  33. result = [pos for pos, value in enumerate(data) if value == '$']
  34.  
  35. print(result)
  36.  
  37. #----------------------------------------------------------------------
  38.  
  39. import numpy as np
  40.  
  41. data = ['10,537.4', '$', '1,086.0', '$', '291.1', '$', '465.3', '$', '12,379.8']
  42.  
  43. arr = np.array(data)
  44. result = np.where(arr == '$')[0]
  45. result = result.tolist()
  46.  
  47. print(result)
  48.  
  49. #----------------------------------------------------------------------
  50.  
  51. import pandas as pd
  52.  
  53. data = ['10,537.4', '$', '1,086.0', '$', '291.1', '$', '465.3', '$', '12,379.8']
  54.  
  55. df = pd.DataFrame(data)
  56. result = df[ df[0] == '$' ].index
  57. result = result.tolist()
  58.  
  59. print(result)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top