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

Python learning notes: Collections

編輯:Python
###########################################################
# 1、1) Define a collection ( Non repetitive sequences , disorder )
class_num = {1, 2, 3, 4, 5}
# print(class_num)
# 2) Collections are sometimes used to eliminate duplicate elements of lists and tuples
# class_list = [1, 2, 3, 3, 4, 1]
# nums = set(class_list)
# print(nums)
# class_tuple = ('datian', 'lily', 'developer', 'tester', 'datian')
# print(set(class_tuple))
# 3) Set to list、 Or tuple
# print(list(class_num))
# print(tuple(class_num))
# Running results :[1, 2, 3, 4, 5]
# (1, 2, 3, 4, 5)
# 2、 Ergodic set , Same as list operation
# for i in class_num:
# print(i)
# 3、 View collection length
# print("class_num The length of :",len(class_num))
# 4、 Determine whether the set contains an element ?in The operator
print(10 in class_num)
# 5、 Add elements 10,add function , We need to pay attention to :add When adding elements, you will determine whether the new elements are included , If it exists, it will do nothing
# class_num.add(10)
# print(class_num)
# print(10 in class_num)
# 6、 The added element cannot be modified , Delete only remove
# class_num.remove(1)
# print(class_num)
# class_num.remove(1)# Deleting again will result in an error , because 1 This element is no longer in this collection ,
# print(class_num)
# Introduce another delete element discard, Without this element , Do nothing
# class_num.discard(1)
# print(class_num)
# Delete an element from the collection , And return this value , use pop function , This function can be executed all the time , Until empty
# num = class_num.pop()
# print(class_num, num) # Running results :{2, 3, 4, 5} 1
# Cycle through element values
# while class_num:
# num = class_num.pop()
# print(num)
###########################################################
# 2、 Set function
# Find the intersection
s1 = {1,2,4,5}
s2 = {2,5,6}
print(s1.intersection(s2))
# Union
print(s1.union(s2))
# Determine whether a set is a subset of another set
s3 = s1.intersection(s2)
print(s3.issubset(s1))
# Determine whether it is a parent set
print(s3.issubset(s1))


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