Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python | Split String Hyphen

#1
Python | Split String Hyphen

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;942547&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p class="has-background" style="background-color:#b5b8f6"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Summary: </strong>Use <code>"given string".split('-')</code> to split the given string by hyphen and store each word as an individual item in a list. Some other ways to split using hyphen include using a list comprehension and the regex library.</p>
<h3><strong>Minimal Example</strong></h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
# Method 1
print(text.split("-"))
# Method 2
import re
print(re.split('-', text))
# Method 3
print(list(filter(None, text.split('-'))))
# Method 4
print([x for x in re.findall(r'[^-]*|(?!-).*$', text) if x != '']) # OUTPUT: ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']</pre>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2><strong>Problem Formulation</strong></h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dc.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Problem: </strong>Given a string, how will you split the string into a list of words using the hyphen as a delimiter?</p>
<h3><strong>Example</strong></h3>
<p>Let’s understand the problem with the help of an example.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Input:
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
# Output:
['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']</pre>
<p>Now without any further ado, let’s dive into the numerous ways of solving this problem.</p>
<h2><strong>Method 1: Using split()</strong></h2>
<p>Python’s built-in <code>split()</code> function splits the string at a given separator and returns a split list of substrings. Here’s how the <code>split()</code> function works: <code>'finxterx42'.split('x')</code> will split the string with the character ‘<code>x</code>’ as the delimiter and return the following list as an output: <code>['fin', 'ter', '42']</code>.</p>
<p><strong>Approach: </strong>To split a string by hyphen, you can simply pass the underscore as a separator to the <code>split('-')</code> function.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
print(text.split("-")) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-string-split/"><strong>Python String split()</strong></a></p>
<h2><strong>Method 2: Using re.split()</strong></h2>
<p>Another way of separating a string by using the underscore as a separator is to use the <code>re.split()</code> method from the regex library. The <code>re.split(pattern, string)</code> method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, <code>re.split('a', 'bbabbbab')</code> results in the list of strings <code>['bb', 'bbb', 'b']</code>.</p>
<p><strong>Approach: </strong>You can use the <code>re.split()</code> method as <code>re.split('-', text)</code> where <code>'-'</code> returns a match whenever the string contains a hyphen. Whenever any hyphen is encountered, the text gets separated and the split substring gets stored as an element within the resultant list.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red"
print(re.split('-', text)) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-regex-split/"><strong>Python Regex Split</strong></a></p>
<h2><strong>Method 3: Using filter()</strong></h2>
<p><strong>Note: </strong>This approach is efficient when the resultant list contains empty strings along with substrings.</p>
<p>Python’s built-in <code>filter()</code> function filters out the elements that pass a filtering condition. It takes two arguments: function and iterable. The function assigns a Boolean value to each element in the iterable to check whether the element will pass the filter or not. It returns an iterator with the elements that passes the filtering condition.</p>
<p><strong>Approach: </strong>Use the <code>filter()</code> method to split the string by hyphen. The function takes <code>None</code> as the first argument and the list of split strings as the second argument. The <code>filter()</code> function then iterates through the list and removes any empty elements. As the <code>filter()</code> method returns an object, we need to use the <code>list()</code> to convert the object into a list.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red" print(list(filter(None, text.split('-')))) # ['Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-filter/"><strong>Python filter()</strong></a></p>
<h2><strong>Method 4: Using re.findall()</strong></h2>
<p>The <code>re.findall(pattern, string)</code> method scans the string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order- when scanning the string from left to right.</p>
<p><strong>Approach: </strong>You can use the <code>re.findall()</code> method from the regex module to split the string by hyphen. Use ‘<code>[^-]<em>|(?!-).</em>$</code>‘ as the pattern that can be fed into the <code>findall</code> function to solve the problem. It simply, means a set all characters that are joined by a hyphen will be grouped together.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
text = "Violet-Indigo-Blue-Green-Yellow-Orange-Red" print([x for x in re.findall(r'[^_]*', text) if x != '']) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: </strong><a href="https://blog.finxter.com/python-re-findall/"><strong>Python re.findall() – Everything You Need to Know</strong></a></p>
<h2><strong>Python | Split String by Dot</strong></h2>
<p>Now that we have gone through numerous ways of solving the given problem, here’s a similar programming challenge for you to solve.</p>
<p><strong>Challenge: </strong>You are given a string that contains dots in it. How will you split the string using a dot as a delimiter? Consider the code below and try to split the string by dot.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Input:
text = "stars.moon.sun.sky" # Expected Output:
['stars', 'moon', 'sun', 'sky']</pre>
<p>Try to solve the problem yourself before looking into the given solutions.</p>
<p><strong>Solution: </strong>Here are the different methods to split a string by using the dot as a delimiter/separator:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">text = "a*b*c"
# Method 1
print(text.split("*")) # Method 2
print(list(filter(None, text.split('*')))) # Method 3
import re
print([x for x in re.findall(r'[^/*]*|(?!/*).*$', text) if x != '']) # Method 4
print(re.split('[/*]', text))</pre>
<h2>Conclusion</h2>
<p>Hurrah! We have successfully solved the given problem using as many as four different ways. We then went on to solve a similar coding challenge. I hope this <a href="https://blog.finxter.com/"><strong>article</strong></a> helped you. Please <a href="https://blog.finxter.com/subscribe/"><strong>subscribe and stay tuned</strong></a> for more interesting articles!</p>
<p>Happy coding! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p><strong><em>Do you want to master the regex superpower?</em></strong> Check out my new book <em><strong><a href="https://blog.finxter.com/ebook-the-smartest-way-to-learn-python-regex/" target="_blank" rel="noreferrer noopener" title="[eBook] The Smartest Way to Learn Python Regex">The Smartest Way to Learn Regular Expressions in Python</a></strong></em> with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. </p>
</div>


https://www.sickgaming.net/blog/2022/12/...ng-hyphen/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016