def foo(arr): a = ctr(arr) t = dict() for k, v in a.items(): if k > 1000: t[k] = v return t
def get_array_centroid(arr, part, ofAll=True): """ Find some most resent elements of array of arrays part : 0-1 : filter of frequency ofAll = True : find some most recent elements of all ofAll = False: find elements with frequency of appear in rows greater then part -> maybe empty """ rows_amount = len(arr) if rows_amount == 0: return -1 plain_array = [] for a in arr: plain_array += a counter = ctr(plain_array) el_amount = len(counter) res = [] if ofAll: # Find min frequency p = int(part * el_amount) for el in counter.most_common(p): res.append(el[0]) else: p = int(part * rows_amount) for cort in counter.most_common(): if cort[1] >= p: res.append(cort[0]) return res
def get_centroid(arr): """ Find most resent element of plain array """ if len(arr) == 0: return -1 return ctr(arr).most_common(1)[0][0]
def word_dictionary(self): """ assigns each word[redundant enough to be in the vocabulary] a numerical value builds a dictionary of words and their assigned numerical value :return: tuple of dictionary and word frequency """ vocabulary = [] for line in self.labeled_dataset[0]: sentence = line.split() for word in sentence: vocabulary.append(word) word_frequency = [[self.rare_word, -1], [self.sentence_begin, -2], [self.sentence_end, -3]] word_frequency.extend( ctr(vocabulary).most_common(self._vocabulary_size - 1)) dictionary = {} word_index = 0 for word in word_frequency: dictionary[word[0]] = word_index word_index += 1 self.word_dictionary = dictionary return (self.word_dictionary, word_frequency)
def get_array_centroid(arr, part, ofAll=True): """ Find some most resent elements of array of arrays part : 0-1 : filter of frequency ofAll = True : find some most recent elements of all ofAll = False: find elements with frequency of appear in rows greater then part -> maybe empty """ rows_amount = len(arr) if rows_amount == 0: return -1 plain_array = [] for a in arr: plain_array += a counter = ctr(plain_array) el_amount = len(counter) res = [] if ofAll: # Find min frequency p = int(part*el_amount) for el in counter.most_common(p): res.append(el[0]) else: p = int(part*rows_amount) for cort in counter.most_common(): if cort[1] >= p: res.append(cort[0]) return res
def getValue(hand, DeckDict): """ Calculates the value of the person's hand Input: list of cards on hand Output: Int value of hand """ hv = [] if ("AH" in hand) or ("AC" in hand) or ("AS" in hand) or ("AD" in hand): counted = ctr(hand) NoAces = counted["AH"] + counted["AC"] + counted["AS"] + counted["AD"] for i in hand: hv.append(DeckDict[i]) Value1 = sum(hv) Value2 = Value1 for z in range(NoAces - 1): Value2 -= 10 Value3 = Value2 - 10 if Value1 == 21: return Value1 elif Value2 == 21: return Value2 elif Value3 == 21: return Value3 elif Value1 < 21: return Value1 elif Value2 > 21: return Value3 elif Value1 > 21: return Value2 else: for i in hand: hv.append(DeckDict[i]) Value = sum(hv) return Value
def get_max_word(list_lyrics): counts = [] count_list = [] for lyric in list_lyrics: count_list = ctr(lyric.split()).most_common(3) counts.append(count_list) return counts
def step(grid, threshold, pss): new_grid = copy.deepcopy(grid) for i in range(len(grid)): for j in range(len(grid[i])): num_occ = ctr(nbs(i, j, grid, pss).values())['#'] if grid[i][j] == 'L' and num_occ == 0: new_grid[i][j] = '#' elif grid[i][j] == '#' and num_occ >= threshold: new_grid[i][j] = 'L' return new_grid
def Shannon_Fano(signal): znakovi = ctr(signal) lista = sorted([(b, '') for b, a in znakovi.items()], key=itemgetter(0), reverse=True) Shannon_Fano_Pomocna(0, len(lista) - 1, lista) rjecnikKodovaFun = dict((key, value) for key, value in lista) kodiraniSignal = '' for karakter in signal: kodiraniSignal += rjecnikKodovaFun[karakter] #print(kodiraniSignal) return kodiraniSignal, rjecnikKodovaFun
def get_many_time_padded_key(ct, *args): max_len = max(map(len, ct)) key = [0 for _ in range(max_len)] for i in range(max_len): eligible_ciphertexts = filter(lambda c: len(c) > i, ct) ''' For each c[i], we guess that it is a space and add c[i] ^ SPACE into the counter for each ascii letter it reveals in another ciphertext ''' most_common_candidates = ctr(c[i] ^ ord(' ') for c, d in permutations(eligible_ciphertexts, 2) if chr(c[i] ^ d[i] ^ ord(' ')) in ascii_letters).most_common(1) key[i] = most_common_candidates[0][0] if most_common_candidates else 0 if args: for i in args[0]: key[i] = args[0][i] return b''.join(map(lambda k: bytes([k]), key))
def steadyGene(gene): # let's get input n = len(gene) cnt = ctr(gene) # If all element is less than n/4, substring length is 0 if all(e<=n/4 for e in cnt.values()): return 0 minSub = math.inf cnted = 0 # Find the last sequence of the string satisfying the condition for i in range(n): cnt[gene[i]] -= 1 while all(e<=n/4 for e in cnt.values()) and cnted <= i: minSub = min(minSub, i-cnted+1) cnt[gene[cnted]]+=1 cnted += 1 return minSub
def izvjestaj(): brojBitaOrginalnogSignala = len(ulazniSignal) * 32 brojBitaKodiranogSignala = len(kodiraniSignal) znakovi = ctr(ulazniSignal) lista_ponavljanja = [a for b, a in znakovi.items()] print('Rjecnik vrijednosti:') for karakter in rjecnikKodova: print("Vrijednost:" + str(karakter) + ", Kod: " + rjecnikKodova[karakter]) print(" ") print("Broj bita potreban za prikaz nekodiranog signala: " + str(brojBitaOrginalnogSignala)) print("Broj bita potreban za prikaz kodiranog signala: " + str(brojBitaKodiranogSignala)) print("Stepen kompresije iznosi: " + str(brojBitaOrginalnogSignala / brojBitaKodiranogSignala)) if (ulazniSignal == dekodiraniSignal).all(): print("Ulazni i dekodirani signal su identicni.") else: print("Ulazni i dekodirani signal nisu identicni.") print('Ulazni signal:', ulazniSignal) datoteka = open('kodiraniSignalShannonFano.txt', 'a+') datoteka.write(kodiraniSignal) datoteka.close() print('Kodirani signal je upisan u datoteku kodiraniSignalShannonFano.txt') fig, pt = plt.subplots(2) fig.tight_layout(pad=3.0) fig.suptitle('Shannon-Fano kompresija') pt[0].plot(x, ulazniSignal) pt[0].set_title('Ulazni signal') pt[1].plot(x, dekodiraniSignal) pt[1].set_title('Dekodirani signal: ') plt.show()
def count(grid): count = 0 for row in grid: count += ctr(row)['#'] return count
def apureVotes(li): return ctr(li).most_common(1)[0][0]
from collections import Counter as ctr with open("sample.txt") as f: counter = ctr() for line in f: counter.update(line.strip().split()) print(counter.most_common())
from collections import Counter as ctr print("Initial Counter") print(ctr()) counter = ctr([(1, 2), (1, 1), (1, 2), (2, 1)]) print(counter) print("*" * 10) # Modifying the counter counter.update("abbefg") print(counter) print("*" * 10) # Modifying the counter using the dictionary counter.update({'z': 3, 'q': 4}) print(counter) print("*" * 10) # Getting the most repeated item print(counter.most_common()) print(counter.most_common(3)) print("*" * 10) #iterating the counter for letter in "abcd": print("{}, {}".format(letter, counter[letter])) # Counter instances support arithmetic and set operations for aggregating results. c1 = ctr(['a', 'b', 'c', 'a', 'b', 'b']) c2 = ctr('alphabet') print(c1)
def permAll(lst): cntr = ctr(lst) ans = 1 for i in cntr.values(): ans *= perm(i,i) return perm(len(lst),len(lst))/ans
def weather(self, hS): print('Calculating the weather . . .') #print(hS) # Estaciones del año invierno = { 'temperatura': ' bajas temperaturas ', 'clima': ['Frío', 'Lluvias', 'Nieve'] } primavera = { 'temperatura': ' temperaturas oscilando los 25 °C', 'clima': 'Cálido' } verano = { 'temperatura': ' altas temperaturas ', 'clima': ['Calor', 'Caluroso'] } otoño = {'temperatura': ' temperaturas agradables ', 'clima': 'Fresco'} # Regiones de la República Mexicana region_1 = { 'region': ' Región NorOeste ', 'estados': [ 'BajaCalifornia', 'BajaCaliforniaSur', 'Chihuahua', 'Durango', 'Sinaloa', 'Sonora' ], 'semaforo': 'Naranja y Rojo' } region_2 = { 'region': 'Región NorEste', 'estados': ['Coahuila', 'NuevoLeón', 'Tamaulipas'], 'semaforo': 'Naranaja' } region_3 = { 'region': 'Región Occidente', 'estados': ['Nayarit', 'Jalisco', 'Colima', 'Michoacán'], 'semaforo': 'Naranja' } region_4 = { 'region': 'Región Oriente', 'estados': ['Puebla', 'Veracruz', 'Tlaxcala', 'Hidalgo'], 'semaforo': 'Naranja y Amarillo' } region_5 = { 'region': 'Región Centro Norte', 'estados': [ 'Aguascalientes', 'Guanajuato', 'SanLuisPotosí', 'Zacatecas', 'Querétaro' ], 'semaforo': 'Naranja y Rojo' } region_6 = { 'region': 'Región Centro Sur', 'estados': ['Morelos', 'EdoMéx', 'CDMX'], 'semaforo': 'Rojo' } region_7 = { 'region': 'Región SurOeste', 'estados': ['Guerrero', 'Oaxaca', 'Chiapas'], 'semaforo': 'Naranja y Verde' } region_8 = { 'region': 'Región SurEste', 'estados': ['Tabasco', 'Campeche', 'QuintanaRoo', 'Yucatán'], 'semaforo': 'Naranja y Verde' } #regiones= [region_1, region_2, region_3, region_4, region_5, region_6, region_7, region_8] weather_dict = dict() weather_dict['date'] = hS['date'] weather_dict['states'] = [] # Se determina la estación del año respecto al mes month = hS['date'][0] if month < 3: weather_dict['season'] = 'Invierno' weather_dict['temperature'] = invierno['temperatura'] weather_dict['climate'] = invierno['clima'] elif (month > 3) & (month < 7): weather_dict['season'] = 'Primavera' weather_dict['temperature'] = primavera['temperatura'] weather_dict['climate'] = primavera['clima'] elif month > 6 & month < 10: weather_dict['season'] = 'Verano' weather_dict['temperature'] = verano['temperatura'] weather_dict['climate'] = verano['clima'] elif month > 9 & month < 13: weather_dict['season'] = 'Otoño' weather_dict['temperature'] = otoño['temperatura'] weather_dict['climate'] = otoño['clima'] # estados = hS['states'] R1, R2, R3, R4, R5, R6, R7, R8 = ([] for i in range(8)) # Se determina la región de la República Mexicana for item in estados: if item in region_1['estados']: R1.append(item) if ctr(R1) == ctr(region_1['estados']): weather_dict['states'].append(region_1) for item in estados: if item in region_2['estados']: R2.append(item) if ctr(R2) == ctr(region_2['estados']): weather_dict['states'].append(region_2) for item in estados: if item in region_3['estados']: R3.append(item) if ctr(R3) == ctr(region_3['estados']): weather_dict['states'].append(region_3) for item in estados: if item in region_4['estados']: R4.append(item) if ctr(R4) == ctr(region_4['estados']): weather_dict['states'].append(region_4) for item in estados: if item in region_5['estados']: R5.append(item) if ctr(R5) == ctr(region_5['estados']): weather_dict['states'].append(region_5) for item in estados: if item in region_6['estados']: R6.append(item) if ctr(R6) == ctr(region_6['estados']): weather_dict['states'].append(region_6) for item in estados: if item in region_7['estados']: R7.append(item) if ctr(R7) == ctr(region_7['estados']): weather_dict['states'].append(region_7) for item in estados: if item in region_8['estados']: R8.append(item) if ctr(R8) == ctr(region_8['estados']): weather_dict['states'].append(region_8) #print(weather_dict) return weather_dict
from math import ceil #a = (np.random.geometric(p=0.01, size=10**7)-1)*10 #np.savetxt("test_depths.txt", a, fmt="%d") filename = "short_depths.txt" depths = dd(lambda:0) with open(filename, 'r') as f: for line in f: depth = int(line.split("\t")[0]) depths[depth] += 1 a = [5,1,2,2,9,345,7,100,7,100,7] depths = ctr() for el in a: depths[el] += 1 # frequencies has: # Keys: Depth values # Values: Frequency of those depths # Sorted by Values frequencies = od(sorted(depths.items(), reverse=True)) rankdata = dict() ranks = [] current_rank = 1 for depth, frequency in frequencies.items(): #print("depth: {}, frequency: {}".format(frequency, count)) if frequency > 1: