Biotechgeek Biotechgeek - 1 year ago 88
Python Question

How to rewrite the following zip code in nested for and if loops?

List l has the following elements

l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

List l was split into pairs using tuples, such that every alphabet corresponded as A:6, G:6, C:35 ..etc
If a value for less than 10, then the alphabets were converted to Z.
The following is the code:

pairs = []
for a, b in (sub.split(None, 1) for sub in l):
pairs.append([("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a,b.split())])

How can one code the same thing using nested for and if loops? (I need to do this exercise to help understand coding in Python better)

Here is my attempt:

pairs =[]
for a, b in (sub.split(None, 1) #What is sub.split(None,1)
for sub in l:
if int(i) < 10:
print pairs

Note 1: If someone could help me frame the question better- specific to the questions, please do suggest changes

The above question and code (credits: P. Cunningham) can be found here

sal sal
Answer Source

To reverse engineer the code, usually working on the python console is the best way to proceed. So, I would start taking the different statements apart one by one and see what each does. Now, there will probably be many different ways to explain/rewrite that code, so mine will be one that hopefully will help you understand the process.

Your code is good, but there is one problem and one discrepancy to the final desired output.

  1. The problem is at the first loop: you should swap the for a, b with the for sub in l line, because this should give you an error about sub not being defined or so.
  2. The discrepancy is that pairs is a list of lists, and instead you are trying to append a tuple. I say "try" because the statement is invalid and should be written as pairs.append(("Z",i)) for example. But still misses the subtle list which is defined in the original code. In fact there you have a pairs.append([]), and the full logic inside creates what is called a list comprehension.

Nevertheless, very good work from your part.

The final "new code" looks like below and you can see it in action at:

l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
pairs = []
# for each element in the list
for sub in l:   
    # we will create a new empty list
    new_list = []

    # split 'sub' into the string and the numbers parts
    a, b = sub.split(None, 1)   

    # further, split the numbers into a list
    numbers = b.split()

    # now using positional index, we will...
    for x in range(len(a)):
        # ... create a new tuple taking one character from string 
        # 'a' and one number from list 'numbers'
        new_tuple = ( a[x], numbers[x] )

        # if the value is less than 10, then...
        if int(numbers[x]) < 10:
            # ... let's just put "Z" 
            new_tuple = ("Z", numbers[x])   

        # append this to the temporary list

    # once the sequence is complete, add it to the main list

# When it's all done, print the list
print pairs
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download