>>> list1 = [3,2,4,1, 1]
>>> list2 = ['three', 'two', 'four', 'one', 'one2']
>>> list1, list2 = zip(*sorted(zip(list1, list2)))
>>> list1
(1, 1, 2, 3, 4)
>>> list2
('one', 'one2', 'two', 'three', 'four')
def sort_str_num_ls(ls=[1,2,3]):
if isinstance(ls[0],int):
return sorted(ls)
if isinstance(ls[0],str):
try:
return map(str,sorted([int(i) for i in ls]))
except:
return sorted(ls)
def find_all_value_index_in_list(lst=[1,2,3,4,5,1],f=1):
return [i for i, x in enumerate(lst) if x == f]
def list_list_sum(lists=[[1,2],[3,4]],mode='count_sum'):
if mode == 'count_sum':
total=sum(sum(ls) for ls in lists)
if mode == "len_sum":
total=sum(len(ls) for ls in lists)
return total
def ls_ls_flat(ls_ls):
return list(itertools.chain.from_iterable(ls_ls))
# list to percent list
def list_pct(ls):
ls=map(float,ls)
ls_sum=sum(ls)
ls_pct=[i/ls_sum for i in ls]
return ls_pct
def list_remove_na(ls):
return [i for i in ls if not np.isnan(i)]
参考这里:
>>> import random
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
参考这里:
# ref: 需要参考的顺序list
# input:需要排序的list
[x for x in Ref if x in Input]
source: Python Crash Course - Cheat Sheets