列表去重

以前经常用遍历或者科学计算库等方法,其实有一个很简单的技巧,那就是利用集合的互异性

  1. >>> a
  2. [0, 1, 2, 3, 1]
  3. >>> b = set(a)
  4. >>> b
  5. {0, 1, 2, 3}
  6. >>> list(b)
  7. [0, 1, 2, 3]

列表中去除重复字典

对嵌套的字典无效
[dict(t) for t in set([tuple(d.items()) for d in the_list])]

  1. li = [dict(t) for t in set([tuple(d.items()) for d in li])]

修改删除列表部分值

  1. >>> a = [1, 2, 3, 4, 5]
  2. >>> a[2:3] = [0, 0]
  3. >>> a
  4. [1, 2, 0, 0, 4, 5]
  5. >>> a[1:1] = [8, 9]
  6. >>> a
  7. [1, 8, 9, 2, 0, 0, 4, 5]
  8. >>> a[1:-1] = []
  9. >>> a
  10. [1, 5]

压缩和解压缩

  1. # 压缩
  2. import zipfile
  3. import os
  4. def zipDir(dirpath,outFullName):
  5. """
  6. 压缩指定文件夹
  7. :param dirpath: 目标文件夹路径
  8. :param outFullName: 压缩文件保存路径+xxxx.zip
  9. :return: 无
  10. """
  11. zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
  12. for path,dirnames,filenames in os.walk(dirpath):
  13. # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
  14. fpath = path.replace(dirpath,'')
  15. for filename in filenames:
  16. zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
  17. zip.close()
  18. print(1)
  19. startdir = "MathAmino/train_models" #要压缩的文件夹路径
  20. file_news = 'mytrain.zip' # 压缩后文件夹的名字
  21. zipDir(startdir,file_news)
  22. # 解压缩
  23. # 压缩文件
  24. import zipfile
  25. f = zipfile.ZipFile("/home/kesci/MathAmino.zip",'r')
  26. for file in f.namelist():
  27. f.extract(file,"/home/kesci/work/")

列表组合为字典

  1. >>> a = [1, 2, 3]
  2. >>> b = ['a', 'b', 'c']
  3. >>> z = zip(a, b)
  4. >>> z
  5. [(1, 'a'), (2, 'b'), (3, 'c')]
  6. >>> zip(*z)
  7. [(1, 2, 3), ('a', 'b', 'c')]

两个一一对应列表转化为字典

  1. >>> a = [1, 2, 3]
  2. >>> b = ['a', 'b', 'c']
  3. >>> z = zip(a, b)
  4. >>> z
  5. [(1, 'a'), (2, 'b'), (3, 'c')]
  6. >>> d = dict(z)
  7. >>> d
  8. {1: 'a', 2: 'b', 3: 'c'}

翻转字典

  1. >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  2. >>> m.items()
  3. [('a', 1), ('c', 3), ('b', 2), ('d', 4)]
  4. >>> zip(m.values(), m.keys())
  5. [(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
  6. >>> mi = dict(zip(m.values(), m.keys()))
  7. >>> mi
  8. {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

集合操作

  1. >>> A = {1, 2, 3, 3}
  2. >>> A
  3. set([1, 2, 3])
  4. >>> B = {3, 4, 5, 6, 7}
  5. >>> B
  6. set([3, 4, 5, 6, 7])
  7. >>> A | B
  8. set([1, 2, 3, 4, 5, 6, 7])
  9. >>> A & B
  10. set([3])
  11. >>> A - B
  12. set([1, 2])
  13. >>> B - A
  14. set([4, 5, 6, 7])
  15. >>> A ^ B
  16. set([1, 2, 4, 5, 6, 7])
  17. >>> (A ^ B) == ((A - B) | (B - A))
  18. True

在字符串中找指定字符串位置

使用list的index方法可以找到list中第一次出现该元素的位置
>>> l ``= [``'a'``,``'b'``,``'c'``,``'c'``,``'d'``,``'c'``]
>>> find``=``'b'
>>> l.index(find)
1

找出出现该元素的所有位置可以使用一个简单的表理解来实现
>>> find ``= 'c'
>>> [i ``for i,v ``in enumerate``(l) ``if v``=``=``find]
[``2``, ``3``, ``5``]

列表倒序

  1. >>> x = [1,5,2,3,4]
  2. >>> x.reverse()
  3. >>> x
  4. [4, 3, 2, 5, 1]

找出列表中最大或最小的三个

  1. # -*- coding: utf-8 -*-
  2. import heapq
  3. nums = [1, 8, 2, 23, 7, -4, 18, 23, 24, 37, 2]
  4. # 最大的3个数的索引
  5. max_num_index_list = map(nums.index, heapq.nlargest(3, nums))
  6. # 最小的3个数的索引
  7. min_num_index_list = map(nums.index, heapq.nsmallest(3, nums))
  8. print(list(max_num_index_list))
  9. print(list(min_num_index_list))

列表平均分为n份

  1. # listTemp 为列表 分成每份n个元素的列表
  2. def func(listTemp, n):
  3. for i in range(0, len(listTemp), n):
  4. yield listTemp[i:i + n]
  5. 平均分为n
  6. def average_func(m, n):
  7. f = False
  8. s = len(m) // n
  9. lef = len(m) % n
  10. lop = 0
  11. stopat = 0
  12. if lef != 0:
  13. s += 1
  14. f = True
  15. ret = []
  16. if f:
  17. for i in range(lef):
  18. ret.append(m[i*s:(i+1)*s])
  19. stopat = i*s+1
  20. lop = i
  21. s -= 1
  22. for i in range(1, n-lop):
  23. ret.append(m[stopat+i*s:stopat+(i+1)*s])
  24. return ret
  25. else:
  26. for i in range(n):
  27. ret.append(m[i*s:(i+1)*s])
  28. return ret

统计列表中重复元素的个数

  1. >>> from collections import Counter
  2. >>> Counter([1,2,2,2,2,3,3,3,4,4,4,4])
  3. Counter({2: 4, 4: 4, 3: 3, 1: 1})

查看当前文件夹中文件

  1. for (dirpath, dirnames, filenames) in os.walk("try_test"):
  2. print(dirpath, dirnames, filenames)

存格式化的json文件

  1. json_str = json.dumps(your_list, ensure_ascii=False, indent=4) # 缩进4字符
  2. with open(your_path, 'w') as json_file:
  3. json_file.write(json_str)

读取json文件

  1. with open("../config/record.json",'r') as load_f:
  2. load_dict = json.load(load_f)