[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)

3.1 Lists and tuples

In addition to strings, python has two types of sequence structures, ** tuples ** and ** lists **, and can have 0 or more elements. The difference from the character string is that the element may have a different type.

Features of tuples

Tuples are immutable, and when you substitute an element, it cannot be rewritten as if it had been baked.

List features

It is mutable and allows you to delete and insert elements.

3.2 List

The list is suitable when you want to manage the elements in order, especially when the order and contents may change. Unlike strings, lists are mutable and can be modified directly.

3.2.1 Created by [] or list ()

The list separates zero or more elements with commas and encloses them in square brackets.

>>> empty_list = []
>>> weekdays=['Monday','Tuesday','Wednsday','Thursday','Friday']
>>> another_empty_list=list()#list()Empty list in function[]Can be created.
>>> another_empty_list
[]

3.2.2 Converting from other data types to a list with list ()

#The character string is converted into a character string list for each character.
>>> list('cat')
['c', 'a', 't']

#Convert tuples to lists
>>> a_tuple=('ready','fire','aim')
>>> list(a_tuple)
['ready', 'fire', 'aim']
>>> birthday="1/4/1995"

#split()If you use a function, it will be separated by a separator and made into a list.
>>> birthday.split("/")
['1', '4', '1995']
>>> splitme="a/b//c/d//e"

#If the separators are continuous, an empty string is created as a list element.
>>> splitme.split('/')
['a', 'b', '', 'c', 'd', '', 'e']
>>> splitme="a/b//c/d///e"
>>> splitme.split('//')
['a/b', 'c/d', '/e']

3.2.3 Extracting elements using [offset]

Individual elements can be extracted from the list by specifying the offset as with the character string.

>>> marxes=["TTTTTT","aaaa","bbbb"]
>>> marxes[0]
'TTTTTT'
>>> marxes[1]
'aaaa'
>>> marxes[2]
'bbbb'
>>> marxes[-1]
'bbbb'
>>> marxes[-2]
'aaaa'
>>> marxes[-3]
'TTTTTT'

#The offset must be valid in the list of targets. (It must be a position that has already been assigned.)
>>> marxes[-34]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

3.2.4 List of lists

>>> small_birds=["a","b"]
>>> extinct_birds=["c","d","e"]
>>> carol_birds=["f","g","h","i"]
>>> all_birds=[small_birds,extinct_birds,"AAA",carol_birds]
>>> all_birds#List of lists
[['a', 'b'], ['c', 'd', 'e'], 'AAA', ['f', 'g', 'h', 'i']]
>>> all_birds[0]
['a', 'b']

#[1]Is all_The second element of birds,[0]Points to the first element of its built-in list.
>>> all_birds[1][0]
'c'

3.2.5 Rewriting elements by [offset]

Again, it must be valid in the list offset target list.

>>> marxes=["TTTTTT","aaaa","bbbb"]
>>> marxes[2]=["CCCC"]
>>> marxes[2]="CCCC"
>>> marxes
['TTTTTT', 'aaaa', 'CCCC']

3.2.5 Subsequence retrieval by slice with specified offset range


>>> marxes[0:2]
['TTTTTT', 'aaaa']

#Slices can specify steps other than 1.
>>> marxes[::2]
['TTTTTT', 'CCCC']
>>> marxes[::-2]
['CCCC', 'TTTTTT']

#Reverse the list.
>>> marxes[::-1]
['CCCC', 'aaaa', 'TTTTTT']

Impressions

I entered Chapter 4, but I was sleepy, so I did it moderately today.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[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)
[Introduction to Python3 Day 8] Chapter 4 Py Skin: Code Structure (4.1-4.13)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3 Day 3] Chapter 2 Py components: Numbers, strings, variables (2.2-2.3.6)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Python3 | Lists, Tuples, Dictionaries
Python lists, tuples, dictionaries
How to use lists, tuples, dictionaries, and sets
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Save lists, dictionaries and tuples to external files python
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Udemy Python3 + Application] 23. How to use tuples
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Python language
Python> Tuples versus Lists
Introduction to OpenCV (python)-(2)
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary