【问题标题】:how to store values from a list of lists into a separate list in python?如何将列表列表中的值存储到python中的单独列表中?
【发布时间】:2020-07-28 12:40:19
【问题描述】:

我有一个列表列表,并希望将其中的一些值存储在一个单独的列表中。我的列表名为data,它是从数据文件中解析出来的。数据文件的标题标记了它下面的值所代表的内容。

标题看起来像这样: fruit, animal, technology, drink, color

data 的前几个元素看起来像这样:

[[apple,  dog,  phone,    water, black], 
     [banana, cat,  laptop,   milk,  pink], 
     [melon,  bird, computer, juice, green]]

我想按类别对值进行分组,以便对其进行排序。这是一个相当大的列表,所以我想用循环或其他东西遍历它,但我不知道该怎么做。

我想要一个与此类似的输出,但对于外部列表中的所有元素:

fruits = [apple, banana, melon]

我该怎么做?

感谢任何帮助,因为我是 python 新手,谢谢!

【问题讨论】:

  • 你是如何分类的?那些应该是列表中的字符串文字,还是对对象的引用?
  • 您希望计算机何时将apple 视为水果,将dog 视为动物?
  • 我的实际列表中充满了数字,但我认为这样做会使我希望它如何分类看起来更清楚。我将进行编辑以使其更清晰!

标签: python list loops


【解决方案1】:

您真正想做的是旋转二维列表。有一个很好的答案 here 关于如何做到这一点,但我将总结其背后的主要思想。

如果 x 是您提供的数组,即

x = [[apple,  dog,  phone,    water, black], 
     [banana, cat,  laptop,   milk,  pink], 
     [melon,  bird, computer, juice, green]]

那么你只需要

rotated = list(zip(*x))
fruits = rotated[0]
animals = rotated[1]
# etc.

它的作用如下:

  • *x 将 x 解包,以便使用
    zip([apple, dog, ...], [banana, cat, ...], [melon, bird, ...]) 调用 zip
  • zip 然后拉开内部列表,并构造一个 zip 对象。
  • list 将其转换回列表。

这假定fruit 将始终位于索引 0,animal 将始终位于索引 1,等等。

如果不是这样,那么您必须为每个类别构建一组值,然后手动遍历列表以对其进行排序。

knownFruits = {banana, melon, apple, pineapple, guava}
knownAnimals = {dog, cat, bird, leopard, shark}
fruits = []
animals = []

for row in x:
    for elem in row:
        if elem in fruits:
            fruits.append(elem)
        elif elem in animals:
            animals.append(elem)
#etc.

【讨论】:

    【解决方案2】:
    list_of_list = [ [apple, dog, phone, , water, black], 
                     [banana, cat, laptop, milk, pink], 
                     [melon, bird, computer, juice, green] ] 
    

    注意所有 list_of_list[i][0] 是水果,list_of_list[i][1] 是动物等等

    所以你想遍历列表并将列表中的每个项目分配给它们对应的输出列表

    # initialize the output lists
    fruits,animals,devices,drinks,colors = []
    for inner_list in list_of_lists:
        fruits.append(inner_list[0])
        animals.append(inner_list[1])
        # repeat for rest of list
    
    
    

    【讨论】:

      【解决方案3】:

      您可以使用带参数解包的 zip 将列表直接分成变量:

      data = [["apple",  "dog",  "phone",    "water", "black"], 
              ["banana", "cat",  "laptop",   "milk",  "pink"], 
              ["melon",  "bird", "computer", "juice", "green"]]
      
      fruits,animals,techs,drinks,colours = map(list,zip(*data))
      

      输出:

      print(fruits)  # ['apple', 'banana', 'melon']
      print(animals) # ['dog', 'cat', 'bird']
      print(techs)   # ['phone', 'laptop', 'computer']
      print(drinks)  # ['water', 'milk', 'juice']
      print(colours) # ['black', 'pink', 'green']
      

      如果您只想要水果或任何其他列组合,则可以忽略其他部分:

      fruits,*_ = map(list,zip(*data))
      
      _,animals,_,drinks,_ = map(list,zip(*data))
      

      或者你可以把你的数据变成字典:

      keys     = ("fruits","animals","techs","drinks","colours")
      dataDict = { key:list(values) for key,values in zip(keys,zip(*data)) }
      
      print(dataDict)
      
      {
       'fruits':  ['apple', 'banana', 'melon'], 
       'animals': ['dog', 'cat', 'bird'], 
       'techs':   ['phone', 'laptop', 'computer'], 
       'drinks':  ['water', 'milk', 'juice'], 
       'colours': ['black', 'pink', 'green']
      }
      

      【讨论】:

      • 天哪,我希望我早点看到最后一点代码,水果,动物,技术,饮料,颜色 = map(list,zip(*x))!会让我免于手动命名。谢谢你!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2013-07-23
      • 1970-01-01
      相关资源
      最近更新 更多