【问题标题】:How to iterate over a list if lists in Python?如果 Python 中的列表,如何迭代列表?
【发布时间】:2019-12-28 19:25:49
【问题描述】:

我有这个清单:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

并尝试对其进行迭代以获得值及其位置,如下所示:

date    1
country 1
date    2
country 1
date    2

它应该稍后存储在 pandas df 中,值可以不同,没有修复。

原来是字典列表:

my_original_list = [
    [{'name': 'ga:date'}],
    [{'name': 'ga:country'}, {'name': 'ga:date'}],
    [{'name': 'ga:country'}, {'name': 'ga:date'}]
]

# But I got the values out of it in a list:
my_list = [li['name'] for li in my_original_list]

# the result
my_list = [
    ['ga:date'], 
    ['ga:country', 'ga:date'],
    ['ga:country', 'ga:date']
]

已经想好怎么弄了,不胜感激

【问题讨论】:

    标签: python pandas list loops dictionary


    【解决方案1】:

    将列表推导与enumerateflattening 一起用于元组列表:

    my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]
    
    x = [(b, a) for i in my_list for (a, b) in enumerate(i, 1)]
    print (x)
    [('ga:date', 1), ('ga:country', 1), ('ga:date', 2), ('ga:country', 1), ('ga:date', 2)]
    
    df = pd.DataFrame(x, columns = ['field','listIndex'])
    print (df)
            field  listIndex
    0     ga:date          1
    1  ga:country          1
    2     ga:date          2
    3  ga:country          1
    4     ga:date          2
    

    或者如果可能的话改变列的位置:

    x1 = [z for i in my_list for z in enumerate(i, 1)]
    print (x1)
    [(1, 'ga:date'), (1, 'ga:country'), (2, 'ga:date'), (1, 'ga:country'), (2, 'ga:date')]
    
    df = pd.DataFrame(x1, columns = ['listIndex','field'])
    print (df)
       listIndex       field
    0          1     ga:date
    1          1  ga:country
    2          2     ga:date
    3          1  ga:country
    4          2     ga:date
    

    如果需要删除:之前的值:

    my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]
    
    x = [(b.split(':')[-1], a) for i in my_list for (a, b) in enumerate(i, 1)]
    print (x)
    [('date', 1), ('country', 1), ('date', 2), ('country', 1), ('date', 2)]
    
    df = pd.DataFrame(x, columns = ['field','listIndex'])
    print (df)
         field  listIndex
    0     date          1
    1  country          1
    2     date          2
    3  country          1
    4     date          2
    

    【讨论】:

      【解决方案2】:

      您可以为此使用枚举:

      my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]
      
      for sublist in my_list:
          for position, entry in enumerate(sublist):
              print(entry, position + 1)  # +1 to count positions starting at 1 instead of 0.
      

      【讨论】:

      • 请注意,这里enumerate(sublist, start=1) 每次都避免position + 1...
      【解决方案3】:

      这个怎么样?

      import pandas
      
      my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]
      df = pandas.DataFrame(data=[(sublist[i],i) for sublist in my_list for i in range(len(sublist))], columns=["field", "listIndex"])
      

      结果:

              field  listIndex
      0     ga:date          0
      1  ga:country          0
      2     ga:date          1
      3  ga:country          0
      4     ga:date          1
      

      【讨论】:

        猜你喜欢
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多