【问题标题】:How to update a dynamic list in python? [closed]如何在python中更新动态列表? [关闭]
【发布时间】:2021-06-13 06:04:31
【问题描述】:

我有一个列表,其中包含 Business = ['Company name','Mycompany',Revenue','1000','Income','2000','employee','3000','Facilities','4000 ','Stock','5000'] ,列表结构的输出如下图所示:

Company        Mycompany
Revenue        1000
Income         2000
employee       3000
Facilities     4000
Stock          5000

动态列表已更新 ***

对于列表的每次迭代和列表中的某些项目是 不见了

***。例如执行 1,列表更新如下:

Company        Mycompany
Income         2000             #revenue is missing
employee       3000          
Facilities     4000
Stock          5000

在上面的列表中,由于公司没有收入,因此从列表中删除了收入,在第二个示例中:

Company        Mycompany
Revenue        1000
Income         2000                
Facilities     4000              #Employee is missing
Stock          5000

在上面的示例中,缺少 2 个员工。如何创建将缺失值替换为 0 的输出列表,例如 1 revenue is missing ,因此我必须将输出列表替换为 ['Revenue,'0 '] 在它的位置,为了更好地理解,请在下面找到

为示例 1 创建的输出列表:收入替换为 0

Company Mycompany| **Revenue 0**| Income 2000| employee 3000| Facilities 4000| Stock 5000

示例 2 的输出列表:将员工替换为 0

Company Mycompany| Revenue 1000| Income 2000| **employee 0**| Facilities 4000| Stock 5000

如何在不改变列表结构的情况下将缺失列表项上的输出列表替换为 0 来实现输出列表。到目前为止我的代码:

       for line in Business:
        if 'Company' not in line:
            Business.insert( 0, 'company')
            Business.insert( 1, '0')
        if 'Revenue' not in line:
            #got stuck here
        if 'Income' not in line:
            #got stuck here
        if 'Employee' not in line:
            #got stuck here
        if 'Facilities' not in line:
            #got stuck here
        if 'Stock' not in line:
            #got stuck here

提前非常感谢

【问题讨论】:

  • 字典似乎是更适合您的任务的数据结构。
  • 如果您可以更改为字典,这会容易得多。

标签: python list dynamic-list


【解决方案1】:

如果您将输入作为列表获取,那么您可以将列表转换为这样的字典,那么您将有更好的数据处理方法,但作为字典获取将是更好的选择

Business = ['Company name','Mycompany','Revenue',1000,'Income',2000,'employee',3000,'Facilities',4000,'Stock',5000]

BusinessDict = {Business[i]:Business[i+1] for i in range(0,len(Business)-1,2)}
print(BusinessDict)

【讨论】:

    【解决方案2】:

    正如 cmets 中所说,dict 是解决问题的更好数据结构。如果你真的需要这个列表,你可以使用这样的临时字典:

    example = ['Company name','Mycompany','Income','2000','employee','3000','Facilities','4000','Stock','5000']
    template = ['Company name', 'Revenue', 'Income', 'employee', 'Facilities', 'Stock']
    
    # build a temporary dict
    exDict = dict(zip(example[::2], example[1::2]))
    
    # work on it
    result = []
    for i in template:
        result.append(i)
        if i in exDict.keys():
            result.append(exDict[i])
        else:
            result.append(0)
    

    更高效一点(但对于初学者来说更难理解)是像这样创建临时字典:

    i = iter(example)
    example_dict = dict(zip(i, i))
    

    这是因为 zip 使用惰性求值。

    【讨论】:

      【解决方案3】:

      你可以像这样使用字典:

      d={'Company':0,'Revenue':0,'Income':0,'employee':0,'Facilities':0,'Stock':0}
      given=[['Company','Mycompany'],['Income',2000],['employee',3000],['Facilities',4000],['Stock',5000]]
      for i in given:
          d[i[0]]=i[1]
      ans=[]
      for key,value in d.items():
          ans.append([key,value])

      【讨论】:

      • 给定的数据结构,不是 OP 所拥有的。 ([[key1, value1], [key2, value2],...]) 而不是 ([key1, value1, key2, value2,...])
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      相关资源
      最近更新 更多