Bulk replacement of strings in Python arrays

When storing the acquired character string in an array while scraping Extra line breaks in the array ... At that time, I came across a scene where I wanted to replace or delete it. With the meaning of a memorandum

What you want to do

python


str_list = ['Ai\n Ueo', 'Kaikukeko', 'Sashi\n suse\n', 'Tachitsute\n and', 'What\n nu\n\n']
str_list2 = [['Ai\n Ueo', 'Kaikukeko'], ['Sashi\n suse\n', 'Tachitsute\n and', 'What\n nu\n\n'], ['Hahifu\n Heho', 'Ma\n Mimumemo\n'], ['yayuyo'], ['Lari\n Rurero', 'Won']]

If you scrape like this, it may contain characters other than the one you want

I want to simplify this

usually


replace_list = []
for s in str_list:
    if '\n' in s:
        text = s.replace('\n', '')
        replace_list.append(text)
    else:
        replace_list.append(s)

print(replace_list)
>>['AIUEO', 'Kaikukeko', 'SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it']

for i in str_list2:
    for s in i:
        if '\n' in s:
            text = s.replace('\n', '')
            replace_list2.append(text)
        else:
            replace_list2.append(s)
    replace_list3.append(replace_list2)
    replace_list2 = []

print(replace_list3)
>>[['AIUEO', 'Kaikukeko'], ['SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it'], ['Hahifuheho', 'Mamimumemo'], ['yayuyo'], ['Larry Lero', 'Won']]

I was able to simplify

Comprehension notation


replace_list = [s.replace('\n', '') for s in str_list]
replace_list2 = [[s.replace('\n', '') for s in text] for text in str_list2]

print(replace_list)
print(replace_list2)
>>['AIUEO', 'Kaikukeko', 'SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it']
>>[['AIUEO', 'Kaikukeko'], ['SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it'], ['Hahifuheho', 'Mamimumemo'], ['yayuyo'], ['Larry Lero', 'Won']]

Which one is better just by using the inclusion notation

Recommended Posts

Bulk replacement of strings in Python arrays
Compare strings in Python
Reverse strings in Python
Search for strings in Python
Equivalence of objects in Python
Implementation of quicksort in Python
[Small story] [Python] Replace strings in 2D arrays with numbers
Pixel manipulation of images in Python
python web scraping-get elements in bulk
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Law of large numbers in python
Implementation of original sorting in Python
Replacement of strings containing Linux spaces
Reversible scrambling of integers in Python
Extract strings from files in Python
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
(Note) Bulk upgrade of python installed packages
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
[Python] Various combinations of strings and values
Practical example of Hexagonal Architecture in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Draw a graph of a quadratic function in Python
Python 3.3 in Anaconda
Geocoding in python
Introduction of Python
SendKeys in Python
3 ways to parse time strings in python [Note]
[Python] Sort the list of pathlib.Path in natural sort
Receive websocket of kabu station ® API in Python
Meta-analysis in Python
Convert PDFs to images in bulk with Python
Unittest in python
Summary of how to import files in Python 3
Project Euler # 10 "sum of prime numbers" in Python
Unattended operation of Google Spreadsheets (etc.) in Python