Addition deletion and modification of python lists collections and dictionaries
- 2021-12-11 18:13:31
- OfStack
Directory 1 Listing 2 Set 3 Dictionary Summary
Summarize
1 List
# List: An ordered queue of 0 or more object references, indicated by brackets []
# Increase
a = []
a.append(1) # a.append(x) Add at the end of the list 1 New elements x
print(a) # Returns as [1]
a = [1, 2, 3]
a.insert(1, 'hf') # a.insert(i, x) In the list i Add elements to positions x
print(a) # Returns as [1, 'hf', 2, 3]
a = [1, 2, 3]
a.extend('hf6') # a.extend(lt) Or a += lt, Will list lt Element to list a Medium
print(a) # Return at this time [1, 2, 3, 'h', 'f', '6']
# Delete
a.clear() # Delete all
print(a) # Return at this time []
a = [i for i in range(10)]
del a[1:8:2] # del a[i:j:k] Delete the list i To the first j Items with k Data for the number of steps
print(a) # Return at this time [0, 2, 4, 6, 8, 9]
a = [1, 2, 3, 2]
a.remove(2) # a.remove(x) Will the column of the list 1 A x Element deletion
print(a) # Return at this time [1, 3, 2]
a = [1, 2, 3, 2]
a.pop(2) # a.pop(i) Set the first in the list i Elements are taken out and deleted
print(a) # Return at this time [1, 2, 2]
# Modify
a = [1, 2, 3, 2]
a[2] = 'h' # a[i] = j, Will list the first i Elements are changed to j
print(a) # Return at this time [1, 2, 'h', 2]
a = [i for i in range(10)]
a[0:9:2] = "hhhhh" # a[i:j:k]=lt Use a list lt Replacement list a Middle grade i Item to item j Items with k Data for the number of steps
print(a) # Return at this time ['h', 1, 'h', 3, 'h', 5, 'h', 7, 'h', 9]
# Query
a = [i for i in range(10)]
print(a) # Check all and return at this time [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[4]) # print(a[i]) Chady i Elements, at which time return 4
print(a[0:9:2]) # print(a[i:j:k]) , check i To j Step is k Returns the element of the [0, 2, 4, 6, 8]
# Sort
a = [1, 4, 7, 2, 3]
a.sort() # Ascending order
print(a) # Return at this time [1, 2, 3, 4, 7]
a.reverse() # Descending order
print(a) # Return at this time [7, 4, 3, 2, 1]
a.sort(reverse=False) # When reverse=true In descending order, reverse=False Time ascending order
print(a) # Return at this time [1, 2, 3, 4, 7]
# Others
a = [i for i in range(10)]
a *= 3 # a *= n, Will list a Element duplication in n Times
print(a) # Return at this time [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 sets
The # collection (set) is unordered, non-repeatable, usually denoted by curly braces {}, has no concept of index and position, and cannot be fragmented
# Increase
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a.add(5) # If the data 5 Not in the collection a In, will 5 Increase to a Medium
print(a) # Return at this time {1,2,3,4,5}
b.update("hf") # Will " hf "Separately increased to b Medium
print(b) # Return at this time {3, 4, 5, 6, 'h', 'f'}
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a = a | b # Will a And b The complement set of is put into the a Medium
print(a) # Return at this time {1, 2, 3, 4, 5, 6}
# Delete
a = {1, 2, 3, 4}
a.clear() # Delete all
print(a) # Return at this time set()
a = {1, 2, 3, 4}
a.remove(2) # Delete the specified element 2
print(a) # Return at this time {1, 3, 4}
a = {1, 2, 3, 4}
a.discard(2) # If 2 In the collection a In, remove the element, and no error will be reported
print(a) # Return {1,3,4}
# a.remove(2) # If 2 In the collection a The element is removed, and if it is not, the element is generated KeyError Anomaly , An exception is generated at this time
a = {1, 2, 3, 4}
print(a.pop()) # Random return 1 Set of a And remove the element in the a Is empty, generating KeyError Anomaly
print(a) # I don't know why pop The default returns the first 1 Elements, at which time return {2,3,4}
a = {1, 2, 3, 4}
b = {1, 3}
a = a - b # Delete a And b Intersection of
print(a) # Return at this time {2, 4}
a = {1, 2, 3, 4}
b = {1, 3}
a = a & b # And a = a - b Same
print(a) # Return at this time {2, 4}
# Query
a = {1, 2, 3, 4}
print(a) # Check all and output at this time {1, 2, 3, 4}
# Others
a = {1, 2, 3, 4}
c = a.copy() # Returns a collection a Adj. 1 Copies
print(c) # Return at this time {1,2,3,4}
a = {1, 2, 3, 4}
b = {5}
print(a.isdisjoint(b)) # If the collection a And b There are no identical elements, returning True . Return at this time True
print(len(a)) # Returns a collection a Returns the number of elements in the 4
print(a in b) # If a Yes b Returns the element in the True Otherwise, return Fals . Return at this time False
print(a not in b) # If a No b Returns the element in the True Otherwise, return Fals . Return at this time True
3 Dictionary
The # dictionary (dit) is an extension of the set and is also unordered and consists of {}.
# Increase
a = {1: 2, 3: 4}
a[5] = 6 # a[i]=j, Plus 1 Elements, where i Cannot be an existing key
print(a) # Return at this time {1: 2, 3: 4, 5: 6}
a = {1: 2, 3: 4}
a.update({'a': 'b', 'c': 'd'}) # Add more than one element, add without the key, and change with the key
print(a) # Return at this time {1: 2, 3: 4, 'a': 'b', 'c': 'd'}
a = {1: 2, 3: 4}
a.setdefault(5, 6) # No 5 This key is added when
print(a) # Return at this time {1: 2, 3: 4, 5: 6}
# Delete
a = {1: 2, 3: 4}
a.clear() # Delete all
print(a) # Return at this time {}
a = {1: 2, 3: 4}
a.popitem() # Delete the last 1 Elements
print(a) # Return at this time {1: 2}
a = {1: 2, 3: 4}
print(a.pop(1)) # The key exists and returns the key value and deletes it 2
print(a) # Return at this time {3: 4}
a = {1: 2, 3: 4}
del a[3] # Deletes the specified key-value pair
print(a) # Return at this time {1: 2}
# Modify
a = {1: 2, 3: 4}
a[1] = 'new' # Give the key and enter the new value directly. The key is changed, but the key is not added
print(a) # Return at this time {1: 'new', 3: 4}
a = {1: 2, 3: 4}
a.update({1: 'hf'}) # The key has been changed, and the key is not added
print(a) # Return at this time {1: 'hf', 3: 4}
# Query
a = {1: 2, 3: 4}
print(a) # Check all and return at this time {1: 2, 3: 4}
print(a[1]) # Returns the value information corresponding to the specified key, and returns 2
print(a.keys()) # Returns all keys, and returns at this time dict_keys([1, 3])
print(a.values()) # Returns all values, and returns dict_values([2, 4])
print(a.items()) # Returns all key-value pairs, and returns dict_items([(1, 2), (3, 4)])
print(a.get(1)) # a.get(<key>,<default>), If the key exists, return the value corresponding to the key, otherwise return the default value, and return at this time 2
print(a.popitem()) # Random return 1 Key-value pairs, returned as tuples, and returned at this time (3, 4)
# Others
a = {1: 2, 3: 4}
print(1 in a) # <key> in <d>, Returns if the key is in the dictionary True Otherwise, return False Returns at this time True
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!