Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 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 = grpgrp.apply(lambda x: x.abs().max() * np.sign(x[x.abs().idxmax()]))
  64. sizes = grp.count()
  65.  
  66. In [99]: extremums
  67. Out[99]:
  68. 0
  69. 0 46
  70. 1 -316
  71. 2 148
  72. 3 -126
  73. 4 109
  74. 5 -168
  75. 6 41
  76. 7 -333
  77. 8 85
  78. 9 -123
  79. 10 70
  80. 11 -11
  81. 12 35
  82. 13 -97
  83. 14 190
  84. 15 -73
  85. Name: 0, dtype: int64
  86.  
  87. In [100]: sizes
  88. Out[100]:
  89. 0
  90. 0 2
  91. 1 29
  92. 2 31
  93. 3 10
  94. 4 12
  95. 5 33
  96. 6 5
  97. 7 45
  98. 8 18
  99. 9 17
  100. 10 14
  101. 11 1
  102. 12 14
  103. 13 18
  104. 14 75
  105. 15 13
  106. Name: 0, dtype: int64
  107.  
  108. import numpy as np
  109. import pandas as pd
  110.  
  111. s = pd.Series([1,2,1,-4,-7,-2,-5,4,3,1,2,-1,-2,-3])
  112.  
  113. extremums = s.groupby((np.sign(s).diff().fillna(0).ne(0)).cumsum())
  114. .apply(lambda x: x.abs().max() * np.sign(x[x.abs().idxmax()]))
  115.  
  116. In [74]: print(extremums)
  117. 0 2
  118. 1 -7
  119. 2 4
  120. 3 -3
  121. dtype: int64
  122.  
  123. In [75]: print(extremums.values)
  124. [ 2 -7 4 -3]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement