current position:Home>[introduction to Python 3 6: basic data structure] list, tuple, set, dictionary

[introduction to Python 3 6: basic data structure] list, tuple, set, dictionary

2022-02-02 15:17:54 Overflow tears

list

  • Lists are mutable objects , There are the following methods :
list.append(x)	#  Add an element at the end of the list  x
list.extend(L)	#  Add the specified list at the end of the list  L  The content in 
list.insert(p,x)#  In subscript  p  Insert elements  x
list.remove(x)	#  Delete the first element in the list  x
list.pop([i])	#  Delete list location  i, And return it , If no index is specified , Then delete the last element 
list.clear()	#  clear list 
list.index(x)	#  The first value in the return list is  x  The index of 
list.sort()		#  Sort the elements of the list 
list.copy()		#  Returns a shallow copy of the list 
  • You can use the list as a stack
stack = [1,2,3]
stack.append(x)
stack.pop()
  • You can use the list as a queue , But it's not efficient
queue = deque([1,2,3])
queue.append(x)
queue.popleft()
queue.popright()

List derivation

  • List derivation provides a simple way to create a list from a sequence .
    Each list derivation is in for Followed by an expression , Followed by zero or more for or if Clause
print ([a+b for a in "abc" for b in "[email protected]#"])
print([[x,x**x] for x in range(5) if x > 2]) 

Output is as follows :

['a!', '[email protected]', 'a#', 'b!', '[email protected]', 'b#', 'c!', '[email protected]', 'c#']
[[3, 27], [4, 256]]

Nested list parsing

  • Lists can be nested , Analog multidimensional array
	arr = [
        [1,2,3],
        [4,5,6],
        [7,8,9],
    ]
    print(arr)
    
    arr2 = [[x+3*y for x in range(1,4)] for y in range(3)]
    print(arr2)

The output is as follows :

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

del sentence

  • del Statement can delete elements by index from a list
del a[0]
del a[2:4]
del a[:]
del a

Tuples

  • Tuples may be entered without parentheses , But there are always parentheses in the output , Is a non modifiable object
t = 1,2,3
print(t)

t = (1,2,3)
print(t)

u = t,(4,5,6)
print(u)

The output is as follows :

(1, 2, 3)
(1, 2, 3)
((1, 2, 3), (4, 5, 6))

aggregate

  • A collection is a collection of unordered non repeating elements , Enclosed in curly brackets
    To create an empty collection, you must use set(), If you use {} An empty dictionary will be created
st = {
    'c','b','a',3,2,1,1,2,3}
print(st)
print(1 in st)

a = set({
    1,2,3,4,5})
b = set({
    2,4,6,8,10})
print(a-b)
print(a|b)
print(a&b)
print(a^b)

The output is as follows :

{
    1, 2, 3, 'b', 'c', 'a'}
True
{
    1, 3, 5}
{
    1, 2, 3, 4, 5, 6, 8, 10}
{
    2, 4}
{
    1, 3, 5, 6, 8, 10}

Dictionaries

  • The dictionary is indexed by keywords , Keywords can be any immutable type , But the keywords must be different from each other
    Use curly braces to create an empty dictionary {}
dict = {
    'alice' : 1, 'bob' : 2,3 : 3,4 : 4,5 : 'Nick'}
print(dict['alice'])
print(dict)
del dict['alice']
print(dict)
print(list(dict.keys()))
print('bob' in dict)
print('Nick' in dict)

The output is as follows :

1
{
    'alice': 1, 'bob': 2, 3: 3, 4: 4, 5: 'Nick'}
{
    'bob': 2, 3: 3, 4: 4, 5: 'Nick'}
['bob', 3, 4, 5]
True
False
  • We can traverse the dictionary in this way
dict = {
    'alice' : 1, 'bob' : 2,3 : 3,4 : 4,5 : 'Nick'}
for a,b in dict.items():
	print(a,b)

The output is as follows :

alice 1
bob 2
3 3
4 4
5 Nick

copyright notice
author[Overflow tears],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021517518727.html

Random recommended