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

本文介绍了Python中的列表、数组(通过array模块)和字典数据结构,涵盖了创建、操作、索引和常用功能,如长度获取、插入、排序及范围检索。字典特别强调了其键值对的优势和有序性的区别于列表。

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值