Python (2)
(1) Collection:
characteristics: unordered, subscripts are not supported, deduplication
operations:
1. add (): add an item (the position is not sure where)
2. update (): append data as a sequence
s1 = {10,20}
s1.update (100)
print (s1) #Report an errors1={10,20}
#s1.update(100) s1.update([100,200,300])
s1.update(‘asdfff’)
print(s1)
#{100, ‘f’, 200, 10, 300, 20, ‘s’, ‘a’, ‘d’}
- The difference between remove, pop and del (the following examples are mainly based on the list because of the order):
- remove (item) deletes the specified data (not subscript) in the list , if the data does not exist, an error will be reported, and the returned is None and the remaining list elements
>>>list1=[1,3,6,7,8]
>>>print list1.remove(3) #对列表元素进行搜索删除,而不是下表
>>>print list1
None
[1,6,7,8]
- pop (index): delete the corresponding value of the variable sequence subscript index without writing, default delete from the tail
>>>list1=[1,3,6,7,8]
>>>print list1.pop(3),#对列表下表进行检索删除
>>>print list1
7
[1,3,6,8]
>>>dict1={'Abby':21,'Bob':22,'cindy':21}
>>>print dict1.pop(1)
>>>print dict1
- of the:
del(list[index])方法是对可变序列中元素下边进行检索删除,不返回删除值(可以使用切片操作)
>>>list1=[1,3,6,7,8]
>>>del list[3]
>>>print list1
[1.3,6,8]
- Determine whether the data is in it: in and not in
- Dictionary, list, set generation
https://blog.csdn.net/weixin_41179709/article/details/81751117
(Two) dictionary:
- Features: Key-value pairs (separated by commas) Multiple key-value pairs with the same key-value: In this form, the value assigned to the key later will become the true value of the key .
dict={ key1: value1
key1: vaklue2,
... }
eg:dic={'one':1,'one':2,'two':3}
print(dic['one'])
#值为2
- Operation:
(1) Add: dic [key] = 'value'
(2) Delete: del dic [key] Use dic.clear () to clear
(3) Search: get ()
Dictionary sequence.get (key, default)
#keys
dict1={'name':'Tom', 'age':20,'gender':'男'}
print(dict1.keys())
#dict_keys(['name', 'age', 'gender'])
#values
print(dict1.values())
#dict_values(['Tom', 20, '男'])
#items
print(dict1.items())
#dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')])
- Key-value pair unpacking:
for key,value in dict1.items():
print(f'{key}={value}')
"""
name=Tom
age=20
gender=男
"""
- Dictionary for traversal
for value in dict1.values():
print(value)
“”“
Tom
20
男
”“”
#key和item同理
(Triad:
- Features: Repeatable, data types can be different, can only be viewed, can not be modified (Note: If the defined tuple has only one data, then add a comma after this data, otherwise the data type is the only data type)
2. Operation :
(1) Search by subscript: tuple [index]
(2) Search by specified data: if it exists, return to subscript
tuple1=('aa','bb','cc')
print(tuple1.index('aa')) #返回 0(下标)
(3) count (): count the number of times a certain data appears in the current tuple
tuple1=('aa','bb','cc')
print(tuple1.count('aa')) #1