Home>
I want to achieve
I want to implement the process to remove the character string specified in the list from the following original data
original data['oneXXXaaa','twoXXXbbb','three999aaa', '000111222', '999yyyy', '888bbb000']
Exclusion list['XXX','bbb']
Processing result['three999aaa', '000111222', '999yyyy']
Clogged pointI was able to determine if the specified character string was included by the following method.
When the target to be excluded is a list, I am wondering how to implement it to make it simple.
for target in target_list:
if'except_word' in target:
-
Answer # 1
-
Answer # 2
Built-in
all ()
By combining and the inclusion notation, you can write a little refreshingly.target_list = ['oneXXXaaa','twoXXXbbb','three999aaa', '000111222', '999yyyy', '888bbb000'] word_list = ['XXX','bbb'] result_list = [] for target in target_list: if all (word not in target for word in word_list): result_list.append (target)
-
Answer # 3
If you don't care about the processing speed, you can double the loop.
target_list = ['oneXXXaaa','twoXXXbbb', 'three999aaa', '000111222', '999yyyy', '888bbb000'] word_list = ['XXX','bbb'] result_list = [] for target in target_list: for word in word_list: if word in target: break else: else: result_list.append (target) print (result_list)
-
Answer # 4
This is a code that includes bsdfan's code. It's almost the same, but I'll show you.
target_list = ['oneXXXaaa','twoXXXbbb','three999aaa', '000111222', '999yyyy', '888bbb000'] word_list = ['XXX','bbb'] result_list = [target for target in target_list if all (word not in target for word in word_list)] print (result_list) # ['three999aaa', '000111222', '999yyyy']
Related articles
- python - please tell me about the processing contents of opencv background subtraction
- python about iterative processing with specified numbers
- python 3x - i'm not sure about asynchronous processing (asyncio)
- python - about multiple processing and loop processing in discordpy
- python - about image display and position of main ()
- python 3x - about the amount of data for object detection using machine learning
- python 3x - about functions used in python for statement
- about image output of python
- python 3x - please teach about the exchange of information with qthead
- python 3x - processing to jump to the link destination using chrome driver in python
- python - about x-axis adjustment in matplotlib
- python - about "" "of" "" select === = "" "
- i don't know the order of processing higher-order functions in python
- python, about the fire spread step of forest fire simulation
- python - about hamiltonian neural networks
- python - about write loop to csv
- about python argument and data definition
- python 3x - about downloading anaconda
- python - tkinter parallel processing
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
If you want to write in an easy-to-understand manner, it is easier to understand by breaking it down into parts rather than doing it all at once.
If you want to write it short, it is as follows, but it is not easy to understand.