Home > Python > Main text

Python element list


Tag: python, list, 列表


tricks


sort list in zip to keep relation order stackoverflow

>>> 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')

sort list of str or number

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)

find index for a value

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]

sum of list of list elements

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

flat nested list

def ls_ls_flat(ls_ls):
    return list(itertools.chain.from_iterable(ls_ls))

convert value list to percentage list

# 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

remove NA value of a list

def list_remove_na(ls):
	return [i for i in ls if not np.isnan(i)]

shuffle a list with seed

参考这里

>>> 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]

sort a list based on anther list order

参考这里

# ref: 需要参考的顺序list
# input:需要排序的list
[x for x in Ref if x in Input]

qucik cheatsheet

source: Python Crash Course - Cheat Sheets

beginners_python_cheat_sheet_pcc_lists1.png

beginners_python_cheat_sheet_pcc_lists2.png



If you link this blog, please refer to this page, thanks!
Post link:https://tsinghua-gongjing.github.io/posts/python_list.html

Previous: RNA labs