Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the number of keys in the list is large, and the number of the occurences in the string is low (and mostly zero), then you could iterate over the occurences of the ampersands in the string, and use the dictionary keyed by the first character of the substrings. I don't code often in python so the style might be a bit off, but here is my take at it:</p> <pre><code>str = "The &amp;yquick &amp;cbrown &amp;bfox &amp;Yjumps over the &amp;ulazy dog" dict = {"&amp;y":"\033[0;30m", "&amp;c":"\033[0;31m", "&amp;b":"\033[0;32m", "&amp;Y":"\033[0;33m", "&amp;u":"\033[0;34m"} def rep(s): return dict["&amp;"+s[0:1]] + s[1:] subs = str.split("&amp;") res = subs[0] + "".join(map(rep, subs[1:])) print res </code></pre> <p>Of course there is a question what happens when there is an ampersand that is coming from the string itself, you would need to escape it in some way before feeding through this process, and then unescape after this process. </p> <p>Of course, as is pretty much usual with the performance issues, timing the various approaches on your typical (and also worst-case) dataset and comparing them is a good thing to do.</p> <p>EDIT: place it into a separate function to work with arbitrary dictionary:</p> <pre><code>def mysubst(somestr, somedict): subs = somestr.split("&amp;") return subs[0] + "".join(map(lambda arg: somedict["&amp;" + arg[0:1]] + arg[1:], subs[1:])) </code></pre> <p>EDIT2: get rid of an unneeded concatenation, seems to still be a bit faster than the previous on many iterations.</p> <pre><code>def mysubst(somestr, somedict): subs = somestr.split("&amp;") return subs[0].join(map(lambda arg: somedict["&amp;" + arg[0:1]] + arg[1:], subs[1:])) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload