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

被折叠的 条评论
为什么被折叠?



