Trying to get all matches in my list in the correct form. example:
import re
regex = re.compile(r'(\d{1,4})')
text = ['(261, 264)', '(458, 393)', '(960, 540)', '(542, 424)', '(541, 424)']
print(regex.findall(text[0]))
print(list(filter(regex.findall, text)))
outputs:
['261', '264']
['(261, 264)', '(458, 393)', '(960, 540)', '(542, 424)', '(541, 424)']
I'm trying to get the bottom part in the same format as the top (without the parenthesis or comma). Is this possible? I can't seem to get these functions to return all matches in my list (I want to make it in 1 line like the list(filter()) is if at all possible)
edit:: desired output:
['261', '264', '458', '393', '960', '540', '542', '424', '542', '424']
Try this:
import re
regex = re.compile(r'(\d{1,4})')
text = ['(261, 264)', '(458, 393)', '(960, 540)', '(542, 424)', '(541, 424)']
print(regex.findall(text[0]))
print([elem for tup in map(regex.findall, text) for elem in tup])
list comprehension that merges all matches (by iterating over all of them):
import re
regex = re.compile(r'(\d{1,4})')
text = ['(261, 264)', '(458, 393)', '(960, 540)', '(542, 424)', '(541, 424)']
print([x for t in text for x in regex.findall(t)])
Here is my solution:
This will leave you with the following result:
[['261', '264'],
['458', '393'],
['960', '540'],
['542', '424'],
['541', '424']]
Which will leave you with the result you want
['261', '264', '458', '393', '960', '540', '542', '424', '541', '424']
import itertools
import re
original_list = ['(261, 264)', '(458, 393)', '(960, 540)', '(542, 424)', '(541, 424)']
regex = re.compile(r"\d\d\d")
matches_per_element = [regex.findall(txt) for txt in original_list]
flattened_list = list(itertools.chain(*matches_per_element))
### Display the result
print(flattened_list)
>>> ['261', '264', '458', '393', '960', '540', '542', '424', '541', '424']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.