0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python 3 エンジニア認定基礎試験対策5 - データ構造

0
Last updated at Posted at 2024-01-23

はじめに

Python3エンジニア認定基礎試験の対策として自分用に執筆しています。

この記事はPython・執筆学びたての初心者が執筆しています。間違いがあるかもしれませんので、ぜひ各項目にある参考文献も確認してください。また、誤りがあればコメントで教えていただけると幸いです。

データ構造

リスト

>>> test = [0]
>>> test.append(1) # 値
[0, 1]
>>> test.extend([2, 3]) # イテラブル
[0, 1, 2, 3]
>>> test.insert(0, -1) # (挿入位置, 値)
[-1, 0, 1, 2, 3]
>>> test.remove(3) # 要素の削除
[-1, 0, 1, 2]
>>> test.pop(-1) # 要素を取り出し、リストから排除
[0, 1, 2]
>>> test.index(1) # インデックスを返す
1
>>> test.count(2)
1
>>> test.sort() # 要素のソート
>>> test.reverse() # 逆順
[2, 1, 0]
>>> test2 = test.copy()
>>> print(test2)
[2, 1, 0]
>>> del test2[0] # 指定したインデックスの要素を削除
[1, 0]
>>> del test2[:]
[]
>>> del test # 変数を削除
>>> print(test)
NameError: name 'test' is not defined

リストをスタックして

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> print(stack)
[3, 4, 5, 6, 7]
>>> stack.pop()
[3, 4, 5, 6]
>>> stack.pop()
>>> stack.pop()
[3, 4]

リストをキューとして

>>> from collections import deque
>>> queue = deque([1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
>>> queue.append(6)
>>> queue.append(7)
[1, 2, 3, 4, 5, 6, 7]
>>> queue.popleft()
>>> queue.popleft()
[3, 4, 5, 6, 7]

タプル

>>> tup = 123, 456, 789
>>> tup[0]
123
>>> tup
(123, 456, 789)
>>> tup[0] = 321
TypeError: 'tuple' object does not support item assignment

丸括弧がつくシーケンス型。要素を代入することはできない。

>>> tup = 1, 2, 3 # パック
>>> x, y. z = tup # アンパック

アンパック動作によりタプルを変数に入れることができる。アンパックするには変数の数とタプルの要素数が同じである必要がある。

集合型

重複する要素を持たない、順序付けられていない要素

>>> basket = {'apple', 'orange', 'apple', 'banana'}
>>> print(basket)
{'apple', 'orange', 'banana'}
>>> 'grape' in basket
False
>>> a = set('anaconda') # 集合型を生成するset()
>>> b = set('alacazam')
>>> print(a)
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # bだけにしかない要素を取り出す
{'r', 'd', 'b'}
>>> a | b # 和集合(全部)
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # 積集合(どっちにもある)
{'a', 'c'}
>>> a ^ b # 対称差(片方にしかない)
{'r', 'd', 'b', 'm', 'z', 'l'}

辞書型

マッピング型。キーで取り出す。

>>> dic = {1: 'one', 2: 'two', 3: 'three'}
>>> dic[4] = 'four'
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> del dic[4]
{1: 'one', 2: 'two', 3: 'three'}
>>> list(dic)
[1, 2, 3]

dict()を用いてキーと値のペアのタプルを含むリストから辞書を生成する

>>> dict([('Python', 'snake'), ('Java', 'coffee')])
{'Python': 'snake', 'Java': 'coffee'}
>>> dict(blue='ocean', red='fire', green='forest')
{'blue': 'ocean', 'red': 'fire', 'green': 'forest'}

ループする際に覚えておくよい技

辞書に対してのループでキーと値をそれぞれ取り出したいならitems()を使う。

>>> basket = {'apple': 'red', 'banana': 'yellow', 'grape': 'purple'}
>>> for k, v in basket.items():
...     print(k, v)
apple red
banana yellow
grape purple

シーケンスにわたるループで、インデックスと要素を取り出したいならenumerate()を使う。

>>> for i, v in enumerate(['zero', 'one', 'two'])
...     print(i, v)
0 zero
1 one
2 two

2つ以上のシーケンスを利用するならzip()を使う。

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}?  It is {1}.'.format(q, a))
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

条件をくわしく

and, orは短絡演算子なので、答えが確定した時点でそれ以降の評価は行わない。

>>> string1, string2, string3 = '', 'Japanese', 'English'
>>> non_null = string1 or string2 or string3
>>> non_null
Japanese # 非空を左から評価した際に真ん中が非空だったので右は評価されない

シーケンスとシーケンスは評価し合える(同じシーケンス型のみ)

>>> (1, 2, 3) < (1, 2, 4)
>>> [1, 2, 3] < [1, 2, 4]
>>> 'ABC' < 'C' < 'PHP'
>>> (1, 2, 3, 4) < (1, 2, 4)
>>> (1, 2) < (1, 2, -1)
>>> (1, 2, 3) == (1.0, 2.0, 3.0)

上の例は全てTrueになる

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?