【问题标题】:how to create a for loop for this case in python?如何在python中为这种情况创建一个for循环?
【发布时间】:2016-09-21 16:17:37
【问题描述】:

也许标题看起来很奇怪,但我在某些时候被困在寻找如何创建这种情况的 for 循环:

我有一个具有这种格式的列表:

 data=[['nature', author1, author2, ...author n]
       ['sport', author1, author2, ....author n]
       ....
      ]

我试过这段代码:

        authors=[author1, author2, ...author n]
         for i in range(len(authors)):

        data = [['nature', function(names[i], 'nature')],
                ['sport', function(names[i], 'sport')
             ..]

但不幸的是,我猜它会以这种格式返回结果:

 data=[['nature', author1]
       ['sport', author1]
       ....
      ]

【问题讨论】:

  • 你能更清楚你想要的输出是什么吗?
  • 我想要的输出是第一个代码,但带有一个 for 循环
  • 给我几分钟,我会给你一个工作代码的例子。我不明白你的挑战在哪里。也许我的代码会清除它。你用的是什么 Python 版本?我在 3.5 上只是想确保我的代码对你有用。
  • 我正在使用 python2.7

标签: python django for-loop


【解决方案1】:

你想要的是这些方面的东西吗?

>>> data=[['nature', 'author1', 'author2', 'author3'],['sport', 'author1', 'author2', 'author3'],['Horses', 'author1', 'author2']]
>>> for i in range(len(data)):
        for x in range(1, len(data[i])):
            print data[i][0], data[i][x] 
nature author1
nature author2
nature author3
sport author1
sport author2
sport author3
Horses author1
Horses author2

【讨论】:

    【解决方案2】:
    ary_grp_Example = [["AA1", "BB1"], ["CC2", "DD2"],["EE3","FF3"]] ### Dimension as a three column matrix array (list)
    ary_grp_Example.pop(0)  ## Kill the first record leaving two
    
    #Loop through by row (x)
    for int_CurCount in range(0,len(ary_grp_Example)): 
          print ("Row: " + str(int_CurCount) 
                 + " was #" 
                 + str(ary_grp_Example[int_CurCount][0]) 
                 + "#" + str(ary_grp_Example[int_CurCount][1]) 
                 +"#")
    
    #Loop through by Cell (x,y) 
    for int_RowCount in range(0,len(ary_grp_Example)): 
       for int_ColCount in range(0,len(ary_grp_Example[int_RowCount]) ):
          print ("Cell Data for location =(" 
                 + str(int_RowCount) + "," 
                 + str(int_ColCount) +  ") was #" 
                 + ary_grp_Example[int_RowCount][int_ColCount] 
                 + "#")  
    

    如果您使用两个数字 data[1][1] 或 data[1][2],则可以获取数据。有时称为矩阵数组或矩阵列表。它的 Python 名称似乎是一个“列表”,但它是一个多维列表,也就是矩阵数组/列表。

    例如,Sport Author 2 将是 data[1][2],因为它们是基于零的计数。

    要遍历所有数据,您必须像数据库结果集一样逐行逐列循环。

    【讨论】:

    • 当我尝试将数据声明为矩阵时,它会显示带有红色下划线的数据.. data[i][j]= ..
    • 尝试运行我的代码,确保它适用于您的 python 版本。然后将我的数据更改为您的数据。看看它是否仍然有效。让我知道这是否是你想要做的。
    【解决方案3】:

    将其转换为字典,然后遍历密钥对

    data = [['foo', 1, 2, 3], ['bar', 2,3,4]]
    dat = {i[0]: i[1:] for i in data }        
    for k, v in dat.items():
        print("{0}: {1}".format(k, v))
    
    
    Output
    bar: [2, 3, 4]
    foo: [1, 2, 3]
    

    除了.... 不要这样做

    我只是为了表明您的数据首先应该在字典中。

    【讨论】:

    • 我一般都是用矩阵数组/多维列表。您建议的“字典”有什么更好的地方?
    • @mthead - 我什至不确定问题是什么。我可能很快就会删除我的答案。但如果内部数组中的第一项始终是键,那么您应该使用为键值设计的数据结构。
    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多