python list、dictionary、tuple

python 知识点整理(四)

本文只是对python部分知识点进行学习和整理
本篇主要是针对python的list、dictionary、tuple的总结

list

collection/ not collection

collection: could carry many varianles in a package(list)
not collection: variables only have one value

list constants

  • A list element can be any Python object – even another list
  • could be empty
  • can be indexed
  • list is mutable 元素可以通过索引方式进行改变

looping/counting(for i in list)

slicing

List can be sliced using colon;
the first number is start position while the second is “up to but not included”

list[first_num:second_num]

mutable/immutable

直接通过list[2]=‘e’,进行改变 那么其list的id不会变
通过replace()更改数据 会使得id也会变

len()/max()/min()/sum()/range()

nested list

list[list[]···]

concatenate

a=[1,2,3]
b=[4,5,6]
c=a+b
[1, 2, 3, 4, 5, 6]

append()

add element

“in” operator

list.sort()/sorted(list)

A list can be sorted using sort(), meaning “sort yourself”

friend=['Tom','Jerry','Bat']
friend.sort()
print(friend)
print(friend[0])
number=[1,2,5,100,32,7,97,1001]
number.sort()
print(number)
['Bat', 'Jerry', 'Tom']
Bat
[1, 2, 5, 7, 32, 97, 100, 1001]

str.split() return a list

  • Use the split() method to break up a string into a list of strings
str='catch me if you can'
words=str.split()
words
['catch', 'me', 'if', 'you', 'can']
  • When you do not specify a delimiter, multiple spaces are treated like “one” delimiter
  • You can specify what delimiter character to use in splitting

dictionary

a “bag” of values, each with its own label
list of key:value pairs

index

dict is no order
index by key

dictionary literals(contants)/assignment

definite loops/counting

in: check if an element is in list or a key is in the dictionary

for key,value in dict2.items():
    print(key,value)
chuck 1
fred 42
jan 100

dict.get()

counts={'aaa':1,'bbb':2,'ccc':5}
print(counts.get('bbb',0))
print(counts.get('eee',0))
2
0

dict.keys()/dict.values()/dict.items()

dict2={'chuck':1,'fred':42,'jan':100}
print(list(dict2))
print(list(dict2.keys()))
print(list(dict2.values()))
print(list(dict2.items()))
['chuck', 'fred', 'jan']
['chuck', 'fred', 'jan']
[1, 42, 100]
[('chuck', 1), ('fred', 42), ('jan', 100)]

tuple

more efficient in terms of memory use and performance than lists

index

start from 0

immutable

once you create a tuple, you cannot change its contents – similar to a string

comparison

The comparison operators work with tuples and other sequences if the first item is equal. Python goes on to the next element, until it finds the elements which are different
return Ture/False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值