对list的操作
向list中插入一个元素
前面有一个向list中追加元素的方法,那个追加是且只能是将新元素添加在list的最后一个。如:
>>> all_users = ["qiwsir","github"] >>> all_users.append("io") >>> all_users ['qiwsir', 'github', 'io']
从这个操作,就可以说明list是可以随时改变的。这种改变的含义只它的大小即所容纳元素的个数以及元素内容,可以随时直接修改,而不用进行转换。这和str有着很大的不同。对于str,就不能进行字符的追加。请看官要注意比较,这也是str和list的重要区别。
与list.append(x)类似,list.insert(i,x)也是对list元素的增加。只不过是可以在任何位置增加一个元素。
我特别引导列为看官要通过官方文档来理解:
代码如下:
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
这次就不翻译了。如果看不懂英语,怎么了解贵国呢?一定要硬着头皮看英语,不仅能够学好程序,更能...(此处省略两千字)
根据官方文档的说明,我们做下面的实验,请看官从实验中理解:
>>> all_users ['qiwsir', 'github', 'io'] >>> all_users.insert("python") #list.insert(i,x),要求有两个参数,少了就报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: insert() takes exactly 2 arguments (1 given) >>> all_users.insert(0,"python") >>> all_users ['python', 'qiwsir', 'github', 'io'] >>> all_users.insert(1,"http://") >>> all_users ['python', 'http://', 'qiwsir', 'github', 'io'] >>> length = len(all_users) >>> length 5 >>> all_users.insert(length,"algorithm") >>> all_users ['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm']
小结:
list.insert(i,x),将新的元素x 插入到原list中的list[i]前面
如果i==len(list),意思是在后面追加,就等同于list.append(x)
删除list中的元素
list中的元素,不仅能增加,还能被删除。删除list元素的方法有两个,它们分别是:
list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
我这里讲授python,有一个习惯,就是用学习物理的方法。如果看官当初物理没有学好,那么一定是没有用这种方法,或者你的老师没有用这种教学法。这种方法就是:自己先实验,然后总结规律。
先实验list.remove(x),注意看上面的描述。这是一个能够删除list元素的方法,同时上面说明告诉我们,如果x没有在list中,会报错。
>>> all_users ['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm'] >>> all_users.remove("http://") >>> all_users #的确是把"http://"删除了 ['python', 'qiwsir', 'github', 'io', 'algorithm'] >>> all_users.remove("tianchao") #原list中没有“tianchao”,要删除,就报错。 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
注意两点:
如果正确删除,不会有任何反馈。没有消息就是好消息。
如果所删除的内容不在list中,就报错。注意阅读报错信息:x not in list
看官是不是想到一个问题?如果能够在删除之前,先判断一下这个元素是不是在list中,在就删,不在就不删,不是更智能吗?
如果看官想到这里,就是在编程的旅程上一进步。python的确让我们这么做。
>>> all_users ['python', 'qiwsir', 'github', 'io', 'algorithm'] >>> "python" in all_users #这里用in来判断一个元素是否在list中,在则返回True,否则返回False True >>> if "python" in all_users: ... all_users.remove("python") ... print all_users ... else: ... print "'python' is not in all_users" ... ['qiwsir', 'github', 'io', 'algorithm'] #删除了"python"元素 >>> if "python" in all_users: ... all_users.remove("python") ... print all_users ... else: ... print "'python' is not in all_users" ... 'python' is not in all_users #因为已经删除了,所以就没有了。
上述代码,就是两段小程序,我是在交互模式中运行的,相当于小实验。
另外一个删除list.pop([i])会怎么样呢?看看文档,做做实验。
>>> all_users ['qiwsir', 'github', 'io', 'algorithm'] >>> all_users.pop() #list.pop([i]),圆括号里面是[i],表示这个序号是可选的 'algorithm' #如果不写,就如同这个操作,默认删除最后一个,并且将该结果返回 >>> all_users ['qiwsir', 'github', 'io'] >>> all_users.pop(1) #指定删除编号为1的元素"github" 'github' >>> all_users ['qiwsir', 'io'] >>> all_users.pop() 'io' >>> all_users #只有一个元素了,该元素编号是0 ['qiwsir'] >>> all_users.pop(1) #但是非要删除编号为1的元素,结果报错。注意看报错信息 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range #删除索引超出范围,就是1不在list的编号范围之内
给看官留下一个思考题,如果要向前面那样,能不能事先判断一下要删除的编号是不是在list的长度范围(用len(list)获取长度)以内?然后进行删除或者不删除操作。
list是一个有意思的东西,内涵丰富。看来下一讲还要继续讲list。并且可能会做一个有意思的游戏。请期待。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

在兩小時內高效學習Python的方法包括:1.回顧基礎知識,確保熟悉Python的安裝和基本語法;2.理解Python的核心概念,如變量、列表、函數等;3.通過使用示例掌握基本和高級用法;4.學習常見錯誤與調試技巧;5.應用性能優化與最佳實踐,如使用列表推導式和遵循PEP8風格指南。

Python適合初學者和數據科學,C 適用於系統編程和遊戲開發。 1.Python簡潔易用,適用於數據科學和Web開發。 2.C 提供高性能和控制力,適用於遊戲開發和系統編程。選擇應基於項目需求和個人興趣。

Python更適合數據科學和快速開發,C 更適合高性能和系統編程。 1.Python語法簡潔,易於學習,適用於數據處理和科學計算。 2.C 語法複雜,但性能優越,常用於遊戲開發和系統編程。

每天投入兩小時學習Python是可行的。 1.學習新知識:用一小時學習新概念,如列表和字典。 2.實踐和練習:用一小時進行編程練習,如編寫小程序。通過合理規劃和堅持不懈,你可以在短時間內掌握Python的核心概念。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用