Python列表比较实现和错误

发布于 2025-01-23 20:26:25 字数 1029 浏览 0 评论 0原文

该程序应该在[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文