python统计一个数字序列中,按指定步长和步幅来分割指定步长的数字序列出现的频率并根据评率降序排列

python统计一个数字序列中,按指定步长和步幅来分割指定步长的数字序列出现的频率。
先看效果:
chen

[4, 4, 4]': 68, ‘[6, 6, 6]’: 31, ‘[4, 6, 4]’: 27, ‘[6, 4, 6]’: 24,
‘[6, 4, 4]’: 24, ‘[4, 4, 6]’: 23, ‘[6, 6, 4]’: 21, ‘[4, 6, 6]’: 19,
‘[8, 4, 4]’: 15, ‘[8, 6, 6]’: 13, ‘[6, 8, 4]’: 12, ‘[6, 6, 8]’: 10

liu:

[4, 4, 4]': 129, ‘[6, 6, 6]’: 74, ‘[4, 4, 6]’: 49, ‘[6, 4, 4]’: 43,
‘[4, 6, 6]’: 34, ‘[4, 6, 4]’: 34, ‘[6, 6, 4]’: 29, ‘[6, 4, 6]’: 25,
‘[6, 1, 6]’: 16, ‘[8, 6, 6]’: 15, ‘[6, 6, 8]’: 15, ‘[6, 8, 6]’: 15,
‘[1, 1, 1]’: 12, ‘[6, 8, 4]’: 10, ‘[3, 3, 3]’: 10, ‘[8, 4, 4]’: 10,

全部代码:

import pandas as pd

import read_excel

#获取先提供列表便于切片用


my_df = read_excel.df3
code=read_excel.get_code(my_df)

#获取指定步长序列
numbers = code.tolist()
step = 1
groups = [numbers[i:i+3] for i in range(0, len(numbers), step)]
#print(groups)

# 使用字典来统计每组数字出现的频率
frequency = {}
for group in groups:
    key = str(group)
    if key in frequency:
        frequency[key] += 1
    else:
        frequency[key] = 1

sorted_frequency = {k: v for k, v in sorted(frequency.items(), key=lambda item: item[1], reverse=True)}
print(sorted_frequency)

你可能感兴趣的:(python,python,开发语言,数据分析)