序列构成的数组(数组以列序为主序存储)

网友投稿 274 2022-09-04


序列构成的数组(数组以列序为主序存储)

列表推导,笛卡尔乘积

colors = ['block','white']sizes = ['s','n','l']tshirts = [(color,size) for color in colors for size in sizes]print(tshirts)

[('block', 's'), ('block', 'n'), ('block', 'l'), ('white', 's'), ('white', 'n'), ('white', 'l')]

元祖不仅仅是不可变的列表

元组和记录

lax_coordinates = (32.9425,-118.405689)city,year,pop,chg,area = ('tokyo',2003,32450,0.66,8014)traveler_ids = [('usa','31195855'),('bra','ce342567'),('esp','xda205856')]for passport in sorted(traveler_ids): print('%s/%s' % passport)for country,_ in traveler_ids: print(country)

C:\Python\python3.6.5\python.exe E:/pythoncode/day07/01.pybra/ce342567esp/xda205856usa/31195855usabraesp

元组拆包

lax_coordinates = (32.5985,-118.4582556)lat,lng = lax_coordinatesprint(lat)print(lng)

还可以用*把一个可迭代对象拆开作为函数的参数

print(divmod(20,8))#(2, 4)t=(20,8)q,r=divmod(*t)print(q,r)#2 4

a,b,*reset = range(5)print(a,b,reset)

具名元组

from collections import namedtupleCity = namedtuple('City',('name','country','popolation','coordinates'))tokyo = City('Tokyo','jp','36.933',(35.6,139.66))print(tokyo)#City(name='Tokyo', country='jp', popolation='36.933', coordinates=(35.6, 139.66))print(tokyo.popolation)#36.933print(City._fields)#('name', 'country', 'popolation', 'coordinates')latlong = namedtuple('latlong','lat long')delhi_data = ('delhi ncr' , 'in',21.935,latlong(28.36,77.69))delhi = City._make(delhi_data)print(delhi)#City(name='delhi ncr', country='in', popolation=21.935, coordinates=latlong(lat=28.36, long=77.69))print(delhi._asdict())#OrderedDict([('name', 'delhi ncr'), ('country', 'in'), ('popolation', 21.935), ('coordinates', latlong(lat=28.36, long=77.69))])


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:非阻塞IO发送http请求
下一篇:Java实现解析ini文件对应到JavaBean中
相关文章

 发表评论

暂时没有评论,来抢沙发吧~