zoukankan      html  css  js  c++  java
  • 封装好的函数

    return是在函数的倒数第一层

    list2=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,'余1','余2','余3']
    #===================deleteTopPencent5,再给个input数组============================#
    numDelete=19#每份5%,删num份;num取值0~20
    def deleteTopOfList(list,num):
        if(num==20):#考虑到有余数,那就删完吧~
            list=[]
        else:
            length=len(list)
            fen=length//20  #每份大小5%
            for i in range(fen*num):
                list.pop(0)
        return list
    resultList=deleteTopOfList(list2,numDelete)
    print(len(resultList))
    #===================deleteTopPencent5============================#
    
    
    
    
    
    
    
    
    
    #封装函数
    #imgs--所有训练数据的图片名称--type:数组
    #noisyIndex--噪声图像的数组序号--type:数组
    #Totaldict--所有训练数据的图片&噪声标签的映射关系--type:字典
    imgs=['0ADI-AAAMHQMK.tif', '1ADI-AACCGLYD.tif', '2ADI-AACVGRFT.tif', '3ADI-AADGNDRG.tif', '4ADI-AAEKWPVP.tif', '5ADI-AAGDEQEK.tif', '6ADI-AAGQKKEG.tif', '7ADI-AAGSEPQK.tif']
    noisyIndex=['1','4','6','7']
    Totaldict = {'0ADI-AAAMHQMK.tif':0, '1ADI-AACCGLYD.tif':1, '2ADI-AACVGRFT.tif':2, '3ADI-AADGNDRG.tif':3, '4ADI-AAEKWPVP.tif':4, '5ADI-AAGDEQEK.tif':5, '6ADI-AAGQKKEG.tif':6, '7ADI-AAGSEPQK.tif':7}
    def cleanNoisyImages(imgs,noisyIndex,Totaldict):
        for index in noisyIndex: 
            index=int(index)
            print(index)
            del Totaldict[imgs[index]]
        return Totaldict
    result=cleanNoisyImages(imgs,noisyIndex,Totaldict)
    print(result)
    
    
    import random
    key = 'abcde'
    value = range(1, 6)
    dictName=dict(zip(key, value))#{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
    def random_dic(dicts):#打乱字典,映射关系不变==================#
        dict_key_ls = list(dicts.keys())#取键值keys,转化为list才能shuffle
        random.shuffle(dict_key_ls)
        new_dic = {}                    #创建空字典
        for key in dict_key_ls:
            new_dic[key] = dicts.get(key)#打乱后的key对应的value 赋值给空字典,结果有一位小数   ##一定是key?? 
        return new_dic      
    print(random_dic(dictName))        
    
    
    
    
    arrxx=[1.0, 8.0, 4.0, 1.0, 1.0, 2.0, 8.0, 5.0, 2.0]
    def intList(arr):  #list里面的元素转化为整数================#
        arr_temporary=[]
        for item in arr:
            item=int(item)
            arr_temporary.append(item)
        return(arr_temporary)
    
    
    #=========================
    def SaveList(List, savepath):    #列表里的元素按行存起来,一个元素一行,一个元素一行  
        filename = open(savepath, 'w')  
        for value in List:  
            filename.write(str(value)+'\n') 
        filename.close()  
    
    def ReadList(readpath):
        f= open(readpath,"r")   
        List = f.read().splitlines()   
        f.close() 
        return List
    
    
  • 相关阅读:
    [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors
    [Swift]LeetCode822. 翻转卡片游戏 | Card Flipping Game
    [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character
    [Swift]LeetCode818. 赛车 | Race Car
    [Swift]LeetCode817. 链表组件 | Linked List Components
    [Swift]LeetCode816. 模糊坐标 | Ambiguous Coordinates
    [Swift]LeetCode815. 公交路线 | Bus Routes
    [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning
    [Objective-C语言教程]指针(15)
    转 : net use的使用
  • 原文地址:https://www.cnblogs.com/icemiaomiao3/p/15573238.html
Copyright © 2011-2022 走看看