我需要创建一个列表,其中包含一个组件以及它来自哪个板。我有一个列表,其中包含每个板上的所有组件,还有一个字符串列表,其中包含该板上的所有组件。


board = ['C500 C501 C502 L101 ','R100 R101 C500']

comps = ['C500','C501','C502','L101','R100','R101','C500']

combinedlist=[]
for i in range(0,(len(comps))):
    found = False
    for j in range(len(board)):
        if str(comps[i]) in str(board[j]):
            found = True
            combinedlist.append(comps[i] + ',' + ('Board'+str(int(j+1))))
            break
    if not found:
        combinedlist.append(comps[i] + ', ' + 'NOT FOUND' )


我希望输出是

C500 , Board1
C501 , Board1
C502 , Board1
L101 , Board1
R100 , Board2
R101 , Board2
C500 , Board2


但是,当C500再次出现时,它将再次说Board1

C500 , Board1
C501 , Board1
C502 , Board1
L101 , Board1
R100 , Board2
R101 , Board2
C500 , Board1 # here

最佳答案

这应该有助于:

board = ['C500 C501 C502 L101 ','R100 R101 C500']
for i, c in enumerate(board) :
    for part in c.strip().split() :
        print part, ', Board%d' % (i+1)


输出:

C500 , Board1
C501 , Board1
C502 , Board1
L101 , Board1
R100 , Board2
R101 , Board2
C500 , Board2

08-24 16:42