Set은 array나 list처럼 순열 자료구조(collection)이지만, 순서라는 개념이 존재하지 않는다.
아래는 Set의 특징을 나열한 것이다.
>>> my_set = {1, 2, 3, 4, 5, 1, 2}
>>> my_set
{1, 2, 3, 4, 5}
>>> for i in my_set:
... print(i)
...
1
2
3
4
5
>>> my_set.append(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'append'
>>> my_set[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> my_set.add(7)
>>> my_set
{1, 2, 3, 4, 5, 7}
>>>
>>> 5 in my_set # look up
True
hash는 단방향 (one way) 암호화이다. 단방향이란 한 번 암호화하면 복호화가 안된다는 뜻이다. 실제 값의 길이와 상관없이 hash값은 일정한 길이를 가진다. 그러므로 hash는 주로 임의의 길이를 갖는 임의의 데이터에 대해 고정된 길이의 데이터로 매핑할 때 사용된다.
Dictionary(다른 언어에서는 hashmap이나 hash table이라고 하기도 함)는 Key-value 형태의 값을 저장할 수 있는 자료구조이다. Dictionary의 특징은 아래와 같다.
>>> my_dict = {1 : "one", "two" : 2, 3 : 3.0, 1: "one_one"}
>>> my_dict
{1: 'one_one', 'two': 2, 3: 3.0}
>>> for key, value in my_dict.items():
... print(f"{key} : {value}")
...
1 : one_one
two : 2
3 : 3.0
>>> my_dict[1]
'one_one'
>>> my_dict.get("two")
2
>>> my_dict[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 5
>>> my_dict.get(5, 0)
0
>>> my_dict[7] = "Seven"
>>> my_dict
{1: 'one_one', 'two': 2, 3: 3.0, 7: 'Seven'}