#list1 = [str(i) for i in "here"]
#list2 = [str(i) for i in "hello"]
簡單的迴圈,卻很有效,因為總不能寫成『h','e','r','e'吧
#list1 = [str(i) for i in "here"]
#list3=
list3.extend([list1[x]])#
列表取元素
可以使用 del 語句來刪除列表的元素
#list1=[1,2,3,4]
del list1[2]
#s=set(['d','df','ser'])
#print(s)
#集合減去交集,返回新集合 -----add()用於set()集合類
#s1=set('herea')
#s2=set('hello')
#s3=set()
#for x in s1:
#if x not in s2:#集合減去交集,返回新集合 -----add()用於set()集合類
#s3.add(x)
#print(s3)
#remove() #刪除元素
但是不是直接刪除元素,而是刪除集合的下標元素
#s1.remove(1)
我們經常會用到迴圈去遍歷集合或者列表,當找到所需條件元素時便會對其增加或刪除(從某個列表或者集合中),這裡便是雷區,錯誤及其不易察覺
引用2.2的例子,將其修改成如下操作:
#list1 = [str(i) for i in "here"]
#list2 = [str(i) for i in "hello"]
#for x in range(len(list1)):
#if list1[x] in list2:#列表刪除元素,用不同時往空列表中加
修改為存在,然後刪除,看似莫得問題,但這裡的迴圈語句
len(list1),在你刪除後會動態的變化,便會出現列表下表越界的情況
#list1.remove(list1[x])
#print(list1)
修改為存在,然後刪除,看似莫得問題,但這裡的迴圈語句len(list1),在你刪除後會動態的變化,便會出現列表下表越界的情況**************************************希望能幫助到大家,解除相關疑惑*********************************************
python列表 元組與集合
python列表 一 列表的建立與操作 1.建立列表 a 元素型別可為int,float,complex,str,list,tuple b a,1,true,3j 2,hi c 1,2,3,4 a,b 233,hello d a,b,c 2.列表的索引與切片 c 1,2,3,4 5,6 233,he...
集合與列表
陣列是基本的集合,遍歷集合常用的方法是foreach和for迴圈 查詢陣列中的物件 person persons new person 3 person zs new person 張 三 person zs1 new person 張 三 response.write zs.equals zs1 ...
集合與列表
陣列是基本的集合,遍歷集合常用的方法是foreach和 for迴圈,使用foreach語句時,編譯好的中間語言 與使用 for的 相同。除非需要一些複雜的操作,如逆序迭代或每隔 一項進行迭代,否則應使用foreach迭代陣列和大多數集合,特定語言的編譯器會處理這種情況,不容易出錯,且易於 理解。重寫...