栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

云学编程的第12天—【微软官方python入门教程 P25-P26笔记】2021-11-12 列表-数组-字典

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

云学编程的第12天—【微软官方python入门教程 P25-P26笔记】2021-11-12 列表-数组-字典

P25 Collections concepts of lists arrays and dictionaries

​​​​​​​

We've seen how we can store individual values inside variables.Sometimes we want  to be able to store a group of items in a single colum(列),Sometimes we want to be able to store them into a group(组) where we can actually give each one a name.

Lists are collections of items :   [square bracket] 

names = ['Christopher','Susan']
print(name)

You can start with an empty list  append()

scores = []
scores.append(98)#Add new item to the end
scores.append(99)
print(scores)
print(scores[1])#Collections are zero-indexed

 [1]是99 The reason for that is because everything in Python is going to be zero indexed.

Arrays are also collections of items (in order to use an array, it's slightly  different syntax)  You'll notice that we have to import in our arrays.

from array import array
scores = array('d') #d is actually going to be floating point
scores.append(97)
scores.append(98)
print(scores)
print(scores[1])# I want to access an individual item, use the same indexer
#In my case here I'm declaring an array of digits
#本例中声明的是数字类型的数组

append() takes exactly one argument (3 given) 

其他可以在数组中使用的数据类型 check :(must be b, B, u, h, H, i, I, l, L, q, Q, f or d)GitHub - microsoft/c9-python-getting-started: Sample code for Channel 9 Python for Beginners courseSample code for Channel 9 Python for Beginners course - GitHub - microsoft/c9-python-getting-started: Sample code for Channel 9 Python for Beginners coursehttps://github.com/microsoft/c9-python-getting-started

Common operations 

What else can we do with arrays besides find individual items?We can grab the length of an array by calling len(), we can call insert() to put someting inside of an array. we can call sort() which in my case here is going to wind up doing it alphabetically.

names = ['Sisan','Christopher']
print(len(names))
names.insert(0,'Bill')#Insert before index, put it at the very beginning
print(names)
names.sort()
print(names)

Retrieving ranges

names = ['Sisan','Christopher','Bill']
presenters = names[0:2]#get the first two items
#starting index and number of items to retrieve
print(names)
print(presenters)

[起始(包括):结束(不包括)] 

Dictionary 

person = {'first':'christopher'} #resemble JSON
person['last'] = 'harrison'
print(person)
print(person['first'])

 The big advantage that a dictionary gives to me, is it gives me a set of key-value pairs, if i need to be able to go in and find a specific item, and i don't necessarily remember the order of it. Or sometimes just the ability to name something is exactly what you're looking for, you'd be looking for a dictionary.

list everything's going to be number indexed, and it's going to guarantee the Order of items that go in and the order of items when I go to pull them all back out.

P26实操

christopher = {}
christopher['first'] = 'Christopher'
christopher['last'] = 'Harrison'
#control D 可以全部选中then can change them all in one shot
Susan = {'first' : 'Susan','last' : 'Ibach'}

# print(christopher)
# print(Susan)
people = [christopher,Susan]
people.append({'first' : 'bill', 'last' : 'gates'})
presentrts = people[0:2]
# print(people)
# print(presentrts)
print(len(presentrts))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/488716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号