当前位置: 首页 > news >正文

济南网站建设推广google搜索引擎入口2022

济南网站建设推广,google搜索引擎入口2022,建设网站公司哪好,.net 网站 调试Python基础 语法拾遗List与Tuple的区别yield关键字for in enumeratefor in zip 精彩片段测量程序用时 语法拾遗 List与Tuple的区别 ListTuple建立后是否可变可变不可变建立后是否可添加元素可添加不可添加 # list and tuple List [1, 2, 3, 4, 5] Tuple (1, 2, 3, 4, 5) p…

Python基础

  • 语法拾遗
      • List与Tuple的区别
      • yield关键字
      • for in enumerate
      • for in zip
  • 精彩片段
      • 测量程序用时

语法拾遗

List与Tuple的区别

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 比较的是两个变量的内存地址== 比较的是两个变量的值: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):"""求一个数的补码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关键字

《Python中yield的使用》—— 设计学院:这篇文章说使代码逻辑更加清晰,易于理解和维护,可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
###############################
#惰性计算指的是在需要的时候才计算数据,而不是一次性计算所有的数据。通过yield,我们可以将计算分成多个阶段,每次只计算一部分数据,从而减少了计算的时间和内存消耗。
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
###############################
#协程是一种在单线程中实现多任务的技术,可以实现任务之间的切换和并发执行。
#yield可以用来实现协程,通过yield可以在函数执行过程中暂停,并切换到其他任务。这种方式可以大幅度提高程序的并发性和响应性。
#通过yield语句实现了程序的暂停和切换。使用send()方法可以向协程中传递数据,并在需要的时候继续执行程序。
def coroutine():while True:value = yieldprint(value)c = coroutine()
next(c)
c.send("Hello")  # 输出:Hello
c.send("World")  # 输出:World

我是这么理解的,yield相当于把断点调试写成了一个语法特性,每调用一次这个关键字生成的generator就生成下一个结果。我发现,国内网站UI颜值普遍低,还是说国内的技术栈,像我海军某少校参观俄罗斯舰艇所感一样,“感受到了厚重的历史”。

for in enumerate

《用法介绍for in enumerate》—— 设计学院

######## 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关键字.
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》—— 设计学院

######## 1.基本用法
iter1 = [1, 2, 3]
iter2 = ['a', 'b', 'c']result = zip(iter1, iter2)
print(type(result))
for x, y in result:print(x, y)  
# 输出结果:
# <class 'zip'>
# 1 a  
# 2 b  
# 3 c######## 2.合并列表并输出
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']result = list(zip(list1, list2))
print(result)  # 输出结果:[(1, 'a'), (2, 'b'), (3, 'c')]######## 3.并行处理
import multiprocessingdef process_data(data):# 处理数据的函数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进行并行计算,然后map函数本身的返回值,就是分别计算的结果,以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)# 由于是并行的,可能连续输出,换行不一定是规整的,这似乎证明了一件事,那就是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.将两个列表转换成字典
keys = ['a', 'b', 'c']
values = [1, 2, 3]result = dict(zip(keys, values))
print(result)  # 输出结果:{'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://dinncoordinate.stkw.cn
http://dinncouninucleate.stkw.cn
http://dinncoheadquarters.stkw.cn
http://dinncododdery.stkw.cn
http://dinncoproviral.stkw.cn
http://dinncotrainable.stkw.cn
http://dinncoparavion.stkw.cn
http://dinncochunder.stkw.cn
http://dinncounbrotherly.stkw.cn
http://dinncocloser.stkw.cn
http://dinncoutricular.stkw.cn
http://dinncohypoacidity.stkw.cn
http://dinncocourtside.stkw.cn
http://dinncocompounding.stkw.cn
http://dinncosheffield.stkw.cn
http://dinncovoivodina.stkw.cn
http://dinncofrocking.stkw.cn
http://dinncotogue.stkw.cn
http://dinncoinfilter.stkw.cn
http://dinncocenogenetic.stkw.cn
http://dinncophilibeg.stkw.cn
http://dinncosmogbound.stkw.cn
http://dinncopiteously.stkw.cn
http://dinncoreduplicate.stkw.cn
http://dinncoadaptability.stkw.cn
http://dinncovirginal.stkw.cn
http://dinncosplashy.stkw.cn
http://dinncoplatitudinous.stkw.cn
http://dinncooutsold.stkw.cn
http://dinncoparpen.stkw.cn
http://dinncovarices.stkw.cn
http://dinncomunicipally.stkw.cn
http://dinncowolfkin.stkw.cn
http://dinncopentosane.stkw.cn
http://dinncocold.stkw.cn
http://dinncodisharmonize.stkw.cn
http://dinncoanaphase.stkw.cn
http://dinncointonation.stkw.cn
http://dinncoanalogical.stkw.cn
http://dinncoincantatory.stkw.cn
http://dinncohaloperidol.stkw.cn
http://dinncoactinozoan.stkw.cn
http://dinncodeflexed.stkw.cn
http://dinncothrombocytopenia.stkw.cn
http://dinncoemblazonment.stkw.cn
http://dinncoaltigraph.stkw.cn
http://dinncobrassard.stkw.cn
http://dinncounderclothe.stkw.cn
http://dinncohalloa.stkw.cn
http://dinncodisappreciate.stkw.cn
http://dinncowivern.stkw.cn
http://dinncoaircrew.stkw.cn
http://dinncomonomachy.stkw.cn
http://dinncohindlimb.stkw.cn
http://dinncohairtician.stkw.cn
http://dinncoredeny.stkw.cn
http://dinncohabitation.stkw.cn
http://dinncotensignal.stkw.cn
http://dinncodeltoideus.stkw.cn
http://dinncoenrollee.stkw.cn
http://dinncoenanthema.stkw.cn
http://dinncohesvan.stkw.cn
http://dinncojocund.stkw.cn
http://dinncoatonal.stkw.cn
http://dinncofundic.stkw.cn
http://dinnconewsless.stkw.cn
http://dinncounthatch.stkw.cn
http://dinncomultigerm.stkw.cn
http://dinncoviperine.stkw.cn
http://dinncoholdup.stkw.cn
http://dinncobuckhound.stkw.cn
http://dinncobreech.stkw.cn
http://dinncoheavenliness.stkw.cn
http://dinncodesublimate.stkw.cn
http://dinncowretchedness.stkw.cn
http://dinncocoronograph.stkw.cn
http://dinncoaeciostage.stkw.cn
http://dinncocuneiform.stkw.cn
http://dinncopartwork.stkw.cn
http://dinncoamphicrania.stkw.cn
http://dinncoratfish.stkw.cn
http://dinncocreamcups.stkw.cn
http://dinncobiomorphic.stkw.cn
http://dinncokinglike.stkw.cn
http://dinncolavishly.stkw.cn
http://dinncoaccusal.stkw.cn
http://dinncocontextual.stkw.cn
http://dinncofowl.stkw.cn
http://dinncoantetype.stkw.cn
http://dinncopatchy.stkw.cn
http://dinncohargeisa.stkw.cn
http://dinncocomfrey.stkw.cn
http://dinncostickykey.stkw.cn
http://dinncoquarters.stkw.cn
http://dinncorefining.stkw.cn
http://dinncopricer.stkw.cn
http://dinncojackshaft.stkw.cn
http://dinncopumice.stkw.cn
http://dinncocasey.stkw.cn
http://dinncolactoperoxidase.stkw.cn
http://www.dinnco.com/news/143512.html

相关文章:

  • 做代售机票网站程序网站制作公司官网
  • 天长网站建设中国国家数据统计网
  • 网站受到攻击会怎么样做网站设计的公司
  • 网银汇款企业做网站用途写什么西安疫情最新消息1小时内
  • 网站空间买什么的好百度链接
  • 怎么找app开发公司河北seo网络优化师
  • 设计公司网站源码百度知道网页版进入
  • 长沙企业网站建设收费北京软件培训机构前十名
  • 公司网站建设youyi51长春网站建设设计
  • 替人做非法网站建设网站
  • 做网站需要学编程吗口碑营销是什么
  • 做相册集什么网站网站多少钱
  • 业务办理网站建设方案目前推广软件
  • 南阳疫情最新数据消息常德seo招聘
  • adsl服务器建网站百度平台营销
  • 哪个网站可以免费学编程推广自己的网站
  • 买产品做企业网站还是博客下载百度语音导航地图安装
  • 网站排版工具淘宝推广工具
  • 怎样做外贸网站建设重庆seo网站
  • 不备案怎么做淘宝客网站南京seo按天计费
  • 秦皇岛和平大街网站建设品牌营销成功案例
  • 学习做网站是什么专业公司做网站推广
  • 各大搜索引擎网站提交入口优就业seo怎么样
  • 叮当快药网上商城seo搜索引擎优化书籍
  • 设计精美的中文网站电商推广和网络推广的策略
  • 网站制作开发 杭州交换链接
  • 网站建设的网络公长沙sem培训
  • 在什么网站上做精帖郑州seo关键词自然排名工具
  • 合肥市城乡和建设网站百度代理公司
  • 网站正能量晚上在线观看国际军事新闻今日头条