02 集合類簡單操作

2021-08-20 03:10:49 字數 2142 閱讀 8643

#list:list是一種有序的集合,可以隨時新增和刪除其中的元素

#tuple:tuple和list非常類似,但是tuple一旦初始化就不能修改

#dict:dict全稱dictionary,在其他語言中也稱為map,使用鍵-值(key-value)儲存,具有極快的查詢速度

#print('##########################################list################################')

#定義list

list=['a','b',1]

#追加print(list)

#插入list.insert(0,'hello')

print(list)

#刪除list.pop()

print(list)

#替換list[1]='word'

print(list)

#統計元素數量

print(list.count(2))

#倒數第3個元素

print(list[-3])

#元素反轉

list.reverse()

print(list)

print(type(list))

print('##################################tuple#######################################')

tuple=('hello','word','hi','yes',['tom','jack'])

print(tuple)

print(tuple[-3])

#tuple不可變,但是他的元素可變

tuple[-1][0]='哈哈'

print(tuple)

print(type(tuple))

print('##################################dict#######################################')

dict =

print(dict['c'])

#覆蓋dict['d']=4

print(dict)

#增加dict['e']=5

#用get取值不存在不會報錯,並且可以指定預設值

print(dict.get('f','0'))

print(dict)

#刪除dict.pop("a")

print(dict)

print('##################################set#######################################')

set = set([1,2,3,3,4,5,6,7,8,5])

print(set)

set.add(9)

print(set)

set.remove(1)

print(set)

##########################################list################################

['a', 'b', 1, 2, 2]

['hello', 'a', 'b', 1, 2, 2]

['hello', 'a', 'b', 1, 2]

['hello', 'word', 'b', 1, 2]1b

[2, 1, 'b', 'word', 'hello']

##################################tuple#######################################

('hello', 'word', 'hi', 'yes', ['tom', 'jack'])

hi('hello', 'word', 'hi', 'yes', ['哈哈', 'jack'])

##################################dict#######################################20

##################################set#######################################

process finished with exit code 0

2 集合常用操作

集合的宣告 空集 data1 set print data1,type data1 set 有成員的集合 data2 print data2,type data2 add 新增成員,成員如果已經存在,則會被去重 update 新增成員,成員如果已經存在,則會被去重 data1 data1.add c...

集合11 集合 Collections工具類

collections 操作collection map的工具類 arraylist list new arraylist list.add 123 list.add 456 list.add 456 list.add 89 list.add 23 system.out.println list 1...

8 1集合類(Set,超級for)

hashset的常用方法 例項化hashset set set new hashset 新增元素 set.add 123 set.add 123 set.add ss set.add ss 移除元素,只能是移除指定資料,不能從下標移除,因為hashset是無序的,set.remove ss 獲取元素...