Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using list comprehensions:</p> <pre><code>&gt;&gt;&gt; [[tuple(map(int, pair)) + (2,) for pair in pairs] for pairs in l] [[(100, 200, 2), (300, 400, 2), (500, 600, 2)], [(100, 200, 2)], [(100, 200, 2)]] </code></pre> <p>Or without the map:</p> <pre><code>&gt;&gt;&gt; [[(int(a), int(b), 2) for a, b in pairs] for pairs in l] [[(100, 200, 2), (300, 400, 2), (500, 600, 2)], [(100, 200, 2)], [(100, 200, 2)]] </code></pre> <h3>Edit</h3> <p>Even with further checks, you can still use list comprehension. I assume that the if/else section you have added to your question should be applied to every pair, and the resulting tuple would be <code>(addr_from, addr_to, 2)</code> then, right?</p> <pre><code>def processPair(a, b): if a.isdigit(): a = int(a) elif a.isalnum(): a = re.sub(r'((?:[A-Z].*?)?(?:\d.*?)?[A-Z]+)(\d+)', r'\1%\2', a) if b.isdigit(): b = int(b) + 2 elif b.isalnum(): b = re.sub(r'((?:[A-Z].*?)?(?:\d.*?)?[A-Z]+)(\d+)', r'\1%\2', b) return (a, b, 2) </code></pre> <p>Here I have defined a function that processes the tuple <code>(a, b)</code> as you did in your question. Note that I have changed it to just modify the values of the variables and return a finished tuple (with the added 2) instead of appending it to some global list.</p> <p>I have also simplified it a bit. <code>a.isdigit() is True</code> is the same as <code>a.isdigit()</code> as that already returns a boolean value. Same for <code>a.isdigit() == False</code>, which is the same as <code>not a.isdigit()</code>. In that situation you can also remove redundant checks. After checking <code>a.isdigit()</code> on the <code>if</code>, you do not need to check its opposite on the <code>elif</code>; it is guaranteed to be false, as you have already checked it before.</p> <p>That being said, when you have said function, you can then use list comprehensions again, to get your output. Of course with your example <code>l</code>, this is a bit boring:</p> <pre><code>&gt;&gt;&gt; [[processPair(*pair) for pair in pairs] for pairs in l] [[(100, 202, 2), (300, 402, 2), (500, 602, 2)], [(100, 202, 2)], [(100, 202, 2)]] </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. 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