Skip to content

jia-zh/Notes-for-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Notes for Python

Some small code knowledge points I often encounter

Some Code for List

  1. 字符串list按照长度排序
my_list = ['青海省', '内蒙古自治区', '西藏自治区', '新疆维吾尔自治区', '广西壮族自治区']  
order_list = sorted(my_list, key = lambda i:len(i),reverse=True)  
print(order_list) 
******************
['新疆维吾尔自治区', '广西壮族自治区', '内蒙古自治区', '西藏自治区', '青海省']  
******************
  1. 数字list连续分片
from itertools import groupby
my_list = [1, 2, 3, 5, 6, 7, 9, 11, 12, 13, 14]
fun = lambda x: x[1] - x[0]
for k, g in groupby(enumerate(my_list), fun):
    cont_list = [j for i, j in g]
    print(cont_list)
******************
[1, 2, 3]
[5, 6, 7]
[9]
[11, 12, 13, 14]
******************
  1. 遍历list提取下标和值
my_list = ['apple', 'oppo', 'vivo']
for index,value in enumerate(my_list):
    print(index,value)
******************
0 apple
1 oppo
2 vivo
******************

Some Code for Dict

  1. dict排序
my_dict = {'am': 5, 'is': 7, 'are': 3}
my_list = sorted(my_dict.items(), key=lambda d: d[1], reverse=True)
for word in my_list:
    print(word[0] + "\t" + str(word[1]))
print("******")
my_list = sorted(my_dict.items(), key=lambda d: d[0], reverse=True)
for word in my_list:
    print(word[0] + "\t" + str(word[1]))
******************
is	7
am	5
are	3
******
is	7
are	3
am	5
******************

About

Some small code knowledge points I often encounter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published