2022-12-15 一些简单的numpy数组操作的代码草稿

This article is categorized as "Garbage" . It should NEVER be appeared in your search engine's results.

我也不知道这些代码当初是拿来干什么的,似乎是随手写的一些numpy数组操作脚本

2024-02-10,重新跑了一遍,把jupyter notebook的结果放在上面(相比于原始代码略有修改):

原始代码:

import numpy as np
import utils as ut
import random
from copy import deepcopy

# a=np.array([[1,2,3,4],
#             [5,6,7,8]])
#
# for i in range(0,a.shape[1]):
#     print(a[:,i])


# a=np.array([[1,2,3],
#            [2,4,5],
#             [5,6,7]])
# b=np.array([15,5,5])
# b1=b.reshape(1,-1)
# c=np.concatenate((b1,a),axis=0)
# print('done')
#
# a=np.array([[1,2,3],
#            [2,4,5],
#             [5,6,7]])
#
# for col in range(1, a.shape[1]):
#     print()


# def highest_freq(lst):
#     counter={}
#     for item in lst:
#         if item in counter.keys():
#             counter[item]+=1
#         else:
#             counter[item]=1
#
#     maxC=-1
#     mostElement=None
#     for key in counter.keys():
#         if counter[key]>maxC:
#             mostElement=key
#             maxC=counter[key]
#     return mostElement
#
# lst = [1,1,1,1,2,2,2]
# print(highest_freq(lst))
# print(lst[:5])



# a=np.empty((3,10))
# b=np.array([[1,2],[4,5],[5,6]])
# start_index=0
# a[:,start_index:start_index+b.shape[1]]=b
# print('done')


#
# y=np.array([
#     [1,2,3.1],
#     [3,4,100],
#     ['喊',3,5]
# ])
#
#
# print(np.issubdtype(y.dtype, np.integer))


def highest_freq_helper(reverse_counter,item):
    for key in reverse_counter.keys():
        for value in reverse_counter[key]:
            if value == item:
                reverse_counter[key].remove(item)
                if (key + 1) in reverse_counter.keys():
                    reverse_counter[key + 1].append(item)
                else:
                    reverse_counter[key + 1] = [item]
                return deepcopy(reverse_counter)

def highest_freq(lst):
    reverse_counter = {0:list(set(lst))}

    # 遍历每一个list元素
    for item in lst:
        reverse_counter=highest_freq_helper(reverse_counter,item)

    # print('debug here')
    # print(reverse_counter)
    max_count=-1
    for key in reverse_counter.keys():
        if key>max_count:
            max_count=key

    # print(max_count)
    print(random.choice(reverse_counter[max_count]))


highest_freq([1,2,3,4,5,5,4,1])

 Last Modified in 2024-02-10 

Leave a Comment Anonymous comment is allowed / 允许匿名评论