对于多个数组的合并及按序排列

博客提及了排序,排序在信息技术领域是重要操作,可用于数据整理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

trans_out_res.txt
[67545 69287 82972 70845 80996]
[142522  90580  97826  97562  72554]
[302981 254754 308544 273743 134552]
[139220  75252  72992  12517  90535]
[ 31490 299477 299478 233707 316110]



list_res = []
with open("../TransH/trans_out_res.txt", 'r', encoding='utf-8') as fo:
    sentence = fo.readline()
    while sentence:
        sentence = sentence.replace("\n", "")
        sentence = sentence.replace("]", "")
        sentence = sentence.replace("[", "")
        tmp = sentence.split()
        for num in tmp:
            list_res.append(int(num))
        sentence = fo.readline()

print(list_res)

排序:

# 将数字和对应重复次数作为key,value放入字典
a = {}
for i in list_res:
    if list_res.count(i) >= 1:
        a[i] = list_res.count(i)
        # 对字典value(重复次数)降序排序,以列表形式返回
a = sorted(a.items(), key=lambda item: item[1], reverse=True)
# [(5, 6), (1, 3), (2, 2), (4, 2)]
num = []
# 获取列表中每个元组中的第一个值,即数组中的数字(此时已是按重复次数降序)
for item in a:
    num.append(item[0])
print(num)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值