隨筆 列表 序列)遍歷的乙個小坑

2021-10-08 11:38:30 字數 3772 閱讀 8684

# 功能描述:將從l1中,刪除s1中的元素

l1 =

["a"

,"b"

,"c"

,"d"

,"e"

,"f"

,"g"

,"h"

,"i"

,"j"

,"k"

]s1 =

for item in l1:

if item in s1:

l1.remove(item)

print

("l1="

, l1)

# l1= ['b', 'd', 'e', 'f', 'g', 'h', 'j']

l1 =

["a"

,"b"

,"c"

,"d"

,"e"

,"f"

,"g"

,"h"

,"i"

,"j"

,"k"

]s1 =

# a、b 和 i、j 在l1 中是連續的

for item in l1:

if item in s1:

l1.remove(item)

print

("l1="

, l1)

# l1= ['b', 'c', 'd', 'e', 'f', 'h', 'j', 'k']

2、for迴圈是否會因為待遍歷列表發生改變,而改變執行順序

3、for迴圈次數是否在第一次執行時就已明確次數

為此我們增加除錯資訊 count、i 、temp_list

l1 =

["a"

,"b"

,"c"

,"d"

,"e"

,"f"

,"g"

,"h"

,"i"

,"j"

,"k"

]s1 =

# a、b 和 i、j 在l1 中是連續的

count =

0# 迴圈外計數器

temp_list=

# 記錄遍歷到的元素

print

(count,

" ",

" ",l1)

# 列印起始狀態的資訊

for i,item in

enumerate

(l1)

:# 迴圈遍歷,並計數

count +=

1

if item in s1:

l1.remove(item)

# 從列表中刪除s1中存在的元素

print

(count, i,item,l1)

# 列印遍歷乙個元素的的資訊

print

("temp_list="

,temp_list)

print

("l1="

, l1)

# 0 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 1 0 a ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 2 1 c ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 3 2 d ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 4 3 e ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 5 4 f ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 6 5 g ['b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k']

# 7 6 i ['b', 'c', 'd', 'e', 'f', 'h', 'j', 'k']

# 8 7 k ['b', 'c', 'd', 'e', 'f', 'h', 'j', 'k']

# temp_list= ['a', 'c', 'd', 'e', 'f', 'g', 'i', 'k']

# l1= ['b', 'c', 'd', 'e', 'f', 'h', 'j', 'k']

l1 =

["a"

,"b"

,"c"

,"d"

,"e"

,"f"

,"g"

,"h"

,"i"

,"j"

,"k"

]s1 =

# a、b 和 i、j 在l1 中是連續的

l2 = l1.copy(

)# 複製l1

count =

0temp_list =

print

(count,

" ",

" ", l1)

for i, item in

enumerate

(l2)

:# 對l2遍歷

count +=

1if item in s1:

l1.remove(item)

print

(count, i, item, l2)

print

("temp_list="

, temp_list)

print

("l1="

, l1)

# 0 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 1 0 a ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 2 1 b ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 3 2 c ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 4 3 d ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 5 4 e ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 6 5 f ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 7 6 g ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 8 7 h ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 9 8 i ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 10 9 j ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# 11 10 k ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# temp_list= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

# l1= ['c', 'd', 'e', 'f', 'h', 'k']

坑 python新建二維列表的乙個小坑

今天覆寫pie架構的 寫完覺得沒什麼問題,結果怎麼跑也跑不通。本來決定跑通就回寢睡覺,一路弄到十二點多,終於地毯式的debug終於找到了問題所在。反正現在精神狀態良好,順便水一篇部落格。我們常常遇到需要初始化乙個空的二維列表來儲存矩陣資料,如鄰接矩陣等二維結構的資料。那麼一般來說我們常規的操作是這樣...

mongodb的乙個小坑

若collection裡有其他的資料,顯示時注意要往query裡新增true,並且需要放在最前面。解釋 下圖是名為test的collection裡面的資料。可以看到上面5條是一樣的資料,第6條是為了測試故意新增進去的。首先,當你執行命令db.getcollection test find 結果如下。...

Mybatis的乙個小坑

以前一直用的ibatis,前陣子才改用的mybatis,對於一些細節不太了解,所以踩了這個坑。廢話不多說,上 下面是出問題的sql語句 insert into g label obj relation his id label obj relation,id label,followed obj c...