furas

find indexes of all "$" elements in the list

Jul 18th, 2018
183
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×