Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在嵌套for和if循环中重写以下邮政编码?_Python_If Statement_For Loop - Fatal编程技术网

Python 如何在嵌套for和if循环中重写以下邮政编码?

Python 如何在嵌套for和if循环中重写以下邮政编码?,python,if-statement,for-loop,Python,If Statement,For Loop,列表l包含以下元素 l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2'] 列表l用元组分成两对,这样每个字母对应A:6,G:6,C:35等等 如果值小于10,则字母表将转换为Z。 代码如下: 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

列表l包含以下元素

l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
列表l用元组分成两对,这样每个字母对应A:6,G:6,C:35等等 如果值小于10,则字母表将转换为Z。 代码如下:

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)
pairs=[]
对于a,b in(子部分拆分(无,1)对于l中的子部分):
pairs.append([((“Z”,i)if int(i)<10 else(c,i)for c,i in zip(a,b.split())]))
打印(双)
如何使用嵌套for和if循环编写相同的代码?(我需要做这个练习来帮助更好地理解Python中的编码)

以下是我的尝试:

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
pairs=[]
对于a,b in(sub.split(无,1)#sub.split(无,1)是什么
对于l中的sub:
如果int(i)<10:
pairs.append(“Z”,i)
其他:
pairs.append(c,i)
打印对
注1:如果有人能帮助我更好地界定问题-针对问题,请提出修改建议


上述问题和代码(学分:p.坎宁安)可以找到

对代码进行反向工程,通常在python控制台上工作是最好的方法。因此,我将开始逐个分解不同的语句,看看每个语句的作用。现在,可能会有许多不同的方法来解释/重写代码,所以我的方法有望帮助您理解过程

您的代码很好,但最终所需的输出存在一个问题和一个差异

  • 问题出现在第一个循环中:您应该将
    替换为a,b
    ,并将
    替换为l
    行中的sub,因为这会给您一个关于
    sub
    未定义的错误
  • 不同之处在于,
    pairs
    是一个列表列表,而您正试图附加一个元组。我之所以说“try”,是因为该语句无效,应该写成
    pairs.append((“Z”,I))例如,
    。但是仍然忽略了原始代码中定义的微妙的
    列表
    。事实上,这里有一个
    对。append([])
    ,其中的完整逻辑创建了所谓的
    列表理解
  • 不过,你的工作做得很好

    最终的“新代码”如下所示,您可以在以下位置看到它的实际操作:

    l=['AGCTT 6 6 35 25 10','AGGGT 7 7 28 29 2']
    成对=[]
    #对于列表中的每个元素
    对于l中的sub:
    #我们将创建一个新的空列表
    新列表=[]
    #将“sub”拆分为字符串和数字部分
    a、 b=子拆分(无,1)
    #此外,将数字拆分为一个列表
    数字=b.拆分()
    #现在使用位置索引,我们将。。。
    对于范围(len(a))内的x:
    #…创建一个从字符串中提取一个字符的新元组
    #“a”和列表“编号”中的一个编号
    新元组=(a[x],数字[x])
    #如果该值小于10,则。。。
    如果int(数字[x])<10:
    #…让我们把“Z”
    新元组=(“Z”,数字[x])
    #将其附加到临时列表中
    新列表。追加(新元组)
    #序列完成后,将其添加到主列表中
    pairs.append(新列表)
    #完成后,打印列表
    打印对
    
    Hi Sal,非常感谢。我修改了我的代码。但是我发现uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu把这当作建设性的批评。变量太多,循环太多。因为这是一个学习练习,试着简化你的方法。我不明白,我必须在哪里更新我的顺序?我不会对我的简历做任何更改。这是我唯一能改变的品质。不,谢谢你让我知道,我很感激:)。我是一个新用户,所以当我继续前进的时候,我一定会努力提高。我们都在这里学习。我在这里制作了一个新版本,其中有更多的注释:您将看到“序列”被固定在循环中的最后一个元素。
    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
            new_list.append(new_tuple)    
    
        # once the sequence is complete, add it to the main list
        pairs.append(new_list)   
    
    # When it's all done, print the list
    print pairs