dimanche 11 septembre 2016

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

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())])
print(pairs)

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:
            pairs.append("Z",i)
        else:
            pairs.append(c,i)
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

Aucun commentaire:

Enregistrer un commentaire