パイソンのデータ構造-パート2


辞書は何ですか.
辞書はオブジェクトの順序なしコレクションです.これは、辞書がインデックスをサポートしていないことを意味し、辞書はキー値のペアのコレクションです.リストでは、各値にインデックスがありますが、各値は特定のキーに関連付けられています.したがって、辞書は異なる値またはオブジェクトの順序なしコレクションです.辞書巻き括弧{ {}を作成するために使用します.

どのように辞書を定義するには?
辞書巻き括弧{ {}を作成するために使用します.辞書を定義できる様々な方法があります.下記のコードを見てください.
d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}
type(d)
# Output: <class 'dict'>

print(d)
# Output
# {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

# dict() function
'''
You can pass the list of tuples, where each tuple will be a key-value pair.
'''
t = dict([('game', 'Cricket'),('type', 'One-day'),('over', 50),('players', 11)])
'''
You can also use mixed type keys to create a dictionary.
'''
c = dict([(1,[1, 2, 3]), ('name', 'Ramesh')])
辞書値にアクセスする方法?
辞書の値はキーでアクセスできます.リストのように、インデックスの代わりに値にアクセスするために角括弧を使用することができます、ブラケットにキーを書きます.下記のコードを見てください.
# Accessing a dictionary value
d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

print(d['game'])
# Output: Cricket

print(d['over'])
# Output: 50
辞書は変更可能です
はい、辞書は変更可能ですが、キーを使用して辞書の値を変更することができます.下記のコードを見てください.
# Changing Dictionary Values
c = dict([(1,[1, 2, 3]), ('name', 'Ramesh')])

print(c)

# Output
# {1: [1, 2, 3], 'name': 'Ramesh'}


'''
Suppose if you want to change the 'name' in this dictionary then how would you do that?
'''
c['name'] = 'Kumar' 

print(c)
# Output
# {1: [1, 2, 3], 'name': 'Kumar'}
' in '演算子
' in '演算子を使用して、キーが辞書に存在するかどうかを確認できます.キーが存在する場合はtrueを返します.
# The 'in' Operator
d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

print('game' in d)
# Output: True

print('type' not in d)
# Output: False
セット
設定は?
setは不変のデータ値の順序なしコレクションです.それは、リストと辞書がセットの値でありえないことを意味します.セットは重複した値を含んでいません、セットのすべての値はユニークです.それはインデックスをサポートしていないため、設定されていないコレクションです.セットを定義するためには、かすみ括弧やPythonの組み込み関数set ()を使うことができます.

セットを定める方法?
セットを定義するには{ }を使用するか、Pythonの組み込み関数set ()を使用できます.set ()関数はインデックス、文字列、リスト、タプルを持つ任意のデータオブジェクトをとります.下記のコードを見てください.空のセットを定義するには、かっこを括弧ではなくset ()関数を使用します.インタプリタは{ }を辞書と仮定します.
# Define an empty Set
b = {}
t = set()

type(b)
# Output: <class 'dict'>

type(t)
# Output: <class 'set'>

set1 = set('Books')
set2 = set([1,1,2,3,4,4,5,6])
set3 = set(('one', 'two', 'one', 'three', 'four', 'two'))

print('set1:', set1)
print('set2:', set2)
print('set3:', set3)

# Output
# set1: {'B', 'k', 's', 'o'}
# set2: {1, 2, 3, 4, 5, 6}
# set3: {'four', 'two', 'one', 'three'}

どのようにセットから要素を追加または削除するには?
あなたはセット内の個々の値にアクセスすることはできませんが、確実に値を追加または削除することができます.下記のコードを見てください.組み込み関数add add ()を使用して値を削除するには、value ()関数を使用することができます.
# Adding the value
s = {4, 'Text', True, False, 3.14}
s.add('Hello')

print(s)
# Output
# {False, True, 3.14, 4, 'Text', 'Hello'}

# Removing the value
s.discard(True)
print(s)

# Output
# {False, 3.14, 4, 'Text', 'Hello'}

設定操作
あなたは、数学的なセットで実行できるすべての操作を実行できます.詳しく見てみましょう.
同盟
つの集合の集合は、両方のセットに存在するすべての値の集合です.を使うことができます.
# Union of sets

x = {1, 4, 6, 3, 9}
y = {'a', 'b', 'c', 4}

xy = x | y
print('xy', xy)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}

# Using union() function.
x.union(y)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}

y.union(x)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}
交差点
つの集合の交点は、各セットに存在する共通の値の集合である.演算子や演算子メソッドを使用できます.
# Intersection
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using & operator
x_and_y = x & y
print(x_and_y)
# Output
# {'c', 4}


# Using intersection() method.
x.intersection(y)
# Output: {'c', 4}

y.intersection(x)
# Output: {'c', 4}
差異
つのセットの違いは、他のセットではない1つのセットに存在する値の集合です.下記のコードを見てください.
# Difference
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using ‘-’ operator
x_diff_y = x - y
print(x_diff_y)
# Output
#{1, 3, 6}


y_diff_x = y - x
print(y_diff_x)
# Output
# {'a', 'b'}

# Using difference() method.
x.difference(y)
# Output:{1, 3, 6}

y.difference(x)
# Output: {'a', 'b'}

対称差
つの集合の対称的な違いは、共通の値を除いて両方のセットに存在する値の集合です.^演算子またはSymmetricCount Difference ()メソッドを使用して、対称性の違いを取得できます.
# Symmetric Difference
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using '^' operator
x_sdiff_y = x ^ y
print(x_sdiff_y)

# Output
# {1, 3, 6, 'a', 'b'}


# Using symmetric_difference() method.
x.symmetric_difference(y)
# Output:{1, 3, 6, 'a', 'b'}

y.symmetric_difference(x)
# Output: {1, 3, 6, 'a', 'b'}

' in '演算子
今、あなたは' in '演算子に精通しています.下記のコードを見てください.
# The 'in' Operator
y = {'a', 'b', 'c', 4}

print('c' in y)
# Output: True

print(5 not in y)
# Output: True