Guest User

Untitled

a guest
Mar 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. b_data=[('example',123),('example-one',456),('example',987),.....]
  2.  
  3. blockslst=[]
  4. for line in b_data:
  5. blockslst.append(line[0])
  6.  
  7. blocklstgtone=[]
  8. for item in blockslst:
  9. if(blockslst.count(item)>1):
  10. blocklstgtone.append(item)
  11.  
  12. from collections import Counter
  13.  
  14. counts = Counter(x[0] for x in b_data)
  15. print(counts['example'])
  16.  
  17. print(counts['foo'])
  18.  
  19. print(counts.most_common(n))
  20.  
  21. from itertools import takewhile
  22.  
  23. l = [1, 1, 2, 2, 3, 3, 1, 1, 5, 4, 6, 7, 7, 8, 3, 3, 2, 1]
  24. c = Counter(l)
  25.  
  26. list(takewhile(lambda x: x[-1] > 1, c.most_common()))
  27. [(1, 5), (3, 4), (2, 3), (7, 2)]
  28.  
  29. [item[0] for item in counts.most_common() if item[-1] > 1]
  30.  
  31. print(list(map(lambda x:x[0],b_data)).count('example'))
  32.  
  33. 2
  34.  
  35. b_data = [('example', 123), ('example-one', 456), ('example', 987)]
  36.  
  37. dict_1={}
  38. for i in b_data:
  39. if i[0] not in dict_1:
  40. dict_1[i[0]]=1
  41. else:
  42. dict_1[i[0]]+=1
  43.  
  44. print(dict_1)
  45.  
  46.  
  47.  
  48. print(list(filter(lambda y:y!=None,(map(lambda x:(x,dict_1.get(x)) if dict_1.get(x)>1 else None,dict_1.keys())))))
  49.  
  50. [('example', 2)]
  51.  
  52. b_data = [('example', 123), ('example-one', 456), ('example', 987),('example-one', 456),('example-one', 456),('example-two', 456),('example-two', 456),('example-two', 456),('example-two', 456)]
  53.  
  54. [('example-two', 4), ('example-one', 3), ('example', 2)]
  55.  
  56. from collections import Counter
  57. import random
  58. from datetime import datetime # good enough for a loong running op
  59.  
  60.  
  61. dt_datagen = datetime.now()
  62. numberOfKeys = 100000
  63.  
  64.  
  65. # basis for testdata
  66. textData = ["example", "pose", "text","someone"]
  67. numData = [random.randint(100,1000) for _ in range(1,10)] # irrelevant
  68.  
  69. # create random testdata from above lists
  70. tData = [(random.choice(textData)+str(a%10),random.choice(numData)) for a in range(numberOfKeys)]
  71.  
  72. tData.append(("aaa",99))
  73.  
  74. dt_dictioning = datetime.now()
  75.  
  76. # create a dict
  77. countEm = {}
  78.  
  79. # put all your data into dict, counting them
  80. for p in tData:
  81. if p[0] in countEm:
  82. countEm[p[0]] += 1
  83. else:
  84. countEm[p[0]] = 1
  85.  
  86. dt_filtering = datetime.now()
  87. #comparison result-wise (commented out)
  88. #counts = Counter(x[0] for x in tData)
  89. #for c in sorted(counts):
  90. # print(c, " = ", counts[c])
  91. #print()
  92. # output dict if count > 1
  93. subList = [x for x in countEm if countEm[x] > 1] # without "aaa"
  94.  
  95. dt_printing = datetime.now()
  96.  
  97. for c in sorted(subList):
  98. if (countEm[c] > 1):
  99. print(c, " = ", countEm[c])
  100.  
  101. dt_end = datetime.now()
  102.  
  103. print( "nnCreating ", len(tData) , " testdataitems took:t", (dt_dictioning-dt_datagen).total_seconds(), " seconds")
  104. print( "Putting them into dictionary took t", (dt_filtering-dt_dictioning).total_seconds(), " seconds")
  105. print( "Filtering donw to those > 1 hits took t", (dt_printing-dt_filtering).total_seconds(), " seconds")
  106. print( "Printing all the items left took t", (dt_end-dt_printing).total_seconds(), " seconds")
  107.  
  108. print( "nTotal time: t", (dt_end- dt_datagen).total_seconds(), " seconds" )
  109.  
  110. # reformatted for bevity
  111. example0 = 2520 example1 = 2535 example2 = 2415
  112. example3 = 2511 example4 = 2511 example5 = 2444
  113. example6 = 2517 example7 = 2467 example8 = 2482
  114. example9 = 2501
  115.  
  116. pose0 = 2528 pose1 = 2449 pose2 = 2520
  117. pose3 = 2503 pose4 = 2531 pose5 = 2546
  118. pose6 = 2511 pose7 = 2452 pose8 = 2538
  119. pose9 = 2554
  120.  
  121. someone0 = 2498 someone1 = 2521 someone2 = 2527
  122. someone3 = 2456 someone4 = 2399 someone5 = 2487
  123. someone6 = 2463 someone7 = 2589 someone8 = 2404
  124. someone9 = 2543
  125.  
  126. text0 = 2454 text1 = 2495 text2 = 2538
  127. text3 = 2530 text4 = 2559 text5 = 2523
  128. text6 = 2509 text7 = 2492 text8 = 2576
  129. text9 = 2402
  130.  
  131.  
  132. Creating 100001 testdataitems took: 4.728604 seconds
  133. Putting them into dictionary took 0.273245 seconds
  134. Filtering donw to those > 1 hits took 0.0 seconds
  135. Printing all the items left took 0.031234 seconds
  136.  
  137. Total time: 5.033083 seconds
Add Comment
Please, Sign In to add comment