我有一个列表,其中包含 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,'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
非常感谢提前