How to use lists, tuples, dictionaries, and sets

Udemy's course, Introduction to Python 3 taught by active Silicon Valley engineers + Applications + American Silicon Valley style code style It is a memo of.

list

It can store data of any type (integer, floating point number, string, etc.). The elements are in order and you can specify them using indexes. List elements can be changed . Use it like an "array" in other programming languages.

Example of use

Determining if a person can get on a taxi seat

>>> seat = []
>>> min = 0
>>> max = 4
>>> min <= len(seat) < max
True
>>> seat.append("person")
>>> len(seat)
1
>>> seat.append("person")
>>> seat.append("person")
>>> seat.append("person")
>>> min <= len(seat) < max
False
>>> seat.pop(0)
"person"

Tuple

It can store data of any type (integer, floating point number, string, etc.). The elements are in order and you can specify them using indexes. Unlike lists, tuple elements are immutable

Example of use

quiz

>>> chose_from_two = ("A", "B", "C")
>>> answer = []
>>> answer.append("A")
>>> answer.append("C")

Dictionary type

Stores data represented by key / value pairs. Elements have no order (elements cannot be specified by index). Corresponds to "associative array" in other programming languages

Example of use

Fruit EC site

>>> fruits = {"apple": 100, "banana": 200, "orange": 300}
>>> fruits.items()
dict_items([('apple', 100), ('banana', 200), ('orange', 300)])
>>> fruits.keys()
dict_keys(['apple', 'banana', 'orange'])
>>> fruits.values()
dict_values([100, 200, 300])
>>> fruits["apple"]
100
>>> fruits.get("apple")
100
>>> fruits.pop("apple")
100
>>> "apple" in fruits
False

set

A type for handling "sets" in mathematics. Each element does not overlap and has no order (elements cannot be specified by index)

Example of use

Find "common friends" on SNS

>>> my_friends = {"A", "C", "D"}
>>> A_friends = {"B", "D", "E", "F"}
>>> my_friends & A_friends
{'D'}
>>> fruits = ["apple", "banana", "apple", "banana"]
>>> kind = set(fruits)
>>> kind
{'apple', 'banana'}

Other

Count the number of arbitrary character strings

>>> s = "fdjsafiewafjdsaeiwfdafke"
>>> d = {}
>>> for c in s:
...     if c not in d:
...             d[c] = 0
...     d[c] += 1
>>> print(d)
{'f': 5, 'd': 3, 'j': 2, 's': 2, 'a': 4, 'i': 2, 'e': 3, 'w': 2, 'k': 1}

You can write the following using the setdefault function

>>> s = "fdjsafiewafjdsaeiwfdafke"
>>> d = {}
>>> for c in s:
...     d.setdefault(c, 0)
...     d[c] += 1
>>> print(d)
{'f': 5, 'd': 3, 'j': 2, 's': 2, 'a': 4, 'i': 2, 'e': 3, 'w': 2, 'k': 1}

You don't have to call setdefault every time you use the python standard library defaultdict

>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for c in s:
...     d[c] += 1
...
>>> print(d)
defaultdict(<class 'int'>, {'f': 5, 'd': 3, 'j': 2, 's': 2, 'a': 4, 'i': 2, 'e': 3, 'w': 2, 'k': 1})

Recommended Posts

How to use lists, tuples, dictionaries, and sets
How to use redis-py Dictionaries
Save lists, dictionaries and tuples to external files python
How to install and use Tesseract-OCR
How to use .bash_profile and .bashrc
How to install and use Graphviz
How to install and use pandas_datareader [Python]
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use is and == in Python
How to use pandas Timestamp and date_range
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
How to sort 2D arrays, dictionaries and lists of proprietary classes
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
Introducing Sinatra-style frameworks and how to use them
[Introduction to Udemy Python3 + Application] 23. How to use tuples
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
Python3 | Lists, Tuples, Dictionaries
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
Python lists, tuples, dictionaries
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
[Python] How to use hash function and tuple.
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to install Cascade detector and how to use it
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[Python] [Django] How to use ChoiceField and how to add options
Beginners! Basic Linux commands and how to use them!
How to use the grep command and frequent samples
Julia Quick Note [01] How to use variables and constants
How to use argparse and the difference between optparse
How to use Decorator in Django and how to make it
How to use Qt Designer