比较和替换列表中的项目

时间:2017-08-01 17:23:14

标签: python list

如何比较两个列表中的对象,然后如果两个列表中有两个相同的对象,请将其替换为某些对象?例如:

list1 = [a,b,c,d]
list2 = ['h','j','a','d']

我想说,如果list1中的某些对象与list2中的某些对象相同,请用其他对象替换这些对象(例如,'hello'

2 个答案:

答案 0 :(得分:1)

Dim num As Integer, counter As Integer, pCount As Integer
counter = 0
Dim rngPN As Range
Dim pName As String

'Header
Sheets("Input Raw Data").Select
Range("A1").Copy
Sheets("Point Names").Select
Range("A1").Select
ActiveSheet.Paste

'Point Names
pName = "Point_" & num & ":"
Set rngPN = Sheets("Point Names").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
pCount = Sheets("Input Raw Data").Range("A2").End(xlDown).row
For counter = 2 To pCount
    If Range("A" & counter).Value = pName Then
        Range("A" & counter & ":D" & counter).Copy (rngPN)
        Set rngPN = rngPN.Offest(1, 0)
    End If
Next counter

答案 1 :(得分:1)