def detect_anagrams(anagram, clist): flist = [] anaCount = cnt(anagram.lower()) for word in clist: p = cnt(word.lower()) if (p - anaCount) == cnt("") and (anaCount - p) == cnt("") and anagram.lower() != word.lower(): flist.append(word) return flist
def dfs(use, i): return ( use and i < len(w) and max( dfs(use, i + 1), not cnt(w[i]) - use and sum(s[ord(c) - ord("a")] for c in w[i]) + dfs(use - cnt(w[i]), i + 1), ) )
def convert_to_list(self, ID): """ ID가 dict면 list로 바꾸고, 아니면 그대로 반환하는 함수 :param ID: dict or list :return: list """ if type(ID) == dict: if ID == self.area_ID: # 나라 기준 rel rli = [] for I in ID: if type(ID[I]) == list: rli.extend(ID[I]) else: rli.append(ID[I]) ID = rli elif ID == self.nati_ID: # 지역 기준 rel rli = [] for I in ID: if type(ID[I]) == list: rli.extend(ID[I]) else: rli.append(ID[I]) ID = list(dict(cnt(rli)).keys()) return ID
def pbl_height_from_profiles(t, tv, p, rh, z, z0=0., z1=4000.0): # user defined height limit (default = 4000.)... ind_lim = np.logical_and(z >= z0, z <= z1) # using variables just below zlim: zlevls = z[ind_lim] pressn = p[ind_lim] thetad = t[ind_lim] thetav = tv[ind_lim] relhum = rh[ind_lim] tempsn = calc_t(thetad, pressn) # Calculate Specific humidity : wvapor = calc_w_from_t(thetad, thetav) evapor = calc_e_from_w(wvapor, pressn) qvapor = calc_q_from_w(wvapor) # Calculate refractivity: nrefrc = calc_refr(pressn, tempsn, evapor) # Store gradients: # dq_dz: vertical gradient of (Qv) specific humidity, # dh_dz: vertical gradient of (Hr) relative humidity, # dt_dz: vertical gradient of (Thetav) virtual potential temperature, # dn_dz: vertical gradient of (Nr) atmospheric refractivity. dt_dz = nl.calc_derv_ngrid(thetav, zlevls) dh_dz = nl.calc_derv_ngrid(relhum, zlevls) dq_dz = nl.calc_derv_ngrid(qvapor, zlevls) dn_dz = nl.calc_derv_ngrid(nrefrc, zlevls) # passing a low-pass filter: ips = 10 dt_dz = nl.low_pass(dt_dz, ipass=ips) dh_dz = nl.low_pass(dh_dz, ipass=ips) dq_dz = nl.low_pass(dq_dz, ipass=ips) dn_dz = nl.low_pass(dn_dz, ipass=ips) # Taking the first (max-min) gradients values with length: sample sample = 20 # Sorting gradients tp_h = zlevls[np.argsort(dt_dz)[:-sample - 1:-1]] rh_h = zlevls[np.argsort(dh_dz)[:sample]] qv_h = zlevls[np.argsort(dq_dz)[:sample]] nr_h = zlevls[np.argsort(dn_dz)[:sample]] # heights = tp_h, rh_h, qv_h, nr_h pair = cnt(np.concatenate((heights), axis=0)).most_common() repv = pair[0][1] if repv < 3: raise Error_Message('Not found') return [] else: return [pair[0][0]]
def filter_dataset(df, drop_all_positives=True): usable = set([ x[0] for x in cnt(df[['qid', 'label']].drop_duplicates() ['qid'].values.tolist()).items() if x[1] > 1 ]) if drop_all_positives: return df[df.qid.isin(usable)] else: return df[df.qid.isin(usable) | df.label == True]
def flip_coin(self, n): outcomes = [] for flip in range(n): flip = random.randint(0, 1) outcomes.append(self.sides[flip]) outcomes = cnt(outcomes) return print( f"Flipped the coin {n} times\nTails occured {outcomes['Tails']} times.\nHeads occured {outcomes['Heads']} times." )
def test_maxpt_min_num_areas_in_region_threshold(self): instance = self.map_instance from collections import Counter as cnt instance.dataOperation("CONSTANTS = 1") thresholds = [5, 8, 13, 21, 34] for threshold in thresholds: instance.cluster('maxpTabu', ['CONSTANTS'], threshold=threshold) region_size = cnt(instance.region2areas).values() self.assertTrue(all(item >= threshold for item in region_size))
def maxScoreWords(self, w: List[str], l: List[str], s: List[int]) -> int: def dfs(use, i): return ( use and i < len(w) and max( dfs(use, i + 1), not cnt(w[i]) - use and sum(s[ord(c) - ord("a")] for c in w[i]) + dfs(use - cnt(w[i]), i + 1), ) ) return int(dfs(cnt(l), 0))
def countCharacters(self, words: List[str], chars: str) -> int: return sum(not cnt(w) - cnt(chars) and len(w) for w in words)
# -*- coding: utf-8 -*- """ Created on Mon Feb 20 16:34:22 2017 @author: congdonguyen """ import json import nltk import csv from collections import Counter as cnt from nltk.corpus import stopwords from nltk.corpus import words as wCorpus #Initialize placeholder for data stars1 = cnt() stars2 = cnt() stars3 = cnt() stars4 = cnt() stars5 = cnt() #Store data in columns to write col1 = [] col2 = [] #Open json file with open('yelp_academic_dataset_review_small.json') as jsonData: jsonObject = json.load(jsonData) #Load JSON json_string = json.dumps(jsonObject)
def getWordCloud(): top_headlines = "" wordString = "" top_headlines = newsapi.get_top_headlines(language='en', country='us', page_size=80) flag = False mydict = {} mylist = [] for x in top_headlines: if (x == 'articles'): for y in top_headlines[x]: for z in y: if (z == 'source'): temp = {} temp = y[z] for x in temp: if (temp[x] == '' or temp[x] == None or temp[x] == "null"): flag = True if z == 'urlToImage': if y[z] != '' and y[z] != None and y[z] != "null": imageUrl = y[z] else: flag = True if z == 'title': if y[z] != '' and y[z] != None and y[z] != "null": newsTitle = y[z] else: flag = True if z == 'description': if y[z] != '' and y[z] != None and y[z] != "null": newsD = y[z] else: flag = True if z == 'url': if y[z] != '' and y[z] != None and y[z] != "null": newsUrl = y[z] else: flag = True if z == 'author': if y[z] == '' or y[z] == None or y[z] == "null": flag = True if z == 'publishedAt': if y[z] == '' or y[z] == None or y[z] == "null": flag = True if flag != True: wordString += newsTitle mydict = { 'Image': imageUrl, 'Title': newsTitle, 'Description': newsD, 'URL': newsUrl } mylist.append(mydict) flag = False print("this is wordstring", wordString) alp = "" alp2 = "" for char in wordString: if char.isalnum() or char.isspace(): alp += char for char in alp: if (not char.isdigit()): alp2 += char print("this is alp", alp2) splitList = alp2.split() Counter = cnt(splitList) most_occur = Counter.most_common(100) print(most_occur) wordList = [] for k, v in most_occur: # print(k) wordList.append(k) stopWordList = [] newList = [] file = open("stopwords_en.txt", "r") lines = file.readlines() for l in lines: stopWordList.append(l.split('\n')[0]) print("stopWordList", stopWordList) for i in stopWordList: newList.append(i) # print(i) x = i.capitalize() # print(x) newList.append(x) print("stopWordList", newList) finalList = list(set(wordList) - set(newList)) print("useful", finalList) return jsonify(finalList) # return jsonify(mylist) mylist.clear() mydict.clear()
from json import load from collections import Counter as cnt with open("incidents.json", mode="r") as f: incidents = load(f) tickets = incidents["tickets"] c = cnt([x["src_ip"] for x in tickets]) print(c) to_check = input("Who to target?") targeted = set() for ticket in tickets: if ticket["src_ip"] == to_check: targeted.add(ticket["dst_ip"]) print(len(targeted)) file_hash_to_recs = {} for ticket in tickets: if ticket["file_hash"] not in file_hash_to_recs: file_hash_to_recs[ticket["file_hash"]] = set() file_hash_to_recs[ticket["file_hash"]].add(ticket["dst_ip"]) print(file_hash_to_recs) print(sum(len(x) for x in file_hash_to_recs.values()) / len(file_hash_to_recs))
def word_count(string): answer = cnt() for i in string.split(): answer[i] += 1 return answer
def countCharacters2(self, words, chars): # 532ms return sum(not cnt(w) - cnt(chars) and len(w) for w in words)
""" Aprendendo a manipular informações usando coleções do python """ from collections import Counter as cnt import pandas as pd import matplotlib import matplotlib.pyplot as plt texto = "Robson rodrigues de souza maria das dores rodrigues da silva roberta rodrigues de souza" palavras = texto.split() palavras = dict(cnt(palavras)) palavras_df = pd.DataFrame(palavras.items(), columns=['Palavras', 'Quantidade']) palavras_df = palavras_df.sort_values(by=["Quantidade"], ascending=False) palavras_df.head(10).plot(kind='bar', x='Palavras', y='Quantidade', title="Palavraaaas mais escritas") #x = [0, 1, 2, 3, 4, 5] #y = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] #plt.plot(x, y) #plt.show()
def uniqueOccurrences(self, arr: List[int]) -> bool: c = cnt(arr) return len(c) == len(set(c.values()))
# verilen string kendini tekrar eden harflerin kaç tane olduğunu yaz from collections import Counter as cnt s = sorted(input()) count = cnt(s).most_common() #print(count) count = sorted(count, key=lambda x: (x[1] * -1, x[0])) for i in range(0, 3): print(count[i][0], count[i][1]) """ from string import ascii_letters as alp from collections import Counter as cnt d = {} s = str(input()) for key, val in cnt(s).items(): if val == 1: pass else: d[key] = val for i in d: print(i, d[i]) """
# -*- coding: utf-8 -*- """ Created on Fri Nov 24 12:38:29 2017 @author: sanga """ #Importing the necessary packages from datetime import datetime from datetime import timedelta as td from collections import Counter as cnt #Start and end dates. Since the question mentioned 1990 and 2000, the beginning and end of #the respective years have been taken. start_date = datetime(1990, 1, 1) end_date = datetime(2000, 12, 31) #Using timedelta to add dates in the considered range of difference in end and start dates days_diff = [ start_date + td(i) for i in range((end_date - start_date).days + 1) ] #Using strftime to convert the tuples to a dict days_dict = dict(cnt(diff.strftime('%a') for diff in days_diff)) #Printing number of Thursdays from the dict print("Number of Thursdays: " + str(days_dict['Thu']))
def uniqueOccurrences(self, arr: List[int]) -> bool: return all(v == 1 for v in cnt(cnt(arr).values()).values())
linked_words = linking_words(p_words, all_articles, tokenizer) # saving the dict np.save('linked_words.npy', linked_words) # Load #read_dictionary = np.load('linked_words.npy').item() linked_words = np.load('linked_words.npy').item() # #get the most common ones # also dict to dataframe # 'collections counter' library to be used from collections import Counter as cnt common_companions = {} keys = list(linked_words.keys()) for key in keys: counted = cnt(linked_words[key]) commons = counted.most_common(10) t = {} for k, v in commons: t[k] = v common_companions[key] = t # to dataframe commonCompanions = pd.DataFrame(common_companions) # columns are primary words # get rid of nans commonCompanions = commonCompanions.fillna(0) # columns to rows commonCompanions = commonCompanions.transpose() """ ################################# #################################
def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: row, col = cnt(r for r, c in indices), cnt(c for r, c in indices) return sum((row[i] + col[j]) % 2 for i in range(n) for j in range(m))
import numpy as np from collections import Counter as cnt tweets = ['a x', 'a c', 'b y'] tweetscores = [1., 3., 2.] words = ['a', 'b', 'c'] repeats = [2, 1, 1] wordscores = np.zeros(len(words)) for i in range(len(words)): for j in range(len(tweets)): intersect = set(words[i]) & set(tweets[j].split()) if intersect != set([]): wordscores[i]=wordscores[i]+(tweetscores[j]/repeats[i]) print wordscores """OR""" A = cnt({'a':1, 'b':2, 'c':3}) B = cnt({'b':3, 'c':4, 'd':5}) totals = A + B X = cnt({'a':1, 'b':2, 'c':3, 'c':5}) print X
def smallestCommonElement(self, mat: List[List[int]]) -> int: return min([k for k, v in cnt(chn(*mat)).items() if v == len(mat)] or [-1])