老斉とPythonの有容乃大のlist(3)

5931 ワード

リストに対する操作
リストに要素を挿入
前にリストに要素を追加する方法がありますが、その追加はリストに新しい要素を追加する最後の方法です.次のようになります.

>>> 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).

今回は翻訳しません.もし英語が読めないなら、どうして貴国を知っていますか.必ず無理に英語を見て、プログラムをマスターするだけでなく、もっと...(ここでは2千字省略)
公式文書の説明によると、次の実験を行います.実験から理解してください.

>>> all_users
['qiwsir', 'github', 'io']
>>> all_users.insert("python")   #list.insert(i,x),       ,     
Traceback (most recent call last):
 File "", line 1, in 
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)は、元のlistに新しい要素xを挿入するlist[i]の前にi=len(list)が追加された場合、listに等しいことを意味する.append(x)リストの要素を削除
リストの要素は、増加するだけでなく、削除されます.リスト要素を削除する方法は、次の2つです.

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 "", line 1, in 
ValueError: list.remove(x): x not in list


次の点に注意してください.
正しく削除された場合、フィードバックはありません.ニュースがなければいいニュースだ.削除した内容がリストにない場合は、エラーが表示されます.注意新聞のエラー情報を読む:x not in listの視聴者は1つの問題を考えていますか?削除する前に、この要素が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    #       ,      。


上記のコードは、2つの小さなプログラムで、私はインタラクティブモードで実行して、小さな実験に相当します.
もう1つlistを削除します.ポップ([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 "", line 1, in 
IndexError: pop index out of range   #        ,  1  list       


前のように削除する番号がリストの長さの範囲(len(list)で長さを取得する)以内かどうか、事前に判断してもらえませんか.その後、削除または削除しない操作を行います.
Listはおもしろいもので、内包が豊富です.次の話はlistを続けなければならないようです.そして面白いゲームをするかもしれません.お楽しみに.