zoukankan      html  css  js  c++  java
  • python小技巧

    同时遍历两个列表

    1 nfc = ["Packers", "49ers"]
    2 afc = ["Ravens", "Patriots"]
    3 for i,j in zip(nfc,afc):
    4     print(i,j)
    Packers Ravens
    49ers Patriots

    列表转换成字符串

    1 teams = ["Packers", "49ers", "Ravens", "Patriots"]
    2 print(','.join(teams))
    3 type(','.join(teams))
     
    Packers,49ers,Ravens,Patriots
    str

    遍历列表同时得出序号和内容

    1 teams = ["Packers", "49ers", "Ravens", "Patriots"]
    2 for index, team in enumerate(teams):
    3     print(index, team)
    0 Packers
    1 49ers
    2 Ravens
    3 Patriots
    
     

    从字典中获得值

    In [10]:
     
     
     
     
     
    data = {'user': 1, 'name': 'Max', 'three': 4}
    is_admin = data.get('admin', False)
    print(is_admin)
     
     
     
    False
    
     

    Collections模块

     
      1 from collections import Counter
     2 print(Counter('hello')) 
     
    Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
    
     

    Itertools模块

     
     
    1 from itertools import combinations
    2   
    3 teams = ["Packers", "49ers", "Ravens", "Patriots"]
    4 for game in combinations(teams, 2):
    5     print(game)
    6   
    ('Packers', '49ers')
    ('Packers', 'Ravens')
    ('Packers', 'Patriots')
    ('49ers', 'Ravens')
    ('49ers', 'Patriots')
    ('Ravens', 'Patriots')
  • 相关阅读:
    keepalived的一些。。
    virtualbox复制了以后网卡启动不了。
    mysql安装之后需要调的参数
    mysql5.6 thread pool
    $releasever 不正确解析
    linux 被入侵后扫尾工作
    简单启动脚本编写
    tcmalloc安装
    rsyslog及loganalyzer
    nsswitch & pam
  • 原文地址:https://www.cnblogs.com/songlin123/p/10776852.html
Copyright © 2011-2022 走看看