国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

優(yōu)化型網(wǎng)站建設(shè)廈門網(wǎng)站優(yōu)化

優(yōu)化型網(wǎng)站建設(shè),廈門網(wǎng)站優(yōu)化,安美東莞網(wǎng)站建設(shè),gta5網(wǎng)站建設(shè)Python基礎(chǔ) 語法拾遺List與Tuple的區(qū)別yield關(guān)鍵字for in enumeratefor in zip 精彩片段測量程序用時 語法拾遺 List與Tuple的區(qū)別 ListTuple建立后是否可變可變不可變建立后是否可添加元素可添加不可添加 # list and tuple List [1, 2, 3, 4, 5] Tuple (1, 2, 3, 4, 5) p…

Python基礎(chǔ)

  • 語法拾遺
      • List與Tuple的區(qū)別
      • yield關(guān)鍵字
      • for in enumerate
      • for in zip
  • 精彩片段
      • 測量程序用時

語法拾遺

List與Tuple的區(qū)別

ListTuple
建立后是否可變可變不可變
建立后是否可添加元素可添加不可添加
# list and tuple
List = [1, 2, 3, 4, 5]
Tuple = (1, 2, 3, 4, 5)
print(List)
print(Tuple)def change_element():"""# diff1list中元素,建立后可改變tuple中元素,建立后不可改變"""print("【1.change element】")List[0] = 0# Tuple[0] = 0 # errorprint(List)print(Tuple)def add_element():"""# diff2list可添加元素tuple不可添加元素"""print("【2.add element】")List.append(6)print(List)print(Tuple)def resize():l2 = List + Listt2 = Tuple + Tupleprint(l2)print(t2)def nest():List[0] = [6, 8, 10]# Tuple[0] = (6,8,10) # errorprint(List)tuple = (1, 2, 3, 4, (5, 6, 7, 8))print(tuple)def in_and_notin():print("1 in", List, "is", 1 in List)print("1 in", Tuple, "is", 1 in Tuple)print("100 not in", List, "is", 100 not in List)print("100 not in", Tuple, "is", 100 not in Tuple)passdef is_and_equal():"""is, is not 比較的是兩個變量的內(nèi)存地址== 比較的是兩個變量的值:return:"""x = "hello"y = "hello"print(x is y, x == y)  # True,Trueprint(x is not y, x != y)  # False,Falsea = ["hello"]b = ["hello"]print(a is b, a == b)  # False Trueprint(a is not b, a != b)  # True Falsec = ("hello")d = ("hello")print(c is d, c == d)  # True,Trueprint(c is not d, c != d)  # False,Falsedef complement_code(x):"""求一個數(shù)的補碼https://tianchi.aliyun.com/notebook/169961方法來源,阿里天池:param x::return:"""if x >= 0:return bin(x)else:return bin(x & 0xffffffff)if __name__ == "__main__":# change_element()# add_element()# resize()# nest()# in_and_notin()# is_and_equal()# print(complement_code(-3))

yield關(guān)鍵字

《Python中yield的使用》—— 設(shè)計學(xué)院:這篇文章說使代碼邏輯更加清晰,易于理解和維護(hù),可yield的缺點就是不好閱讀和理解,不是西方人寫的所有東西都是好的。
Python Yield - NBShare

def my_generator():yield 1yield 2yield 3g = my_generator()
print(next(g))  # 輸出:1
print(next(g))  # 輸出:2
print(next(g))  # 輸出:3###############################
def my_generator():yield 1yield 2yield 3if __name__ == '__main__':for i in my_generator():print(i)
# 輸出:
# 1
# 2
# 3
###############################
#惰性計算指的是在需要的時候才計算數(shù)據(jù),而不是一次性計算所有的數(shù)據(jù)。通過yield,我們可以將計算分成多個階段,每次只計算一部分?jǐn)?shù)據(jù),從而減少了計算的時間和內(nèi)存消耗。
def fib():a, b = 0, 1while True:yield aa, b = b, a + bf = fib()
print(next(f))  # 輸出:0
print(next(f))  # 輸出:1
print(next(f))  # 輸出:1
print(next(f))  # 輸出:2
###############################
#協(xié)程是一種在單線程中實現(xiàn)多任務(wù)的技術(shù),可以實現(xiàn)任務(wù)之間的切換和并發(fā)執(zhí)行。
#yield可以用來實現(xiàn)協(xié)程,通過yield可以在函數(shù)執(zhí)行過程中暫停,并切換到其他任務(wù)。這種方式可以大幅度提高程序的并發(fā)性和響應(yīng)性。
#通過yield語句實現(xiàn)了程序的暫停和切換。使用send()方法可以向協(xié)程中傳遞數(shù)據(jù),并在需要的時候繼續(xù)執(zhí)行程序。
def coroutine():while True:value = yieldprint(value)c = coroutine()
next(c)
c.send("Hello")  # 輸出:Hello
c.send("World")  # 輸出:World

我是這么理解的,yield相當(dāng)于把斷點調(diào)試寫成了一個語法特性,每調(diào)用一次這個關(guān)鍵字生成的generator就生成下一個結(jié)果。我發(fā)現(xiàn),國內(nèi)網(wǎng)站UI顏值普遍低,還是說國內(nèi)的技術(shù)棧,像我海軍某少校參觀俄羅斯艦艇所感一樣,“感受到了厚重的歷史”。

for in enumerate

《用法介紹for in enumerate》—— 設(shè)計學(xué)院

######## 1.基本用法
animals = ['cat', 'dog', 'fish']
for idx, animal in enumerate(animals):print('Index:', idx, 'Animal:', animal)# Index: 0 Animal: cat
# Index: 1 Animal: dog
# Index: 2 Animal: fish######## 2.指定遍歷的起始索引值
fruits = ['apple', 'banana', 'melon']
for idx, fruit in enumerate(fruits, start=1):print('Index:', idx, 'Fruit:', fruit)# Index: 1 Fruit: apple
# Index: 2 Fruit: banana
# Index: 3 Fruit: melon######## 3.使用for in enumerate遍歷字典時,會輸出字典中每個鍵值對的索引值和key,而非value。
fruits = {'apple': 1, 'banana': 2, 'melon': 3}
for idx, fruit in enumerate(fruits):print('Index:', idx, 'Fruit:', fruit)# Index: 0 Fruit: apple
# Index: 1 Fruit: banana
# Index: 2 Fruit: melon######## 4.遍歷嵌套列表
neste_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i, lst in enumerate(neste_list):for j, element in enumerate(lst):print('i:', i, 'j:', j, 'element:', element)# i: 0 j: 0 element: 1
# i: 0 j: 1 element: 2
# i: 0 j: 2 element: 3
# i: 1 j: 0 element: 4
# i: 1 j: 1 element: 5
# i: 1 j: 2 element: 6
# i: 2 j: 0 element: 7
# i: 2 j: 1 element: 8
# i: 2 j: 2 element: 9######## 5.K-折交叉驗證遍歷
fold = KFold(5,shuffle=False) 
y_train_data = pd.DataFrame([11,12,13,14,15, 16,17,18,19,20])
print(type(fold.split(y_train_data))) # generator, the yield feature are used in this function,用到了yield關(guān)鍵字.
print(fold.split(y_train_data))
# for iteration, indices in enumerate(fold.split(y_train_data), start = 0): # Try it
for iteration, indices in enumerate(fold.split(y_train_data), start = 1): # fold.split(): Generate 'indices' to split data into training and test set.print('iteration = ',iteration)print(indices[0])print(indices[1])# <class 'generator'>
# <generator object _BaseKFold.split at 0x7f31ba7479e8>
# iteration =  1
# [2 3 4 5 6 7 8 9]
# [0 1]
# iteration =  2
# [0 1 4 5 6 7 8 9]
# [2 3]
# iteration =  3
# [0 1 2 3 6 7 8 9]
# [4 5]
# iteration =  4
# [0 1 2 3 4 5 8 9]
# [6 7]
# iteration =  5
# [0 1 2 3 4 5 6 7]
# [8 9]

for in zip

《用法介紹for in zip》—— 設(shè)計學(xué)院

######## 1.基本用法
iter1 = [1, 2, 3]
iter2 = ['a', 'b', 'c']result = zip(iter1, iter2)
print(type(result))
for x, y in result:print(x, y)  
# 輸出結(jié)果:
# <class 'zip'>
# 1 a  
# 2 b  
# 3 c######## 2.合并列表并輸出
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']result = list(zip(list1, list2))
print(result)  # 輸出結(jié)果:[(1, 'a'), (2, 'b'), (3, 'c')]######## 3.并行處理
import multiprocessingdef process_data(data):# 處理數(shù)據(jù)的函數(shù)print(type(data))print(data,end='')return 'THE RESULT'input_data1 = [1, 2, 3, 4, 5]
input_data2 = ['a', 'b', 'c', 'd', 'e']
pool = multiprocessing.Pool()# 這里的map(func, param),就是將param(可迭代即可)中的每個值,傳入func進(jìn)行并行計算,然后map函數(shù)本身的返回值,就是分別計算的結(jié)果,以list為形式返回。
ret_list = pool.map(process_data, zip(input_data1, input_data2)) # Apply `func` to each element in `iterable`, collecting the results in a list that is returned.
print(ret_list)# 由于是并行的,可能連續(xù)輸出,換行不一定是規(guī)整的,這似乎證明了一件事,那就是print輸出并非原子操作,中間是可以被插入其它運算的
#<class 'tuple'><class 'tuple'><class 'tuple'><class 'tuple'><class 'tuple'>
#(4, 'd')(1, 'a')
#(5, 'e')(3, 'c')(2, 'b')['THE RESULT', 'THE RESULT', 'THE RESULT', 'THE RESULT', 'THE RESULT']######## 4.將兩個列表轉(zhuǎn)換成字典
keys = ['a', 'b', 'c']
values = [1, 2, 3]result = dict(zip(keys, values))
print(result)  # 輸出結(jié)果:{'a': 1, 'b': 2, 'c': 3}

精彩片段

測量程序用時

import time
start = time.time()
# Your python code
end = time.time()
print('The time for the code executed:', end - start)
http://aloenet.com.cn/news/45275.html

相關(guān)文章:

  • 百度怎樣做網(wǎng)站寧波seo排名方案優(yōu)化公司
  • 一個虛擬主機可以做兩個網(wǎng)站吧發(fā)布信息的免費平臺有哪些
  • 電腦網(wǎng)站策劃書交換鏈接營銷成功案例
  • 請別人做網(wǎng)站如何交付產(chǎn)品軟文
  • 國外有沒有網(wǎng)站是做潘多拉的貴州seo技術(shù)培訓(xùn)
  • 榮耀手機價格表大全一覽泉州seo外包
  • 微信鏈圖片轉(zhuǎn)換wordpress鹽城seo優(yōu)化
  • 做公司網(wǎng)站的尺寸一般是多大零基礎(chǔ)學(xué)seo要多久
  • 網(wǎng)站滾動圖片效果怎么做seo排名策略
  • 做團購網(wǎng)站商品從哪里找長沙網(wǎng)絡(luò)營銷哪家平臺專業(yè)
  • 學(xué)校網(wǎng)站建設(shè)主體怎么學(xué)做電商然后自己創(chuàng)業(yè)
  • 神馬網(wǎng)站可以做兼職狼雨的seo教程
  • 深圳市中心在哪里最繁華優(yōu)化大師免費版
  • 口碑好的網(wǎng)站開發(fā)公司電話網(wǎng)站設(shè)計公司網(wǎng)站制作
  • 旅游網(wǎng)站建設(shè)模板下載aso優(yōu)化的主要內(nèi)容
  • 百度地圖導(dǎo)航下載安裝站長工具seo綜合查詢可以訪問
  • 直播網(wǎng)站怎么做啊北京優(yōu)化互聯(lián)網(wǎng)公司
  • wordpress會員時間網(wǎng)站優(yōu)化排名哪家好
  • 下沙做網(wǎng)站的公司上海優(yōu)化網(wǎng)站方法
  • 手機網(wǎng)站頁面設(shè)計如何做好一個品牌推廣
  • 網(wǎng)站換域名要怎么做每日新聞快報
  • 深圳代做網(wǎng)站谷歌瀏覽器 免費下載
  • 購物網(wǎng)站建設(shè)流程百度app客服人工電話
  • 青島開發(fā)區(qū)人才網(wǎng)青島seo優(yōu)化
  • 麗江建設(shè)信息網(wǎng)站汕頭seo排名
  • 網(wǎng)站負(fù)責(zé)人幕布照片競價網(wǎng)絡(luò)推廣培訓(xùn)
  • 優(yōu)酷網(wǎng)站怎么做的中小企業(yè)管理培訓(xùn)班
  • 六里橋做網(wǎng)站公司長沙網(wǎng)站優(yōu)化價格
  • 現(xiàn)在用什么工具做網(wǎng)站好app拉新項目推廣代理
  • 編譯django做的網(wǎng)站廣州網(wǎng)站快速優(yōu)化排名