<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Function in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/function/m-p/1037616#M60443</link>
    <description>&lt;P&gt;We all await the &lt;STRONG&gt;'walrus'&lt;/STRONG&gt;... python 3.8, where are you!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# text = "The quick brown fox jumps over the lazy dog"
# replacements = [("brown", "red"), ("lazy", "quick")]
[text := text.replace(a, b) for a, b in replacements]
# text = 'The quick red fox jumps over the quick dog'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;(Xavier Guidot in Joshua's link )&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Mar 2021 15:15:21 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2021-03-17T15:15:21Z</dc:date>
    <item>
      <title>Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037461#M60430</link>
      <description />
      <pubDate>Sun, 21 Mar 2021 11:44:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037461#M60430</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-21T11:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037465#M60431</link>
      <description>Easiest way is probably just to chain it.&lt;BR /&gt;&lt;BR /&gt;foo = my.read().replace(‘+’, ‘-‘).replace(‘!’, ‘-‘)&lt;BR /&gt;&lt;BR /&gt;try is a reserved word (part of try-except) so I’d suggest you change it to a different name.&lt;BR /&gt;</description>
      <pubDate>Wed, 17 Mar 2021 03:06:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037465#M60431</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-03-17T03:06:22Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037470#M60432</link>
      <description>&lt;P&gt;leaving out regular expressions or the string "translate" options&lt;/P&gt;&lt;P&gt;Code Golf&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;x = '+-!#'
y = "ab+!#-cd"
[i if i not in x else "-" for i in y ]
['a', 'b', '-', '-', '-', '-', 'c', 'd']

def goodbad(y, x):
    if y not in x:
        return i
    return "-"
    
for i in y:
    print(goodbad(i, x))
a
b
-
-
-
-
c
d&lt;/LI-CODE&gt;&lt;P&gt;Or this&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def goodbad(y, x):
    return "".join(["-" if i in x else i for i in y  ])
    
goodbad(y, x)
'ab----cd'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 03:45:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037470#M60432</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-17T03:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037481#M60434</link>
      <description>&lt;P&gt;I am tempted by&amp;nbsp;@Anonymous User&amp;nbsp; but what if I want to replace three characters? Too many .replace()'s and it starts to get darn un-Pythonic.&lt;/P&gt;&lt;P&gt;I don't know why&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;did not go there with reg expressions. They are just so beautiful...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; import re
&amp;gt;&amp;gt;&amp;gt; text = foo!+bar'
&amp;gt;&amp;gt;&amp;gt; re.sub('[!+]', '-', text)
'foo--bar'
&amp;gt;&amp;gt;&amp;gt; re.sub('[!+].', '-', text)
'foo-bar'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 04:27:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037481#M60434</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-03-17T04:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037549#M60437</link>
      <description>&lt;P&gt;because &lt;STRONG&gt;'re'&lt;/STRONG&gt; only works with text... &lt;STRONG&gt;'in'&lt;/STRONG&gt; can be used with other object types&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 10:45:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037549#M60437</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-17T10:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037592#M60441</link>
      <description>&lt;P&gt;There is a very long discussion on this topic with lots of suggestions over @ &lt;A href="https://stackoverflow.com/questions/6116978/how-to-replace-multiple-substrings-of-a-string" target="_blank"&gt;python - How to replace multiple substrings of a string? - Stack Overflow&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 14:14:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037592#M60441</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-17T14:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037600#M60442</link>
      <description>&lt;P&gt;As much as regex is still black magic to me, it does seem like the best solution here.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 14:32:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037600#M60442</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-17T14:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037616#M60443</link>
      <description>&lt;P&gt;We all await the &lt;STRONG&gt;'walrus'&lt;/STRONG&gt;... python 3.8, where are you!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# text = "The quick brown fox jumps over the lazy dog"
# replacements = [("brown", "red"), ("lazy", "quick")]
[text := text.replace(a, b) for a, b in replacements]
# text = 'The quick red fox jumps over the quick dog'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;(Xavier Guidot in Joshua's link )&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 15:15:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037616#M60443</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-17T15:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037622#M60444</link>
      <description>&lt;P&gt;I don't know why that would make it un-pythonic since it is readable, simple, practical, and easy to implement/ explain without the assistance of code comments or knowledge of regex symbols/ functionalities. I say easier because we code for people, not machines and it may 'look' bad with the chaining, but it is easily understood by people at all skill levels vs trying to decipher regex expressions. ...A newbie might think that you are replacing [!+]. in the string at first glance and try to 'fix' it without looking at what the expression is doing.&lt;/P&gt;&lt;P&gt;To answer the 'what if I wanted to' question, (couldn't tell if you meant they are together or is a third character) add another replace. If you feel there are too many replaces being chained in your code, just add the character to a list and iterate over them:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;charReplace = ['+', '!', '.', 'some']

inString = 'awesome this+ is a. Message!'

for chr in charReplace:
    if chr in inString:
        inString = inString.replace(chr, '-')&lt;/LI-CODE&gt;&lt;P&gt;I do think that regex has a place and that was a nice, succinct re.sub example you provided and if it works, that is great- I personally think that regex can introduce complexity and over-complicate code so I use it sparingly when other methods would become too complex/complicated.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 15:37:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037622#M60444</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-03-17T15:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1037693#M60445</link>
      <description>&lt;P&gt;I had to &lt;A href="https://docs.python.org/3/whatsnew/3.8.html" target="_self"&gt;learn more&lt;/A&gt; about this walrus you speak of.&lt;/P&gt;&lt;P&gt;"There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus."&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 16:57:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1037693#M60445</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-17T16:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1039323#M60522</link>
      <description>&lt;P&gt;Yes, the walrus is an interesting addition.&amp;nbsp; That said, I still can't decide if it is quite elegant or a bit hackish.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 15:23:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1039323#M60522</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-22T15:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Function</title>
      <link>https://community.esri.com/t5/python-questions/function/m-p/1039345#M60524</link>
      <description>&lt;P&gt;There was quite the controversy surrounding it initially&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 15:54:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function/m-p/1039345#M60524</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-22T15:54:11Z</dc:date>
    </item>
  </channel>
</rss>

