简体   繁体   English

在python中检查两个字符是否相等

[英]Checking if two characters are equal to each other in python

I am trying to write a program to find a missing letter in an array of letters of alphabetical order. 我正在尝试编写一个程序,以在按字母顺序排列的字母数组中查找丢失的字母。 ex. 恩。 [a,b,c,d,f] missing => 'e'. [a,b,c,d,f]丢失=>'e'。

Right now I have this: 现在我有这个:

def find_missing_letter(chars):

    # Creates variables of complete alphabet.

    alphabetLower = list(string.ascii_lowercase)
    enumeratedLower = []
    alphabetUpper = list(string.ascii_uppercase)
    enumeratedUpper = []

    # Checks if the function has to enumerate the upper- or lowercase alphabet.

    if(chars[0].islower()):
        for c, value in enumerate(alphabetLower, 1):
            enumeratedLower.append([c, value])
    else:
        for c, value in enumerate(alphabetUpper, 1):
            enumeratedUpper.append([c, value])

    # Checks at what letter the characters begin. 
    # After that it checks if the following letters are equal to eachother.

    if(chars[0].isupper()):
        for x in range(1, 26):
            print enumeratedUpper[x][1]
            print 'char:' + chars[0]
            if(chars[0] == enumeratedUpper[x][1]):
                for i in range(enumeratedUpper[x][0], len(chars)):
                    if(chars[i] != alphabetUpper[i]):
                        return alphabetUpper[i]
    else:
        for x in range(1, 26):
            print enumeratedLower[x][1]
            print 'char:' + chars[0]
            if(chars[0] == enumeratedLower[x][1]):
                for i in range(enumeratedLower[x][0], len(chars)):
                    if(chars[i] != alphabetLower[i]):
                        return alphabetLower[i]

However, the if statements 但是,if语句

if(chars[0] == enumeratedUpper[x][1]):

and

if(chars[0] == enumeratedLower[x][1]):

are not working for some reason. 由于某种原因无法正常工作。 The reason for this statement is because a given array of characters (chars), can start at a random letter (doesn't have to start at 'a' or 'A'). 该语句的原因是因为给定的字符(字符)数组可以以随机字母开头(不必以'a'或'A'开头)。 I put the print statement there to see what was wrong and the output is this: 我把打印语句放在那儿,看看有什么问题,输出是这样的:

b
char:o
c
char:o
d
char:o
e
char:o
f
char:o
g
char:o
h
char:o
i
char:o
j
char:o
k
char:o
l
char:o
m
char:o
n
char:o
o
char:o
p
char:o
q
char:o
r
char:o
s
char:o
t
char:o
u
char:o
v
char:o
w
char:o
x
char:o
y
char:o
z
char:o

You are overcomplicating things a bit. 您使事情变得有些复杂。 There is no need to handle upper and lowercase letters separately. 无需分别处理大写和小写字母。 Also, you don't have to use enumerate just use index method to find where characters begin. 另外,您不必使用enumerate就可以使用index方法来查找字符的开始位置。

import string 

def find_missing_letter(chars):   
    # Creates variables of complete alphabet.
    alphabet = string.ascii_lowercase if chars[0].islower() else string.ascii_uppercase
    # Checks at what letter the characters begin
    start = alphabet.index(chars[0])
    # After that it checks if the following letters are equal to each other.
    for x, y in zip(chars, aplhabet[start:]):
        if x != y:
            return y

Sample output: 样本输出:

>>> find_missing_letter('abcdf')
'e'

Your approach seems to be too complicated. 您的方法似乎太复杂了。 Just convert the list of chars to list of nums using map and ord , then enumerate the resulting list to find the first missing number, convert it back to char using chr 只需使用mapord将chars列表转换为nums列表,然后枚举结果列表以查找第一个缺少的数字,然后使用chr将其转换回char

>>> l = ['a', 'b', 'c', 'd', 'f']
chr(next(i for i,j in enumerate(map(ord, l), ord(l[0])) if i!=j))
'e'

You may search the letters that are not in chars, using a generator expression, and then sort them if you need them in alphabetic order 您可以使用生成器表达式搜索不在char中的字母,如果需要,请按字母顺序对其进行排序

>>> chars = ['a', 'b', 'd', 'f']
>>> alphabet = string.ascii_lowercase if chars[0].islower() else string.ascii_uppercase
>>> sorted(letter for letter in alphabet if letter not in chars and letter > chars[0] and letter < chars[-1])

['c', 'e']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM