ブルーブリッジカップ25日目(Python)(ルーキーチュートリアルの知識ポイントの復習)

目次

1. 機能モジュール

1. 変更可能 (可変) オブジェクトと不変 (イミュータブル) オブジェクト (つまり、関数内で Global を宣言する必要があるかどうか)

 2. 無名関数

3.フォーマット機能

 2.Pythonのデータ構造

1.リスト方式

 2.メソッドの作成

3. 辞書の組み込み関数とメソッド

 4. コレクションの組み込みメソッド

 5. オペレーター

3. Python3標準ライブラリの概要

 1.日時ライブラリ

2. 数学ライブラリ

3. コレクション # キュー

4.itertools # 順列と組み合わせ

5.heapq #スモールトップヒープ()

6.functools # カスタム比較関数 -1 は変更せず、1 は交換

7.sys

4. Python 組み込み関数 


1. 機能モジュール

1. 変更可能 (可変) オブジェクトと不変 (イミュータブル) オブジェクト (つまり、関数内で Global を宣言する必要があるかどうか)

 2. 無名関数

3.フォーマット機能

  塗りつぶしには位置合わせを指定する必要があります

a = 5.5
print("a的值为{0:.0f}".format(a))
# 执行结果
a的值为6

print("{0:5}---{1:<6d}".format("张三",18))
print("{0:<5}---{1:6d}".format("张三",18))
print("{1:5d}---{0:6}".format("张三",18))
# 执行结果
张三   ---18    
张三   ---    18
   18---张三 

print("{0:,}".format(10000))
# 执行结果
10,000


print("{0:.3%}".format(0.24))
# 执行结果
24.000%

 2.Pythonのデータ構造

1.リスト方式

 2.メソッドの作成

辞書 a = dict{ }

リスト a=[ ]  

タプル a=(b,) a=tuple()

set a={ } a=set( )

3. 辞書の組み込み関数とメソッド

 4. コレクションの組み込みメソッド

 5. オペレーター

3. Python3標準ライブラリの概要

 1.日時ライブラリ

>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368

datetime.timedelta(1)  # 以1为递增

day1=datetime.date(2001,2,2)
day2=datetime.date(2001,2,28)
delta=day2-day1
#print(delta)  #26 days, 0:00:00
print(delta.days)  #26
print(day1.day)  # 当前月的第几日
print(day1.isoweekday()) # 判断星期几

2. 数学ライブラリ

3. コレクション # キュー

import collections
deque=collections.deque()

4.itertools # 順列と組み合わせ

import itertools
itertools.combinations(iter,2)  # 组合 

itertools.permutations(iter,2)  #排列


from itertools import combinations
a = ['h', 'y', 'k', 'q', 's']
    for i in combinations(a, 2):
        print(i)

(‘h’, ‘y’)
(‘h’, ‘k’)
(‘h’, ‘q’)
(‘h’, ‘s’)
(‘y’, ‘k’)
(‘y’, ‘q’)
(‘y’, ‘s’)
(‘k’, ‘q’)
(‘k’, ‘s’)
(‘q’, ‘s’)

5.heapq #スモールトップヒープ()

import heapq  # 导入堆
def dij(s):
    done=[0 for i in range(n+1)]  # 记录是否处理过
    hp=[]  #堆
    dis[s]=0
    heapq.heappush(hp,(0,s))   #入堆,小顶堆
    while hp:
        u=heapq.heappop(hp)[1] #出堆元素结点
        if done[u]: #当前结点处理过
            continue
        done[u]=1
        for i in range(len(G[u])): #遍历当前结点的邻居
            v,w =G[u][i]
            if done[v]:continue
            dis[v]=min(dis[v],dis[u]+w)  # 更新当前结点邻居的最短路径
            heapq.heappush(hp,(dis[v],v))
 
 
 
 
n,m = map(int,input().split())#
s=1  # 从1开始访问
G=[[]for i in range(n+1)]   #邻接表存储
inf = 2**50
dis = [inf]*(n+1) #存储距离
for i in range(m):# 存边,这里是单向边
    u,v,w = map(int,input().split())
    G[u].append((v,w))  #记录结点u的邻居和边长
 
dij(s)
for i in range(1,n+1):
    if dis[i]==inf:
        print("-1",end=' ')
    else:
        print(dis[i],end=' ')

6.functools # カスタム比較関数 -1 は変更せず、1 は交換

import functools
def cmp(x,y):
    pass
    # return -1 不变,return 1 交换x,y位置


sorted(iter,key=functools.cmp_to_key(cmp))

7.sys

import sys
sys.setrecursionlimit(300000)  #设置最大递归深度,DFS一定要弄

sys.exit()  # 退出程序

4. Python 組み込み関数 

Supongo que te gusta

Origin blog.csdn.net/weixin_52261094/article/details/129993741
Recomendado
Clasificación