Python learning phase III

More and more students 2022-05-14 14:31:37 阅读数:902

pythonlearningphaseiii

time :5-12

Basic operation of dictionary

(1), Find the element method of the dictionary through the key , If no relevant data is found ,.get() Returns the None.

lis={'zhangsan':100,'lisi':200}
print(lis['zhangsan'])# Method 1
print(lis.get('zhangsan'))# Method 2 

Output results :100

(2), Key judgment

lis={'zhangsan':100,'lisi':200}
print('zhangsan' in lis)

Output results :True

(3), Deletion of dictionary elements , eliminate

lis={'zhangsan':100,'lisi':200}
del lis['zhangsan']
print(lis)
lis.clear()
print(lis)

Output results :{'lisi': 200}

                  {}

(4), The addition of dictionary elements , modify

lis={'zhangsan':100,'lisi':200}
lis['xxx']=80
print(lis)

Output results :{'zhangsan': 100, 'lisi': 200, 'xxx': 80}

(5), Get dictionary view

1, Get all dictionary key values

lis={'zhangsan':100,'lisi':200}
print(lis.keys())

Output results :dict_keys(['zhangsan', 'lisi'])

2, Get dictionary elements

lis={'zhangsan':100,'lisi':200}
print(lis.vaules())

Output results :dict_values([100, 200])

3, Get all key value pairs

lis={'zhangsan':100,'lisi':200}
print(lis.items())

Output results :dict_items([('zhangsan', 100), ('lisi', 200)])

(6), Traversal of dictionary elements

lis={'zhangsan':100,'lisi':200,'xxx':89}
for a in lis :
   print(a,lis.get(a))

Output results :zhangsan 100 lisi 200 xxx 89

The characteristics of a dictionary

1, All elements in the dictionary are a key value pair , Key value key Do not repeat ( Will overwrite the original value ),value Can be repeated

2, Dictionary elements are out of order , Cannot insert element directionally

3, In the dictionary key Must be immutable

4, Dictionaries can scale dynamically


time :5-13

Dictionary generative

That is, the third way to generate a dictionary , Suppose there are two lists , We can use built-in functions zip() Carry out packing operation , Compose a dictionary .( notes : If the number of upper and lower elements is different , Take the list with the smallest number of elements and merge it into a dictionary )

lis1=['asd','qwe','zxc']
lis2=[20,30,40]
x = { i:j for i,j in zip(lis1,lis2) }#i,j All variables
print(x)
lis1=['asd','qwe','zxc']
lis3=[20,30]
y = { i:j for i,j in zip(lis1,lis3) }

Output results :{'asd': 20, 'qwe': 30, 'zxc': 40}

                  {'asd': 20, 'qwe': 30}

Tuples

(1), Concept

Similar to the list , But the difference is that the elements of the list can be changed , Including modifying element values , Delete and insert elements , So the list is a variable sequence , And once the tuple is created , Its elements cannot be changed , So tuples are immutable sequences . Tuples can also be seen as immutable lists , Usually , Tuples are used to hold content that doesn't need to be modified .

(2), Tuple creation and output

If there is only one element in a tuple , To add , In brackets , Otherwise, it is judged as str type .

If the specified element is output , Same as the list , Just write the subscript of the corresponding element

lis=('hellow','python',666)
print(lis)
x = tuple(('hellow','python',666))
print(x)
lis3=(50,)
print(type(lis3))
print(lis[2])

Output results :('hellow', 'python', 666)

                  ('hellow', 'python', 666)

                  <class 'tuple'>

                  666

(3), Traversal of tuples

lis=('hellow','python',666)
for i in lis:
   print(i,'',end='')

Output results :hellow python 666

版权声明:本文为[More and more students]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141424496256.html