文章目录

  • 字典
  • 1. 两个等长列表转字典
  • 2. 输出字典最大value对应的key
  • python
  • 获取numpy数组中某个元素的出现次数


字典

1. 两个等长列表转字典
list1 = [1,2,3]
list2 = ['a', 'b', 'c']
dic = dict(zip(list1, list2))
2. 输出字典最大value对应的key
max(dic, key=dic.get)

python

  • python3调试时input()用法:
n = int(input())
# 如果需要依次读入n行数据,分别处理,还需要加上
while n > 0:
   data = input()  # 读进来是字符串
   
   n -= 1  # 一定要加这个,否则陷入死循环

如果一次输入好几个整数,使用map函数

n, m = map(int, input().split())  # 读入两个整数,
nums = list(map(int, input().split())) # 将读入的数据用空格分割,转成int型,再存到数组里
  • 字符串按照长度排序,例如,字符串”abc“的全组合,[‘a’, ‘ab’, ‘abc’, ‘ac’, ‘b’, ‘bc’, ‘c’],想要先按照长度顺序排序,相同长度内部再按照字母顺序排序,可以使用sorted()或者list.sort()函数对列表进行排序,
list1 = ['a', 'ab', 'abc', 'ac', 'b', 'bc', 'c']
res = sorted(list1, key = lambda i:len(i), reverse = False) # key表示排序规则,即按长度排, reverse = False 升序
print(res)  # ['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']
  • numpy转换数据类型 image.astype(np.uint8)
  • [::-1]表示倒序
  • 显示彩色图像的前两维(即灰度图像):image[:,:,0]
  • 指定工作目录
import os
import sys
os.chdir(WORKDIR)  #改变当前工作目录到指定的路径
  • 获取当前目录的两种方法:
① cur_dir = os.getcwd() 
 ② cur_dir = os.path.dirname(__file__)  
   print(cur_dir)
  • 图片的类型转换:
    从cv2.imread()读取的图片是uint8格式,[0-255]范围之间,进行类似 255. * 2 - 1的操作,就是从uint8—>float64,变成float之后就可以做运算,float64取值范围是[-1,1],float32的取值范围是[0,1],uint8的取值范围是[0,255]
image = cv2.imread("F:\\demo\\1.jpg") 
print(image.dtype) #uint8

image2 = image / 255. * 2  - 1
print(image2.dtype) #float64
  • tensor和array的相互转换
tensor转array:tensor.eval() #(必须在会话中)
 array转tensor:tf.convert_to_tensor(array)
  • 矩阵拼接: 先扩充维度tf.expand_dims(a,axis=0)
    再使用tf.concat([a,a],axis=0) 拼接两个a到第0维。
  • 写入文件使用:
with open("output.txt","w+") as f:
	f.write(parms) 
	#写入的要求是str格式,如果是array,则使用f.write(str(parms))转成str即可。
  • array数组中重要的几个ndarray对象属性(参考链接):
    ndarray.ndim:数组的维数(即数组轴的个数)
    ndarray.shape:数组的维度
    ndarray.size:数组元素的总个数
    ndarray.dtype:表示数组中元素类型的对象
    ndarray.itemsize:数组中每个元素的字节大小
    ndarray.data:包含实际数组元素的缓冲区
  • np.genfromtxt(gt_file, delimiter=’\t’) 读取空格分割的文件(注意:不能用’ ‘)
    np.genfromtxt(gt_file, delimiter=’,’) 读取逗号分割的文件
  • break只跳出最内层循环,不会跳出多层循环的。
  • list,dict和tuple的类型都可以使用type()函数来查看,dtype(name)即可~
  • 注意数字0和字符’0’的区别,‘0’ !=0

产生0-1随机数:coin = tf.to_float(tf.random_uniform([1]))[0]

 
数据的numpy和list

import numpy as np
d = {'cls1':[1,2,3,4], 'cls2':[[2,3,4,5],[3,4,5,6]]}
for key in d:
    annotations = d[key]
    print(type(annotations), annotations)
    
#输出:
<class 'list'> [1, 2, 3, 4]
<class 'list'> [[2, 3, 4, 5], [3, 4, 5, 6]]

list是没有shape的,如果改成numpy会变成:

for key in d:
    annotations = np.array(d[key])
    print(type(annotations), annotations, annotations.shape)
#输出:
<class 'numpy.ndarray'> [1 2 3 4] (4, )
<class 'numpy.ndarray'> [[2 3 4 5] [3 4 5 6]] (2, 4)

python中的lstrip用法——用于截掉字符串左边的空格或指定字符。

str.lstrip([chars])

python格式化输出保留2位小数:
三种方法:

print(" %.2f" % 0.222222)
print("{:.2f}".format(0.222222))
print(round(0.222222, 2))

获取numpy数组中某个元素的出现次数

举个栗子:

对于mask数组

python字典用input赋值 python用input输入字典_python

想要统计mask中1的分布,可以使用:sum(mask == 1),即可输出:

python字典用input赋值 python用input输入字典_数组_02

完整代码:

def _create_gt_mask(shape):
    h, w = shape
    y = np.arange(h, dtype=np.float32) - (h - 1) / 2. # np.arange用于创建等差数组
    x = np.arange(w, dtype=np.float32) - (w - 1) / 2.
    # print(y, x) # [-4. -3. -2. -1.  0.  1.  2.  3.  4.] [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
    y, x = np.meshgrid(y, x) # 生成网格矩阵
    dist = np.sqrt(x ** 2 + y ** 2)
    mask = np.zeros((h, w))
    mask[dist <= 2] = 1
    mask = mask[np.newaxis, :, :]

    print('mask', mask)
    print(sum(mask == 1))
    print(sum(mask == 0))
    return mask.astype(np.float32)

shape = [9, 9]
res = _create_gt_mask(shape)