python3 编程使用技巧

Posted alex0111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 编程使用技巧相关的知识,希望对你有一定的参考价值。

 

  • from random import randint 
  • data = {"Student{}".format(i):randint(60,100) for i in range(1,21)}

 

  1. 如何在list中筛选数据
    [x for x in arr if x >=60]

     

  2. 如何在set中筛选数据
    [x for x in s if x >=60]

     

  3. 如何在dict中筛选数据
    {k:v for k,v in data.items() if v >= 60}
    filter(lambda x:x[1] >= 90,data.items())

     

  4. 给typle中的元素命名
     1 >>> s = (Jim, 16, male, [email protected])
     2 >>> from enum import IntEnum
     3 >>> class StudentEnum(IntEnum):
     4 ...     NAME = 0
     5 ...     AGE = 1
     6 ...     SEX = 2
     7 ...     EMAIL = 3
     8 ...
     9 >>> StudentEnum.NAME
    10 <StudentEnum.NAME: 0>
    11 >>> s[StudentEnum.NAME]
    12 Jim
    13 >>> s[StudentEnum.AGE]
    14 16

    nameTuple

    技术分享图片
     1 >>> from collections import namedtuple
     2 >>> namedtuple(Student, [name, age, sex, email])
     3 <class __main__.Student>
     4 >>> SS = namedtuple(Student, [name, age, sex, email])
     5 >>> SS
     6 <class __main__.Student>
     7 >>> s1 = SS(Jim,16, male, email=[email protected])
     8 >>> s1
     9 Student(name=Jim, age=16, sex=male, email=[email protected])
    10 >>> s1.name
    11 Jim
    View Code

     

  5. dict中元素进行排序
     1 >>> from random import randint
     2 >>> d = {k: randint(60,100) for k in abcdefgh}
     3 >>> d
     4 {a: 70, b: 85, c: 81, d: 89, e: 82, f: 69, g: 64, h: 84}
     5 >>> [(v,k)for k,v in d.items()]
     6 [(70, a), (85, b), (81, c), (89, d), (82, e), (69, f), (64, g), (84, h)]
     7 >>> l = [(v,k)for k,v in d.items()]
     8 >>> sorted(l)
     9 [(64, g), (69, f), (70, a), (81, c), (82, e), (84, h), (85, b), (89, d)]
    10 >>> sorted(l, reverse = True)
    11 [(89, d), (85, b), (84, h), (82, e), (81, c), (70, a), (69, f), (64, g)]
    12 >>> dict(sorted(l, reverse = True))
    13 {89: d, 85: b, 84: h, 82: e, 81: c, 70: a, 69: f, 64: g}
    14 >>> sorted(d.items(), key=lambda item: item[1], reverse=True)
    15 [(d, 89), (b, 85), (h, 84), (e, 82), (c, 81), (a, 70), (f, 69), (g, 64)]
     1 >>> res = sorted(d.items(), key=lambda item: item[1], reverse=True)
     2 >>> res
     3 [(d, 89), (b, 85), (h, 84), (e, 82), (c, 81), (a, 70), (f, 69), (g, 64)]
     4 >>> enumerate(res,1)
     5 <enumerate object at 0x1079de558>
     6 >>> list(enumerate(res,1))
     7 [(1, (d, 89)), (2, (b, 85)), (3, (h, 84)), (4, (e, 82)), (5, (c, 81)), (6, (a, 70)), (7, (f, 69)), (8, (g, 64))]
     8 >>> for i, (k,v) in enumerate(res,1):
     9 ...   print(i,k,v)
    10 ...
    11 1 d 89
    12 2 b 85
    13 3 h 84
    14 4 e 82
    15 5 c 81
    16 6 a 70
    17 7 f 69
    18 8 g 64
    19 >>> d
    20 {a: 70, b: 85, c: 81, d: 89, e: 82, f: 69, g: 64, h: 84}
    21 >>> for i, (k,v) in enumerate(res,1):
    22 ...     d[k] = (i,v)
    23 ...
    24 >>> d
    25 {a: (6, 70), b: (2, 85), c: (5, 81), d: (1, 89), e: (4, 82), f: (7, 69), g: (8, 64), h: (3, 84)}
    26 >>> {k:(i,v) for i,(k,v) in enumerate(res, 1)}
    27 {d: (1, 89), b: (2, 85), h: (3, 84), e: (4, 82), c: (5, 81), a: (6, 70), f: (7, 69), g: (8, 64)}

     

     

  6. 关于zip
    >>> zip([1,2,3],[3,4,5,6])
    <zip object at 0x1079df348>
    >>> list(zip([1,2,3],[3,4,5,6]))
    [(1, 3), (2, 4), (3, 5)]
    >>> list(zip([1,2,3],[3,4]))
    [(1, 3), (2, 4)]

     

  7. 词频统计
    word_list = re.split("W+", txt)
    c2 = Counter(word_list)
    c2.most_common(10)

     

 


以上是关于python3 编程使用技巧的主要内容,如果未能解决你的问题,请参考以下文章

scrapy主动退出爬虫的代码片段(python3)

使用 Pygments 检测代码片段的编程语言

如何在 Django Summernote 中显示编程片段的代码块?

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

python常用代码片段总结