def max_occurrence(s): # 构建哈希表记录每个字母出现的次数 count = {} for c in s: count[c] = count.get(c, 0) + 1 # 找到出现次数最多的字母及其出现次数 max_count = 0 max_chars = [] for c in count: if count[c] > max_count: max_count = count[c] max_chars = [c] elif count[c] == max_count: max_chars.append(c) # 对出现次数一样的字母按字符从小到大排序 max_chars.sort() # 返回结果 return max_chars[0], max_count将这段代码变简单
时间: 2023-06-20 21:05:05 浏览: 75
可以使用Python内置的collections模块中的Counter类来替代手写的哈希表统计每个字母出现的次数,然后使用max函数来找到出现次数最多的字母及其出现次数,最后使用sorted函数对出现次数一样的字母按字符从小到大排序。代码如下:
from collections import Counter
def max_occurrence(s):
count = Counter(s)
max_count = max(count.values())
max_chars = sorted([c for c in count if count[c] == max_count])
return max_chars[0], max_count
相关问题
r1=np.zeros((24,12,12)) #三维矩阵 # np.zeros((11,11)) for i in range(0, 24): r = [] attraction=[] for CSV in list_csv: #if CSV.endswith(str(i)+'.csv'): # print(CSV) # print(str(i)+'.csv') # print(CSV.split('\\')[-1]) if CSV.split('\\')[-1] == (str(i)+'.csv'): print(CSV) df = pd.read_csv( CSV, index_col=0) #index_col用来指定表格的索引值 index_col=0 是将第一列作为索引 #print(df) set_off = np.array(df['开始城区'][0:len(df)].astype(str))#获取开始城区 arrive = np.array(df['结束城区'][0:len(df)].astype(str))#获取离开城区 #astype数据转换类型,转换成字符串存到数组中 # df[col][row]读取某个单元格 先列后行 上面代码表示为读取多行一列 # df['结束城区'][0:len(df)].astype(str) 表示对指定行和列进行类型转换 df=dateframe #print(arrive) m,d = all_np(arrive) #print(result) #print(m) attraction.append(d) r.append(list(m[0])) occurrence =np.sum(r,axis=0) #axis=0 表示在第一个维度上求和,列表只有一个维度 print(occurrence )#发生量 r.append(occurrence ) #print(attraction) # 吸引量 #print(r) r1[i, :, :] = r a1 = np.zeros((11, 11)) for k in range(0, 11): for j in range(0, 11): a = 0.1 * attraction[k] * occurrence[j] a1[k, j] = a / distance[k, j] print(r1) 什么意思
这段代码的意思是创建一个三维矩阵 `r1`,其维度为 24x12x12。然后通过循环遍历一个名为 `list_csv` 的列表,读取其中的 CSV 文件,并提取其中的数据。在每个 CSV 文件中,根据特定条件筛选出所需的数据,并存储到相应的数组中。接着,对这些数据进行计算和处理,生成新的数组 `occurrence` 和 `a1`。最后,将 `occurrence` 添加到原始数据数组 `r` 中,并将其赋值给 `r1` 的相应位置。最后,打印输出 `r1`。
需要注意的是,代码中使用了一些函数和变量,例如 `np.zeros()`、`pd.read_csv()`、`np.array()`、`astype()`、`np.sum()` 和 `distance`。在这段代码中,可能还有其他相关的变量和函数没有提供,所以无法完全理解其含义。
def get_occurrences_of_sequence(original_dataset: [], checked_sequence: ()) -> []:
# This function takes in two parameters:
# 1. original_dataset: a list of sequences that we want to search for occurrences of the checked_sequence
# 2. checked_sequence: a tuple representing the sequence that we want to count occurrences of in the original_dataset
# The function returns a list of integers representing the number of occurrences of the checked_sequence in each sequence of the original_dataset
occurrences_list = [] # create an empty list to store the number of occurrences of the checked_sequence in each sequence of the original_dataset
for sequence in original_dataset: # iterate through each sequence in the original_dataset
count = 0 # initialize a count variable to keep track of the number of occurrences of the checked_sequence in the current sequence
for i in range(len(sequence) - len(checked_sequence) + 1): # iterate through each possible starting index of the checked_sequence in the current sequence
if sequence[i:i+len(checked_sequence)] == checked_sequence: # if the current slice of the sequence matches the checked_sequence, increment the count
count += 1
occurrences_list.append(count) # append the count to the occurrences_list for the current sequence
return occurrences_list # return the list of occurrence counts
阅读全文