Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For doing a single replacement on a string works, you can use <a href="http://docs.python.org/3/library/stdtypes.html#str.rsplit" rel="nofollow"><code>str.split</code></a> to split the string by slashes. <code>str.split</code> will return a list of the substrings it extracted from the string. As you are only interested in the substring before the first slash, you just need to get the first element of the result list. You can further use the <code>maxsplit</code> parameter to restrict <code>str.split</code> from splitting the string more than once (to make it stop processing the string earlier).</p> <pre><code>&gt;&gt;&gt; 'AA/BB/CC'.split('/') ['AA', 'BB', 'CC'] &gt;&gt;&gt; 'AA/BB/CC'.split('/', 1) ['AA', 'BB/CC'] &gt;&gt;&gt; 'AA/BB/CC'.split('/', 1)[0] 'AA' </code></pre> <p>Now to perform the replacement across the list you have two choices: Either you replace the list <em>in place</em>, updating each element if necessary, or you create a new list where you apply the transformation on the fly. Note that you will always have to create new tuples though as they are immutable. As it’s generally a preferred way to solve things, I’m only showing you how to do the second way using list comprehension. But if you wanted to replace the elements in place, you would just have to loop through the list and update the list items.</p> <pre><code>&gt;&gt;&gt; lst = [[("AA", "AA/BB/AA"), ("QQ", "AB/AA")], [("CC", "CC/SS/AA"), ("QQ", "CC/CC")]] &gt;&gt;&gt; [[(a, b.split('/', 1)[0]) for a, b in x] for x in lst] [[('AA', 'AA'), ('QQ', 'AB')], [('CC', 'CC'), ('QQ', 'CC')]] </code></pre> <p>A list comprehension in general looks like this:</p> <pre><code>[t(x) for x in sequence] </code></pre> <p>For every element <code>x</code> in the <code>sequence</code> it will apply the transformation <code>t</code> to the element <code>x</code> and put that as an element in the list. So <code>[x for x in lst]</code> would just return an identical list <code>t</code> would be the identify function.</p> <p>The element <code>x</code> can also be an unpacked tuple. If the elements in the source list are two-element tuples, you can use <code>[t(a, b) for a, b in lst]</code> to unpack the elements from the tuple, apply the transformation and put that into the result list.</p> <p>In your case, we want to unpack the tuple, so we can work on the second element only. But we also want to keep it a tuple, so we put the result in a tuple again: <code>[(a, t(b)) for a, b in x]</code>. In this case, the transformation <code>t</code> would be the splitting I showed earlier.</p> <p>Now because your list is actually not a list of tuples, but a <em>list of lists of tuples</em>, we have to nest two list comprehensions. Similar to above <code>[[y for y in x] for x in lst]</code> would reproduce a list of lists. Now as the inner list is a list of tuples, we can perform the transformation on the tuples there and put everything together.</p>
    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. 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