List & Tuple : an ordered collection of objects


List & Tuple : an ordered collection of objects
realpython

リスト機能


1. Lists Are Ordered

>>> [1, 2, 3, 4] == [4, 1, 3, 2]
False

2. Lists Can Contain Arbitrary Objects

[21.42, 'foobar', 3, 4, 'bark', False, 3.14159]

>>> a = [int, len, foo, math]
>>> a
[<class 'int'>, <built-in function len>, <function foo at 0x02CA2618>,
<module 'math' (built-in)>]

3. List Elements Can Be Accessed by Index


: to accessing individual characters in a string
  • len()
  • >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    
    >>> print(a[2:], a[2:len(a)])
    ['baz', 'qux', 'quux', 'corge'] ['baz', 'qux', 'quux', 'corge']
  • stride
  • >>> a[0:6:2]
    ['foo', 'baz', 'quux']
    >>> a[1:6:2]
    ['bar', 'qux', 'corge']
    >>> a[6:0:-2]
    ['corge', 'qux', 'bar']
  • reverse
  • >>> a[::-1]
    ['corge', 'quux', 'qux', 'baz', 'bar', 'foo']
    
    >>> 'If Comrade Napoleon says it, it must be right.'[::-1]
    '.thgir eb tsum ti ,ti syas noelopaN edarmoC fI'
  • how the [:] syntax works with a string
    : If s is a string, s[:] returns a reference to the same object:
  • >>> s = 'foobar'
    >>> s[:]
    'foobar'
    >>> s[:] is s
    True
    : Conversely, if a is a list, a[:] returns a new object that is a copy of a:
    >>> a[:]
    ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    >>> a[:] is a
    False
  • built-in functions
  • ## in/not in
    >>> a
    ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    
    >>> 'qux' in a
    True
    >>> 'thud' not in a
    True
    
    ## concatenation(+) ,replication(*)
    >>> a + ['grault', 'garply']
    ['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply']
    >>> a * 2
    ['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'foo', 'bar', 'baz',
    'qux', 'quux', 'corge']
    
    ## The len(), min(), and max() functions:
    >>> len(a)
    6
    >>> min(a)
    'bar'
    >>> max(a)
    'qux'
    

    4. Lists Can Be Nested


    >>> x = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
    
    >>> x[1]
    ['bb', ['ccc', 'ddd'], 'ee', 'ff']
    
    >>> x[1][1]
    ['ccc', 'ddd']
    
    >>> print(x[3][0], x[3][1])
    hh ii
    
    >>> x[1][1:3]
    [['ccc', 'ddd'], 'ee']
    >>> x[3][::-1]
    ['ii', 'hh']

    5. Lists Are Mutable

    >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    
    >>> a[2] = 10
    >>> a[-1] = 20
    >>> a
    ['foo', 'bar', 10, 'qux', 'quux', 20]
    
    >>> del a[3]
    >>> a
    ['foo', 'bar', 'baz', 'quux', 'corge']
  • insert multiple elements using a slice
  • >>> a = [1, 2, 3]
    >>> a[1:2] = [2.1, 2.2, 2.3]
    >>> a
    [1, 2.1, 2.2, 2.3, 3]
  • insert multiple elements using a list
  • >>> a = [1, 2, 3]
    >>> a[1] = [2.1, 2.2, 2.3]
    >>> a
    [1, [2.1, 2.2, 2.3], 3]
  • insert elements into a list without removing anything
  • >>> a = [1, 2, 7, 8]
    >>> a[2:2] = [3, 4, 5, 6]
    >>> a
    [1, 2, 3, 4, 5, 6, 7, 8]
  • delete multiple elements
  • >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    >>> a[1:5] = []
    >>> a
    ['foo', 'corge']
    
    >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    >>> del a[1:5]
    >>> a
    ['foo', 'corge']
  • Prepending(プレフィックス)またはAppending(添付ファイル)Items to a list
  • >>> a += ['grault', 'garply']
    >>> a
    ['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply']
    
    >>> a = [10, 20] + a
    >>> a
    [10, 20, 'foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    
    >>> a += 'corge'
    >>> a
    ['foo', 'bar', 'baz', 'qux', 'quux', 'c', 'o', 'r', 'g', 'e']
  • a.append(obj)
    :新規リスト
  • を作成せずに変更
    a = ['a', 'b']
    >>> a.append(123)
    >>> a
    ['a', 'b', 123]
    
    >>> x = a.append(123)
    >>> print(x)
    None
    >>> a
    ['a', 'b', 123]
  • concatenation(+) vs Prepending(+=) vs .append vs .extend
  • >>> a = ['a', 'b']
    >>> a + [1, 2, 3]
    ['a', 'b', 1, 2, 3]
    
    >>> a.append([1, 2, 3])
    >>> a
    ['a', 'b', [1, 2, 3]]
    
    # Thus, with .append(), you can append a string as a single entity:
    >>> a.append('foo')
    >>> a
    ['a', 'b', 'foo']
    
    >>> a.extend([1, 2, 3])
    >>> a
    ['a', 'b', 1, 2, 3]
    
    >>> a += [1, 2, 3]
    >>> a
    ['a', 'b', 1, 2, 3]
    
  • .insert(index, obj)
  • >>> a.insert(3, 3.14159)
    >>> a[3]
    3.14159
    >>> a
    ['foo', 'bar', 'baz', 3.14159, 'qux', 'quux', 'corge']
  • .remove(obj)
  • >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    >>> a.remove('baz')
    >>> a
    ['foo', 'bar', 'qux', 'quux', 'corge']
  • .pip(defalt=-1)
  • >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
    >>> a.pop()
    'corge'
    >>> a
    ['foo', 'bar', 'baz', 'qux', 'quux']
    
    >>> a.pop()
    'quux'
    >>> a
    ['foo', 'bar', 'baz', 'qux']

    6. Lists Are Dynamic


    : When items are added to a list, it grows as needed
    >>> a[2:2] = [1, 2, 3]
    >>> a += [3.14159]
    >>> a
    ['foo', 'bar', 1, 2, 3, 'baz', 'qux', 'quux', 'corge', 3.14159]
    
    >>> a[2:3] = []
    >>> del a[0]
    >>> a
    ['bar', 'qux', 'quux', 'corge']

    Tuple


    :tupleは次の2種類を除き、Listと同じです.
  • ()
  • を使用
  • Tuples are immutable.
  • なぜリストではなくTupleを使うのですか?
  • チュートリアルは、リスト実行プログラムよりも高速です.
  • のデータを変更したくない場合.セット内の値がプログラムの実行中に一致していることを確認し、予期せぬ変更を防ぐことができます.
  • の別のPythonデータ型dictionaryでは、KEYには不変の値が必要です.この目的のためにパレットを使用できますが、リストは使用できません.
  • 調音機能


    :tupleは[]の代わりにカッコ(カッコ)を使用しますが、stringやlistと同様にインデックスやシートも使用できます.
    >>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')
    
    >>> t[0]
    'foo'
    >>> t[-1]
    'corge'
    >>> t[1::2]
    ('bar', 'qux', 'corge')
    
    >>> t[::-1]
    ('corge', 'quux', 'qux', 'baz', 'bar', 'foo')
  • 1個の項目のみを使用する場合は、カンマ(,)
  • を使用します.
    >>> t = (2)
    >>> type(t)
    <class 'int'>
    
    >>> t = (2,)
    >>> type(t)
    <class 'tuple'>
    >>> t[0]
    2
    >>> t[-1]
    2

    Tuple Assignment, Packing, and Unpacking

  • Assignment
  • >>> t = ('foo', 'bar', 'baz', 'qux')
  • Packing
    the items in the tuple have been “packed” into the object:
  • >>> t
    ('foo', 'bar', 'baz', 'qux')
    >>> t[0]
    'foo'
    >>> t[-1]
    'qux'
  • Unpakcing
    If that “packed” object is subsequently assigned to a new tuple, the individual items are “unpacked” into the objects in the tuple:
  • >>> (s1, s2, s3, s4) = t
    >>> s1
    'foo'
    >>> s2
    'bar'
    >>> s3
    'baz'
    >>> s4
    'qux'
  • swap
  • まとめとまとめ


    インベントリ

  • インデックスおよびメソッドを使用するため、値を動的に変更およびソートすることが容易である.
  • デフォルトでは[]四角カッコ
  • を使用します.
  • ダイナミック.
  • 保存と削除が容易
  • tupleとの違い:リストの使用方法は任意に値を変更できますが、tupleは変更されていません.
  • ディレクトリとの違いは、リストはインデックスによって値にアクセスできますが、ディレクトリはインデックスを使用できません.(キーアクセス値)
  • のPythonリストは計算方法を有し,非単純な資料型の資料構造形式を有する.
  • アレイの機能:複数の値を格納
  • すべてのデータ型
  • 多次元リスト(リストスキーマリスト)다차원 리스트는 게입개발, 대규모 머신러닝 알고리즘에서 사용된다
  • は、リスト
  • を特定の変数に保存することができる.