程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python notes \u 3] collection, dictionary

編輯:Python

List of articles

  • 1 aggregate
    • 1.1 Create an empty set
    • 1.2 Sets are mutable types
      • 1.2.1 Add elements to the collection :
      • 1.2.2 Delete operation :remove(),pop(),clear()
      • 1.2.3 clear(): Emptying operation
      • 1.2.4 Loop traversal
    • 1.3 Set generation operation
      • 1.3.1 Members of the operation
    • 1.4 hand over 、 and 、 Look up set operation
      • 1.4.1 intersection
      • 1.4.2 Combine
      • 1.4.3 Difference set
      • 1.4.4 Symmetry difference : Put different elements in a collection
    • 1.5 A subset of 、 True subset
      • 1.5.1 <: Represents a true subset ,<=: A subset of
  • 2 Dictionaries
    • 2.1 Create an empty dictionary
      • 2.1.1 How to create a dictionary
      • 2.1.2 Determine the number of key value pairs in the dictionary according to the number of keys
      • 2.1.3 Dictionary generative
    • 2.2 Find the elements in the dictionary
    • 2.3 Replace the values in the dictionary
    • 2.4 Loop traversal
    • 2.5 len()
    • 2.6 Get all the keys in the dictionary keys()
    • 2.7 Get all the values in the dictionary value()
    • 2.8 Get all key value pairs in the dictionary items()
    • 2.9 Delete elements from Dictionary :
      • 2.9.1 pop(): Delete key value pairs by deleting key values
      • 2.9.2 popitem(): Delete the last key value pair in the dictionary

1 aggregate

Container data type
set
{}

nature :

Disorder : Each element has the same place in the set
The opposite sex : In a collection , An element can only exist once
deterministic : Given an element , This element belongs to or does not belong to this collection

1.1 Create an empty set

# How collections are created 
a = set()
print(type(a))
# How dictionaries are created 
b = {
}
print(type(b))

1.2 Sets are mutable types

1.2.1 Add elements to the collection :

# add()
a.add(33)
# a.add({12, 32}) # Report errors 
print(a)
# updata
a.update({
1, 20, 3})
print(a)

add() You can only add one by one ,updata() Add elements can be directly added in two columns

1.2.2 Delete operation :remove(),pop(),clear()

# remove()
a.remove(33)
print(a)
# pop(): Randomly delete elements from a collection 
# stay cmd Is to randomly delete an element , however Python No 
# If it is a string, delete the leftmost element 
# If it's a number , Delete the smallest value 
a.pop()
print(a)
c = {
'Python', 'java', 'Go', 'C++', 'C#'}
print(c)
c.pop()
print(c)

1.2.3 clear(): Emptying operation

c.clear()
print(c)

1.2.4 Loop traversal

e = set('hello')
print(e)
for i in e:
print(i)

1.3 Set generation operation

f = {
i for i in range(1, 11)}
print(f)

1.3.1 Members of the operation

print(1 in f)

1.4 hand over 、 and 、 Look up set operation

set1 = {
4, 7, 9, 12, 56}
set2 = {
4, 7, 12, 32}

1.4.1 intersection

print(set1 & set2)
print(set1.intersection(set2))

1.4.2 Combine

print(set1 | set2)
print(set1.union(set2))

1.4.3 Difference set

print(set1 - set2)
print(set2 - set1)
print(set1.difference(set2))

1.4.4 Symmetry difference : Put different elements in a collection

print(set1 ^ set2)
print(set1.symmetric_difference(set2))

1.5 A subset of 、 True subset

set3 = {
20, 15, 32, 42}
set4 = {
20, 15}

1.5.1 <: Represents a true subset ,<=: A subset of

print(set4 < set3)
print(set4 <= set3)

2 Dictionaries

Dictionaries
{}
dict
The elements in the dictionary exist as key value pairs ,key:value

2.1 Create an empty dictionary

dict_1 = {
}

2.1.1 How to create a dictionary

The key in the dictionary must be an immutable type : character string 、 Numbers 、 Component
Dictionary values can be of any type

dict_2 = {
'name': ' Xiao Ming ', 'age': 20}
print(dict_2)
dict_3 = dict(name=' The mushroom ', age=30)
print(dict_3)
dict_4 = dict(zip('ABCDE', '12345'))
print(dict_4)

2.1.2 Determine the number of key value pairs in the dictionary according to the number of keys

dict_5 = dict(zip('ABCDE', range(1, 11)))
print(dict_5)

2.1.3 Dictionary generative

dict_6 = {
i: i ** 3 for i in range(1, 6)}
print(dict_6)

2.2 Find the elements in the dictionary

a = dict_2['name']
print(a)
# If the key you are looking for does not exist , Report errors 
# b = dict_2['zone']
# print(b)

2.3 Replace the values in the dictionary

dict_2['name'] = ' Little pig '
print(dict_2)
# If you replace the value of a key that does not exist in the dictionary , Equal to add 
dict_2['zone'] = '123'
print(dict_2)

2.4 Loop traversal

for i in dict_2:
print(i, '--->', dict_2[i])

2.5 len()

print(len(dict_2))

2.6 Get all the keys in the dictionary keys()

print(dict_2.keys())

2.7 Get all the values in the dictionary value()

print(dict_2.values())

2.8 Get all key value pairs in the dictionary items()

print(dict_2.items())
for key, value in dict_2.items():
print(key, '--->', value)

2.9 Delete elements from Dictionary :

2.9.1 pop(): Delete key value pairs by deleting key values

a = dict_2.pop('zone')
print(a)
print(dict_2)

2.9.2 popitem(): Delete the last key value pair in the dictionary

# If the dictionary is empty , Report errors ,ketError
b = dict_2.popitem()
print(b)
print(dict_2)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved