Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. 8
  2. -1
  3. -41
  4. -71
  5. -97
  6. -124
  7. -126
  8. -117
  9. -107
  10. -78
  11. -26
  12. 10
  13. 46
  14. 63
  15. 94
  16. 100
  17. 88
  18. 87
  19. 105
  20. 109
  21. 81
  22. 39
  23. 7
  24. -12
  25.  
  26. -126
  27. 109
  28.  
  29. 1
  30. 10
  31. 11
  32. 1
  33.  
  34. a = [8, -1, -41, -71, -97, -124, -126, -117, -107, -78, -26, 10, 46, 63, 94, 100, 88, 87, 105, 109, 81, 39, 7, -12]
  35.  
  36.  
  37. def search_min(a):
  38. el_min = a[0]
  39. for elm in a[1:]:
  40. if elm < el_min:
  41. el_min = elm
  42. print("The minimum value in the list is: " + str(el_min))
  43.  
  44.  
  45. def search_max(a):
  46. el_max = a[0]
  47. for elm in a[1:]:
  48. if elm > el_max:
  49. el_max = elm
  50. print("The maximum value in the list is: " + str(el_max))
  51.  
  52. search_min(a)
  53. search_max(a)
  54.  
  55. import numpy as np
  56. import pandas as pd
  57.  
  58. fn = r'C:Temp.data.txt'
  59. s = pd.read_csv(fn, header=None, squeeze=True)
  60.  
  61. grp = s.groupby((np.sign(s).diff().fillna(0).ne(0)).cumsum())
  62.  
  63. extremums = grp.apply(lambda x: x.abs().max() * np.sign(x[x.abs().idxmax()]))
  64. sizes = grp.count()
  65.  
  66. In [103]: grp.agg([lambda x: x.abs().max() * np.sign(x[x.abs().idxmax()]), 'count'])
  67. .rename(columns={'<lambda>':'extremum'})
  68. Out[103]:
  69. extremum count
  70. 0
  71. 0 46 2
  72. 1 -316 29
  73. 2 148 31
  74. 3 -126 10
  75. 4 109 12
  76. 5 -168 33
  77. 6 41 5
  78. 7 -333 45
  79. 8 85 18
  80. 9 -123 17
  81. 10 70 14
  82. 11 -11 1
  83. 12 35 14
  84. 13 -97 18
  85. 14 190 75
  86. 15 -73 13
  87.  
  88. import numpy as np
  89. import pandas as pd
  90.  
  91. s = pd.Series([1,2,1,-4,-7,-2,-5,4,3,1,2,-1,-2,-3])
  92.  
  93. extremums = s.groupby((np.sign(s).diff().fillna(0).ne(0)).cumsum())
  94. .apply(lambda x: x.abs().max() * np.sign(x[x.abs().idxmax()]))
  95.  
  96. In [74]: print(extremums)
  97. 0 2
  98. 1 -7
  99. 2 4
  100. 3 -3
  101. dtype: int64
  102.  
  103. In [75]: print(extremums.values)
  104. [ 2 -7 4 -3]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement