麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

跟老齊學Python之有容乃大的list(3)

2020-02-23 05:49:57
字體:
來源:轉載
供稿:網友

對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)>>> length5>>> 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,有一個習慣,就是用學習物理的方法。如果看官當初物理沒有學好,那么一定是沒有用這種方法,或者你的老師沒有用這種教學法。這種方法就是:自己先實驗,然后總結規律。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产午夜精品久久久久婷 | 性 毛片 | 草草视频免费观看 | 亚洲天堂一级片 | 欧美视频一区二区 | 中国洗澡偷拍在线播放 | 黄色羞羞视频在线观看 | 欧美精品一区二区三区久久久 | 污片视频网站 | 国产午夜亚洲精品午夜鲁丝片 | 国产精品视频一区二区噜噜 | 天天操天天碰 | 国产乱色精品成人免费视频 | 成人一级毛片 | 亚洲小视频在线观看,com | 九色com| 99re66热这里只有精品8 | 亚洲成人福利网站 | pornoⅹxxxxhd麻豆 | 久久草草亚洲蜜桃臀 | 国产精品一区自拍 | 日韩大片在线永久观看视频网站免费 | 97精品视频在线观看 | 制服丝袜日日夜夜 | 久久久久免费精品国产小说色大师 | 成人免费观看49www在线观看 | 美国一级黄色毛片 | 成人一级片毛片 | 一区二区三区欧洲 | 狠狠99| 激情亚洲一区二区三区 | 视频一区二区在线播放 | 成人免费毛片在线观看 | 色视频在线播放 | 91精品国产乱码久久久久久久久 | 亚洲少妇诱惑 | 国产精品一区二区三区在线播放 | 免费毛片视频 | 国产精品欧美日韩一区二区 | 在线观看中文字幕av | 成av在线|