如何更新 python 中的动态列表?
How to update a dynamic list in python?
我有一个列表,其中包含 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
动态列表已更新 ***
for every iteration for the list and some of the items in the list is
missing
***。例如执行 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']在它的位置,为了更好的理解请看下面
output list created for example 1: Revenue replaced with 0
Company Mycompany| **Revenue 0**| Income 2000| employee 3000| Facilities 4000| Stock 5000
Output list for example 2: employee is replaced with 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
非常感谢
如果您以列表的形式获取输入,那么您可以像这样将列表转换为字典,这样您将拥有更好的数据处理方法,但作为字典获取将是更好的选择
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)
如评论中所述,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
使用惰性求值。
您可以像这样使用字典:
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])
我有一个列表,其中包含 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
动态列表已更新 ***
for every iteration for the list and some of the items in the list is missing
***。例如执行 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']在它的位置,为了更好的理解请看下面
output list created for example 1: Revenue replaced with 0
Company Mycompany| **Revenue 0**| Income 2000| employee 3000| Facilities 4000| Stock 5000
Output list for example 2: employee is replaced with 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
非常感谢
如果您以列表的形式获取输入,那么您可以像这样将列表转换为字典,这样您将拥有更好的数据处理方法,但作为字典获取将是更好的选择
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)
如评论中所述,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
使用惰性求值。
您可以像这样使用字典:
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])