Python列表比较实现和错误
该程序应该在[newAssetCode]列中取上行,并将它们与列['allassetCodes']中的行进行比较,然后在['allassetCodes']中已经存在的[newAssetCode]中的资产中找到资产。然后,该程序将计算['AllAssetCodes']中资产存在多少次。例如,如果['allassetCodes']包含'xyz','xyz1','xyz2'它将计算出来,然后在该计数中添加一个数字并在新列表中附加该数字,从而创建'xyz3'。我的问题是我是否正确实施了?而且我确实有错误。 在此处输入图像描述
''''
from itertools import count
import pandas as pd
import openpyxl
from csv import reader
data = pd.read_excel('Codes.xlsx')
new_assets = data['newAssetCode'].tolist()
allAssetCodes = data['allAssetCodes'].tolist()
print(len(new_assets))
print(len(allAssetCodes))
assets_real = list()
for asset in new_assets:
if not isinstance(asset,str):
continue
if asset not in allAssetCodes:
assets_real.append(asset)
continue
existing = [exasset for exasset in allAssetCodes if exasset.startswith(asset)]
assets_real.append(f'{asset}{len(existing)+1}')
print(assets_real)
print(existing)
'''
The program is supposed to take the rows in column [newAssetCode] and compare them to the rows in column ['allAssetCodes'] to then find the assets in [newAssetCode] that already exist in ['allAssetCodes']. The program will then count how many times the assets exist in ['allAssetCodes']. for example if If ['allAssetCodes'] contains 'XYZ','XYZ1', 'XYZ2' it will count that and then add a digit to that count and append that number in a new list creating 'XYZ3'. my question is did I implement this correctly? and Also I do have an error. enter image description here
'''
from itertools import count
import pandas as pd
import openpyxl
from csv import reader
data = pd.read_excel('Codes.xlsx')
new_assets = data['newAssetCode'].tolist()
allAssetCodes = data['allAssetCodes'].tolist()
print(len(new_assets))
print(len(allAssetCodes))
assets_real = list()
for asset in new_assets:
if not isinstance(asset,str):
continue
if asset not in allAssetCodes:
assets_real.append(asset)
continue
existing = [exasset for exasset in allAssetCodes if exasset.startswith(asset)]
assets_real.append(f'{asset}{len(existing)+1}')
print(assets_real)
print(existing)
'''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论