[Python] Leave only the elements that start with a specific character string in the array

Introduction

memorandum. I want to keep only the elements that start with a specific string in the array. I have an array of ['hogehoge.form','hugahuga.form','hogehoge.form2'], and I want to keep only the elements that start with hogehoge and delete the other elements.

Solution

Use the string method startswith ()

Method that can determine the beginning of a character string

moji = 'hogehoge'

#Returns true
moji.startswith('hoge')

#Returns false
moji.startswith('huga')

Sample code

#Original array
moji_ary = ['hogehoge.form', 'hugahuga.form', 'hogehoge.form2']

#New array new_In ary, the original array moji_Of the elements of ary`ho`Insert only elements starting with
new_ary = [hoge for hoge in moji_ary if hoge.startswith('ho')]

#Output result['hogehoge.form', 'hogehoge.form2']
print(new_ary)

at the end

There is also a method ʻendswith () `to determine the mating (?) Of a string. You can find other useful things by google with "python string manipulation".

Reference site

Recommended Posts

[Python] Leave only the elements that start with a specific character string in the array
Extract only elements that meet specific conditions in Python
Word count that counts only words that start with a capital letter in python
Get the number of specific elements in a python list
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
[Introduction to Python] How to split a character string with the split function
[Golang] Check if a specific character string is included in the character string
[Python] Combine all the elements in the array
The story of creating a bot that displays active members in a specific channel of slack with python
The eval () function that calculates a string as an expression in python
[Introduction to Python] How to write a character string with the format function
Tips: [Python] Extract only lines that do not contain a specific string
[Python] Get the files in a folder with Python
Get only the subclass elements in a list
Output in the form of a python array
[Python] Manipulating elements in a list (array) [Sort]
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
Check if the string is a number in python
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
Output a character string with line breaks in PyYAML
A memo that I touched the Datastore with python
Get the value of a specific key in a list from the dictionary type in the list with Python
It seems that a new lottery with a total score will start in the sports lottery,
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Published a library that hides character data in Python images
Let's create a script that registers with Ideone.com in Python.
How to quickly count the frequency of appearance of characters from a character string in Python?
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
Play a sound in Python assuming that the keyboard is a piano keyboard
pandas Fetch the name of a column that contains a specific character
Summary of character string format in Python3 Whether to live with the old model or the new model
How to make a string into an array or an array into a string in Python
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
[Introduction to Python] How to output a character string in a Print statement
Try to extract a character string from an image with Python3
A function that measures the processing time of a method in python
Read a file in Python with a relative path from the program
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
A story that didn't work when I tried to log in with the Python requests module
Create a random string in Python
Get the formula in an excel file as a string in Python
Solve the subset sum problem with a full search in Python
The story of making a module that skips mail with python
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
How to input a character string in Python and output it as it is or in the opposite direction.
Fill the string with zeros in python and count some characters from the string
A memo organized by renaming the file names in the folder with python
Receive a list of the results of parallel processing in Python with starmap
Create a record with attachments in KINTONE using the Python requests module
[Introduction to Python] Thorough explanation of the character string type used in Python!
How to sort by specifying a column in the Python Numpy array.
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Write the test in a python docstring
I made a character counter with Python
Display Python 3 in the browser with MAMP
Search the maze with the python A * algorithm
[Python] How to invert a character string
Run the Python interpreter in a script
What I did with a Python array
Generate a class from a string in Python