使用Python计算VLOOKUPS的列数

时间:2018-12-21 11:16:52

标签: python excel vlookup

我正在练习Python,我对此非常陌生,并认为我将它应用于在Excel中进行VLOOKUPS时产生的小刺激。我们拥有的数据库非常庞大,长度超过15,000行,并且列中的GG数量超过GG,因此,我想要一种简单的方法来检查用于查找的列号,而不是突出显示滞后或手动计数的数字。 / p>

我尝试了以下代码,在一定程度上可以正常工作。它允许我检查两个不同的3列代码的值(excel max是XFD);但是如果我在AA列和XFD列之间进行比较,则会出现错误,因为开始[2]处没有值,那里没有数组。

在执行for循环之前,如何排除它或检查它是否存在?

    int lastPositionClicked = -1;
     boolean isClicked = false;

@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

    holder.YOUR_VIEW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // store the position which you have just clicked and you will change the background of this clicked view
            lastPositionClicked = position;
            isClicked = true;

            // need to refresh the adapter
            SlabAdapter.this.notifyDataSetChanged();
        }
    });

    // only change the background of last clicked item 
    if (isClicked && position == lastPositionClicked) {
        // change clicked view background color
    } else {
        // otherwise set the default color for all positions

    }
}

对于结果:

def get_character_distance2(start,end):
endList = list(end)
print(endList)

startList = list(start)
print(startList)

firstDistance = 0
secondDistance = 0
thirdDistance = 0

totalEnd = 0
totalStart = 0

for item in endList[0]:
    firstDistance += (ord(item.upper()) - ord(startList[0])) + 1

for item in endList[1]:
    secondDistance += ((ord(item.upper()) - ord(startList[1])) + 1) * 26

for item in endList[2]:
    thirdDistance += ((ord(item.upper()) - ord(startList[2])) + 1) * 676

totalStart += firstDistance + secondDistance + thirdDistance
print(firstDistance)
print(secondDistance)
print(thirdDistance)
print(totalStart)

结果(精细):

get_character_distance2('AAA','ZZZ')

不同的检查:

['Z', 'Z', 'Z']
['A', 'A', 'A']
26
676
17576
18278

不良结果:

get_character_distance2('AA','ZZZ')

我只是想和第一个一样,它添加了for循环的值,条件是实际上有一个要计数的级别。如果没有数组值,则可以假定该数字为0。

1 个答案:

答案 0 :(得分:0)

我提出了以下解决方案,并针对tryexcept提出了Anwarvic的建议。

def get_character_distance2(start,end):
startList = list(start)
print(startList)

endList = list(end)
print(endList)

firstDistance = 0
secondDistance = 0
thirdDistance = 0

totalEnd = 0
totalStart = 0

for item in endList[0]:
    firstDistance += (ord(item.upper()) - ord(startList[0])) + 1

try:
    for item in endList[1]:
        secondDistance += ((ord(item.upper()) - ord(startList[1])) + 1) * 26
except:
    try:
        for item in endList[1]:
            secondDistance += ((ord(item.upper()) - ord('A')) + 1) * 26
    except:
        secondDistance = 0

try:
    for item in endList[2]:
        thirdDistance += ((ord(item.upper()) - ord(startList[2])) + 1) * 676
except:
    try:
        for item in endList[2]:
            thirdDistance += ((ord(item.upper()) - ord('A')) + 1) * 676
    except:
        thirdDistance = 0

totalStart += firstDistance + secondDistance + thirdDistance
print(firstDistance)
print(secondDistance)
print(thirdDistance)
print(totalStart)

非常感谢您!