def sameNum(fst,sec): """ calculate same and different nums """ length = min(len(fst),len(sec)) x = 0 for index in range(length): if fst[index] == sec[index]: x += 1 z = sum((Counter(fst) & Counter(sec)).values()) return ("{}A{}B".format(x,(z-x)))
def findAnagrams(self, s: str, p: str) -> List[int]: answer = [] m, n = len(s), len(p) if m < n: return answer sc = Counter(s[:n - 1]) pc = Counter(p) for index in range(n - 1, m): sc[s[index]] += 1 if sc == pc: answer.append(index + 1 - n) sc[s[index + 1 - n]] -= 1 if sc[s[index + 1 - n]] == 0: del sc[s[index + 1 - n]] return answer
def sortColors(self, nums: List[int]) -> None: from collection import Counter count = Counter(nums) for i in range(len(nums)): if i < count[0]: nums[i] = 0 elif i < count[1] + count[0]: nums[i] = 1 else: nums[i] = 2
def calculate_popular(base_dir, lines): pn = r'\[IMAGE_VIEW\] image view (.+) at (\d+)' pn = re.compile(pn) counter = Counter() for line in lines: match = pn.search(line) if match.group(1).find(base_dir) != -1: path = os.path.relpath(match.group(1), base_dir) else: path = match.group(1) counter[path.replace('\\', '/')] += 1 return dict(counter.most_common(50))
def make_dictionary(train_dir, number): emails = [os.path.join(train_dir, f) for f in os.listdir(train_dir)] all_words = [] for mail in emails: with open(mail) as m: for line in m: words = line.split() all_words += words dictionary = Counter(all_words) list_to_remove = dictionary.keys() for item in list_to_remove: if item.isalpha() == False and item.isnumeric()==False and item[0:2] != "__": del dictionary[item] elif len(item) <= 1 and item!= "i": del dictionary[item] dictionary = dictionary.most_common(number) return dictionary
counts = get_count(time_zones) ###### 对字典进行排序 # 转化为list再排序 def top_counts(countDict, n=10): countList = [(value, key) for key, value in countDict.items()] countList.sort() return countList[-n:] top_counts(counts) ###### 利用库函数直接实现对字典的 统计+排序 from collection import Counter counts = Counter(time_zones) counts.most_common(10) ###### 利用pandas和numpy进行分析 from pandas import DataFrame, Series import pandas as pd import numpy as np frame = DataFrame(records) # records 是一个List,其中每一个元素是一个字典 frame['tz'][:10] # 直接查看tz列的前10个数据,frame['tz']是一个Series对象 tz_counts = frame['tz'].value_counts() # Series对象有一个value_counts方法用于统计出现的次数 clean_tz = frame['tz'].fillna('Missing') # 填补的是 Nan clean_tz[clean_tz == ''] = 'Unknown' # 填补的是 空 tz_counts = clean_tz.value_counts() tz_counts[:10].plot(kind='barh', rot=0)
def fit(self, data): result = Counter() result.update([item for item in self.data]) return result
from collection import Counter a = "aaaaaabbbbccc" my_counter = Counter(a) print(my_counter)
# number of speeches len(speeches) # list the presidents [speech['president'] for speech in speeches] # list the dates [speech['date'] for speech in speeches] # get text from first speech and make list of words text = speeches[0]['text'] words = [w for w in re.split('\W', text) if w] vocab = sorted(set(words)) from collection import Counter word_counts = Counter(words) # function to extract president and word count def summarize(speech): text = speech['text'] words = [w for w in re.split('\W', text) if w] return (speech['president'], len(words), speech['date']) summary = [summarize(speech) for speech in speeches] # https://wiki.python.org/moin/HowTo/Sorting from operator import itemgetter [speech[2] for speech in sorted(summary, key=itemgetter(1))]
from collection import Counter 2 in Counter(["one", "one", "two"]).values() 2 in Counter(["one", "two", "three"]).values()
#ordenar num_friends.sort() #media num_friends.mean() #mediana np.median(num_friend) #moda np.bincount(num_friends).argmax() #or from collection import Counter b = Counter(num_friends) b.most_common(1) #dispersão ou amplitude num_friends.max() - num_friends.mix() #variancia variace = np.sum( np.square([x_list - myfriends.mean() for x_list in num_friends])) #or np.var(num_friends) #standart deviantion np.sqrt( np.sum(np.square([x_list - myfriends.mean() for x_list in num_friends])))
def getRaceCount(): races = char_df['Race'].tolist() race_count = Counter(races) return (race_count)
def evaluate(_): win_name = 'Detector' cv2.namedWindow(win_name) video = FLAGS.video if is_url(video): videoPafy = pafy.new(video) video = videoPafy.getbest(preftype="mp4").url cam = cv2.VideoCapture(video) if not cam.isOpened(): raise IOError('Can\'t open "{}"'.format(FLAGS.video)) source_h = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) source_w = cam.get(cv2.CAP_PROP_FRAME_WIDTH) model_cls = find_class_by_name(FLAGS.model_name, [yolo]) model = model_cls(input_shape=(source_h, source_w, 3)) model.init() frame_num = 0 start_time = time.time() fps = 0 try: while True: ret, frame = cam.read() if not ret: logger.info('Can\'t read video data. Potential end of stream') return object_count_dict = Counter() predictions = model.evaluate(frame) for o in predictions: x1 = o['box']['left'] x2 = o['box']['right'] y1 = o['box']['top'] y2 = o['box']['bottom'] color = o['color'] class_name = o['class_name'] object_count_dict[str(class_name)] += 1 # Draw box cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) # Draw label (test_width, text_height), baseline = cv2.getTextSize( class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 1) cv2.rectangle(frame, (x1, y1), (x1 + test_width, y1 - text_height - baseline), color, thickness=cv2.FILLED) cv2.putText(frame, class_name, (x1, y1 - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1) logger.info(object_count_dict) end_time = time.time() fps = fps * 0.9 + 1 / (end_time - start_time) * 0.1 start_time = end_time # Draw additional info frame_info = 'Frame: {0}, FPS: {1:.2f}'.format(frame_num, fps) cv2.putText(frame, frame_info, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) logger.info(frame_info) cv2.imshow(win_name, frame) if predictions: logger.info('Predictions: {}'.format( format_predictions(predictions))) key = cv2.waitKey(1) & 0xFF # Exit if key == ord('q'): break # Take screenshot if key == ord('s'): cv2.imwrite('frame_{}.jpg'.format(time.time()), frame) frame_num += 1 finally: cv2.destroyAllWindows() cam.release() model.close()