Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate over Python list, preserving structure of embedded lists
    primarykey
    data
    text
    <p>Python newbie here. I'm trying to perform a basic function on a list containing lists of various lengths. Within these lists are tuples containing pairs of elements that I intend to use as ranges. I want to convert these elements into integers, and then create a new tuple with the integers, followed by <code>2</code>, which will represent the iterator. </p> <p>I want <code>l</code>:</p> <pre><code>l = [[('100', '200'), ('300', '400'), ('500', '600')], [('100', '200')], [('100', '200')]] </code></pre> <p>To be replaced by <code>l_upd</code></p> <pre><code>l_upd = [[(100, 200, 2), (300, 400, 2), (500, 600, 2)], [(100, 200, 2)], [(100, 200, 2)]] </code></pre> <p>This obviously doesn't work (and I couldn't figure out how to get the <code>2</code> placed as an element:</p> <pre><code>l1 = [] l2 = [] for pairs in l: for pair in pairs: l1.append(int(i[0])) l2.append(int(i[1])) l_upd = zip(l1, l2) </code></pre> <p>EDIT: I would prefer to not use a list comprehension method, because I need to include an <code>if</code>, <code>else</code> statement. Some of the elements contain letters, and some are empty.</p> <p>Something like the following is needed for these exceptions:</p> <pre><code>for pair in pairs: if pair[0].isdigit(): addr_from.append(int(i[0])) elif pair[0].isalnum() is True and pair[0].isdigit is False: addr_from.append(re.sub(r'((?:[A-Z].*?)?(?:\d.*?)?[A-Z]+)(\d+)',r'\1%\2',pair[0])) else: addr_from.append(pair[0]) if pair[1].isdigit(): addr_to.append(int(i[1]) + 2) elif pair[1].isalnum() is True and pair[1].isdigit is False: addr_to.append(re.sub(r'((?:[A-Z].*?)?(?:\d.*?)?[A-Z]+)(\d+)',r'\1%\2',pair[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.
 

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