列表字典注意,深淺拷貝

2021-09-03 01:20:08 字數 2517 閱讀 5230

lis = [11,22,33,44,55]

# for i in range(len(lis)):

# print(i) # i = 0 i = 1 i = 2

# del lis[i]

# print(lis) # [22,33,44,55] [22, 44, 55] [22, 44]

#第一種

# lis = lis[::2]

# print(lis)

#第二種

# l1 =

# for i in lis:

# if lis.index(i) % 2 == 0:

# lis = l1

# print(lis)

# lis = [11,22,33,44,55]

# # for i in range(len(lis)-1,-1,-1):

# # if i % 2 == 1:

# # print(i)

# # del lis[i]

# # print(lis)

# # print(lis)

# dic = dict.fromkeys([1,2,3],'春哥')

# print(dic)

# dic = dict.fromkeys([1,2,3],)

# print(dic) #

# print(dic)

# dic[2].extend('二哥')

# print(dic)

# l1 =

# l2 = l1

# l3 = l1

# print(l1,l2,l3)

# dic =

# dic1 = {}

## for i in dic:

# if 'k' not in i:

# dic1.setdefault(i,dic[i])

# dic = dic1

# print(dic)

# l =

# for i in dic:

# if 'k' in i:

# for i in l:

# del dic[i]

# print(dic)

# 轉化成bool值

# 0 '' () {} set()

#元祖 如果元祖裡面只有乙個元素且不加,那此元素是什麼型別,就是什麼型別。

# tu1 = (1)

# tu2 = (1,)

# print(tu1,type(tu1))

# print(tu2,type(tu2))

# tu1 = ([1])

# tu2 = ([1],)

# print(tu1,type(tu1))

# print(tu2,type(tu2))

# dic = dict.fromkeys([1,2,3,],3)

# dic[1] = 4

# print(dic)

複製**

複製**

# 賦值運算

# l1 = [1,2,3]

# l2 = l1

# print(l1,l2)

#copy

# l1 = [1,2,3]

# l2 = l1.copy()

# print(l1,l2)

# print(id(l1),id(l2))

# print(l1,l2)

# l1 = [1,2,[4,5,6],3]

# l2 = l1.copy()

# print(l1,id(l1))

# print(l2,id(l2))

# print(l1,l2)

# print(l1,l2)

# print(id(l1[2]))

# print(id(l2[2]))

import copy

# l1 = [1,2,[4,5,6],3]

# l2 = copy.deepcopy(l1)

# print(l1,id(l1))

# print(l2,id(l2))

# print(l1,l2)

# l1 = [1,[1],2,3,4]

# l2 = l1[:]

#l2 的結果是什麼?

# print(l1,id(l1))

# print(l2,id(l2))

# print(l1[1] is l2[1])

# li = ['alex','taibai','wusir','egon']

# for i in li:

# print(li.index(i),i)

# for index,i in enumerate(li,1):

# print(index,i)

python列表,字典

1字串處理 s.startswith adfaas s這個字串是不是以adfaas開始 s.endswith adfaas s這個字串是不是以adfaas結束 s.find substring,start end 可指範圍查詢子串,返回索引值,否則返回 1 s.rfind substring,sta...

Python 列表 字典

陣列 只能儲存同一種資料型別的結構 元組tuple 定義列表 定義元組 元組被稱為被帶了緊箍咒的列表,那麼就證明元組與列表的功能類似,只是不如列表強大。元組是不可變的資料型別 不能修改元組中的元素 列表是可變資料型別,可以修改元素。那就有了增刪改查 拿出列表最後乙個元素,最後乙個元素是列表,再拿出列...

元組,列表,字典

元組 tuple 元組常用小括號表示,即 元素加逗號,是元組的標識。tuple a b c d e f g 常規來說,定義了乙個元組之後就無法再新增或修改元組的元素,但對元組切片可以新增會修改元組的元素。列表 list 列表常用方括號表示,即 1 list1 a b c 1,3,5 2 list2 ...