Summary of frequently used Python arrays (for myself)

Mainly for myself. I started Python for a competition pro, so I'll make a note of what I often forget.

How to write subarrays and substrings

Basically, specify the range at the end like ls [2: 5]. In this case, the index of ls elements of 2 or more and less than 5 is output.

ls=[0,1,2,3,4,5,6,7,8,9]
subls_1=ls[2:8]
print(subls_1)
  #=> [2,3,4,5,6,7]

If you take a subarray several times, pay attention to the index. (Failed several times)

ls=[0,1,2,3,4,5,6,7,8,9]
subls_1=ls[2:8]
subls_2=ls[1:4]
print(subls_2)
  #=> [3,4,5]

Either number can be omitted.

ls=[0,1,2,3,4,5,6,7,8,9]
subls_3=ls[:8]
print(subls_3)
  #=> [0,1,2,3,4,5,6,7]
subls_4=ls[2:]
print(subls_4)
  #=> [2,3,4,5,6,7,8,9]

You can also retrieve every two or three elements. Note the ":" when omitting numbers.

ls=[0,1,2,3,4,5,6,7,8,9]
subls_5=ls[2:8:3]
print(subls_5)
  #=> [2,5]
subls_6=ls[::3]
print(subls_6)
  #=> [0,3,6,9]

Summary when outputting

When outputting an array separated by spaces, prefix it with "*".

ls=[0,1,2,3,4,5,6,7,8,9]
print(ls)
  #=> [0,1,2,3,4,5,6,7,8,9]
print(*ls)
  #=> 0 1 2 3 4 5 6 7 8 9

When space is not required, such as when outputting characters, the arrays are combined before output. Note that this cannot be used for int types.

ls2=['a', 'b', 'c']
print(ls2)
  #=> ['a', 'b', 'c']
print("".join(ls2))
  #=> abc

(Addition) I was told in the comments how to use it without worrying about the type. Thank you very much.

ls=[0,1,2,3,4,5,6,7,8,9]
print(*ls, sep='')
  #=> 0123456789

Recommended Posts

Summary of frequently used Python arrays (for myself)
Summary of python environment settings for myself [mac] [ubuntu]
[Anaconda3] Summary of frequently used commands
Summary of frequently used commands of django (beginner)
Python + Selenium Frequently used operation method summary
Summary of various for statements in Python
Summary of useful techniques for Python Scrapy
[Python] A memo of frequently used phrases (by myself) in Python scripts
Easy understanding of Python for & arrays (for super beginners)
Summary of Python arguments
[Python/Django] Summary of frequently used commands (2) <Installing packages>
Summary of frequently used commands (with petit commentary)
Selenium webdriver Summary of frequently used operation methods
Summary of tools for operating Windows GUI with Python
Summary of Pandas methods used when extracting data [Python]
Comparison table of frequently used processes of Python and Clojure
Python memo (for myself): Array
Summary of Python3 list operations
Python Tkinter notes (for myself)
[Python] Frequently used library code
python> Handling of 2D arrays
Frequently used subpackages of SciPy
Python frequently used code snippets
[For beginners] Summary of standard input in Python (with explanation)
Summary of Hash (Dictionary) operation support for Ruby and Python
What is Python? What is it used for?
8 Frequently Used Commands in Python Django
A brief summary of Python collections
Frequently used Linux commands (for beginners)
List of frequently used Linux commands
Introductory table of contents for python3
Record of Python introduction for newcomers
[For competition professionals] Summary of doubling
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
A brief summary of Graphviz in python (explained only for mac)
Installation summary often used for AI projects
Python Summary
Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~
[Linux] Review of frequently used basic commands 2
[Python] Minutes of study meeting for beginners (7/15)
Summary of methods often used in pandas
Summary of methods for automatically determining thresholds
[python] Frequently used techniques in machine learning
Python summary
[Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)>
[Python2.7] Summary of how to use unittest
A summary of Python e-books that are useful for free-to-read data analysis
Modules of frequently used functions in Python (such as reading external files)
Summary of petit techniques for Linux commands
Pandas of the beginner, by the beginner, for the beginner [Python]
Bulk replacement of strings in Python arrays
AtCoder cheat sheet in python (for myself)
[Linux] Review of frequently used basic commands
Summary of built-in methods in Python list
Frequently used syntax memorandum for each language
[Machine learning] List of frequently used packages
Summary of how to use Python list
[Summary of books and online courses used for programming and data science learning]
[Python2.7] Summary of how to use subprocess
[Competitive programming] [Python3] Required knowledge, for myself