开发者社区> 问答> 正文

如何在匹配条件后更新/替换嵌套循环中的列表项?

图像被添加,这样你就可以看到我的dataframe df2的样子 我写了一个代码来检查条件,如果匹配,它将更新我的列表中的项目,但它在所有工作,它返回我相同的未更新或相同的前一个列表。这个代码是错的吗? 请建议

emp1=[]
for j in range(8,df.shape[0],10):
    for i in range(2,len(df.columns)):
        b=df.iloc[j][3]
        #values are appended from dataframe to list and values are like['3 : 3','4 : 4',.....]

ess=[]
for i in range(df2.shape[0]):
    a=df2.iloc[i][2]
    ess.append(a) #values taken from file which are(3,4,5,6,7,8,....etc i.e unique id number)
nm=[]
for i in range(df2.shape[0]):
    b=df2.iloc[i][3]
    nm.append(b) #this list contains name of the employees

ap= [i.split(' : ', 1)[0] for i in emp1]  #split it with ' : ' and stored in two another list(if 3 : 3 then it will store left 3)
bp= [i.split(' : ', 1)[1] for i in emp1] #if 3 : 3 the it will store right 3
cp=' : '

#the purpose is to replace right 3 with the name i.e 3 : nameabc and then again join to the list   
for i in range(len(emp1)):
    for j in range(len(ess)):
        #print(i,j)
        if ap[i]==ess[j]:
           bp[i]=nm[j]

for i in range(df.shape[0]):
    ap[i]=ap[i]+cp      # adding ' : ' after left integer    

emp = [i + j for i, j in zip(ap, bp)] # joining both the values

预期的输出: 如果emp1包含3:3 然后在处理后应该显示像3:nameabc 问题来源StackOverflow 地址:/questions/59386144/how-to-update-replace-list-items-inside-a-nested-loop-after-matching-the-conditi

展开
收起
kun坤 2019-12-25 22:07:31 278 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载