python有关list的常用方法:
- list 中if表达式的使用
- list shift移位
- list 某一列的删除
- xrange 使用
- list翻转reverse
- list按照lambda排序
直接贴代码吧,里面有注释还是比较好理解
def lst_condition():
lst = [0, 1, 0, 3]
print [a if a else 2 for a in lst] # change 0 -> 2
# [2, 1, 2, 3]
print ["ha" if i else "Ha" for i in range(3)]
# ['Ha', 'ha', 'ha']
def lst_delete_in_for_loop():
lst = [0, 1, 0, 3]
for item in lst:
print item
lst.remove(item)
def lst_shift():
"""
a basic way of shift list
:return:
"""
lst = [1, 2, 3, 4]
shift_n = 2
print lst[shift_n:] + lst[:shift_n]
# [3, 4, 1, 2]
shift_n = 1
print lst[shift_n:] + lst[:shift_n]
# [2, 3, 4, 1]
lst = [1, 2, 3, 4]
lst.append(lst.pop(0))
print lst
# [2, 3, 4, 1]
def lst_shift_efficient():
"""
a efficient way of shift list
:return:
"""
from collections import deque
de_lst = deque([1, 2, 3, 4])
de_lst.rotate(1)
print de_lst
# deque([4, 1, 2, 3])
def del_col_in_2dlst():
"""
remove certain col in 2-d list
:return:
"""
td_lst = [[1, 2, 3], [1, 2, 3]]
print [lst[1:] for lst in td_lst]
# [[2, 3], [2, 3]]
def range_xrange():
print range(0, 3)
# [0, 1, 2]
print range(3)
# [0, 1, 2]
print range(0, 7, 2)
# [0, 2, 4, 6]
print xrange(0, 3)
# xrange(3)
for i in xrange(3):
print i # 0 1 2
def lst_self_step():
# L[start:stop:step]
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in lst[::2]:
print i
# 1 3 5 7 9
for i in lst[1::2]:
print i
# 2 4 6 8 10
def lst_reverse():
lst = [1, 2, 3, 4, 5, 6]
print lst[::-1]
# [6, 5, 4, 3, 2, 1]
print lst[::-2]
# [6, 4, 2]
lst = [1, 2, 3, 4, 5, 6, 7]
print lst[:-1]
# [1, 2, 3, 4, 5, 6]
print lst[:-3]
# [1, 2, 3, 4]
print lst[:-3:-1]
# [7, 6]
print lst[:-3:1]
# [1, 2, 3, 4]
# is step is minus, firstly we reverse the lst and then get the lst by step
print lst[:3:-1]
# [7, 6, 5]
print lst[:-1:-1]
# []
print lst[:-2:-1]
# [7]
# =======================
# [1, 2, 3, 4, 5, 6, 7]
# 0 1 2 3 4 5 6
# -7 -6 -5 -4 -3 -2 -1
print lst[-7:-1]
# [1, 2, 3, 4, 5, 6]
print lst[-7:0]
# []
print lst[-7:]
# [1, 2, 3, 4, 5, 6, 7]
print lst[-5:-2]
# [3, 4, 5]
def generate_range_tuple_list(start, end, step):
"""
for example generate_range_tuple_list(0, 11, 5) ->
[(0, 5), (5, 10), (10, 11)]
:param start: for eg 0
:param end: for eg 11
:param step: for eg 5
:return: [(0, 5), (5, 10), (10, 11)]
"""
ret_lst = []
for i in xrange(start, end, step):
tp = (i, i + step if(i + step < end) else end)
ret_lst.append(tp)
return ret_lst
def create_list_repeated_n_times():
# generate list with 20 0
lst_i = [0 for i in xrange(20)]
print lst_i
lst_i_2 = [0] * 20
print lst_i_2
def sort_lst_by_item():
"""
for example lst = ['F:/0.jpg', 'F:/1.jpg', ..., 'F:/101.jpg']
in the directory, when we read it.They are sorted by str not the num in pig name
we want to read it like that:
F:/0.jpg F:/1.jpg F:/9.jpg F:/10.jpg
not
F:/0.jpg F:/10.jpg F:/9.jpg
:return:
"""
import glob
import os
img_dir = 'F:/ad_samples/img_voice_test/tencent_img/tencent_ocr_sample/'
lst = glob.glob(img_dir + "*.jpg")
# os.path.splitext(x)[0].split('\\')[-1] get num '9' from F:/9.jpg
lst_sorted = sorted(lst, key=lambda x: int(os.path.splitext(x)[0].split('\\')[-1]))
return lst_sorted
工作中使用的语言比较多写过C++,java, 部分html+js, python的.由于用到语言的间歇性,比如还几个月没有使用python了许多技巧就忘记了,于是我把一些常用的python代码分类项目在本人的github中,当实际中用到某一方法的时候就把常用的方法放到一个文件中方便查询。
实际工作中用到某一些方法的时候基本使用英文关键词google查询,很多技巧大多数是Stack Overflow上面的,本人也做了一定的修改。
比如本文部分方法google:python list if expression, python list shift, python files list sorted by num.得到的结果都是经验丰富的程序员回答的结果很好,从中可以学习到很多技巧,也十分节省时间,发现工作中很多程序员基本用百度中文搜索,这样不是不好,只不过相对于google 英文来说效果,大多数结果确实差不少,而且不如 Stack Overflow 精准。
平时由于为了方便,我会把自己日常的一些工作内容分类然后存到WPS云文档之中这样看起来还是很方便
这些也上传到了我的github感兴趣的可以看下