python字符串循环

时间:2015-07-13 10:56:53

标签: python string

我在python(2.7)上有一个赋值,要求我获取字符串的输出。 在第4个问题中,要求提供早先出现的字母表。这个测试只是句子每个单词的第一个字符。 示例:“这是一个句子”,结果应为“a”,因为它是字母表中的第一个字母。

这是我的代码(包括以前的作业问题)

def GetNumWords ( Sentence ):
    Count = 0
    Length = len( Sentence )
    Index = 0
    while Index < Length:
        Char = Sentence [ Index ]
        if Char != ' ':
            Count += 1
            while Char != ' ' and Index < Length:
                Char = Sentence [ Index ]
                Index += 1
        else:
            Index += 1
    return Count

def GetWordNum ( Sentence, WordNum ):
    Count = 0
    Length = len( Sentence )
    Index = 0
    Word = ''   
    while Index < Length:
        Char = Sentence [ Index ]
        if Char != ' ':
            Count += 1
            while Char != ' ' and Index < Length:
                Char = Sentence [ Index ]
                Index += 1
                if Count == WordNum:
                    Word = Word + Char
        else:
            Index += 1
    if Word == '':
        return ''
    else:
        return Word

def GetFirstLetter ( Sentence, SpecificNum):
    TheWord = GetWordNum ( Sentence, SpecificNum )
    if TheWord == '':
        return ''
    else:
        FirstLetter = TheWord [ 0 ]
        return FirstLetter

def GetEarliestLetter ( Sentence ):
    CurrentMinNum = 1
    CurrentMin = GetFirstLetter ( Sentence, CurrentMinNum )
    LastWord = GetNumWords ( Sentence )
    if CurrentMin == '':
        return ''
    else:
        while CurrentMinNum <=  LastWord:
            FirstLetter = CurrentMin
            if FirstLetter < CurrentMin:
                CurrentMin = FirstLetter
                CurrentMinNum += 1
            else:
                break
        return CurrentMin

这样做会给我第一个单词的第一个字母而不是字母顺序中最早的字母。

我哪里做错了?过去两天我看过这个,我看不出我做错了什么。

2 个答案:

答案 0 :(得分:6)

我认为你可能过于复杂了。

>>> s = "this is a sentence"
>>> min(c for c in s if c.isalpha())
'a'

答案 1 :(得分:2)

如果您不能使用任何str方法并且只能使用while循环,raw_input和len,那么您的输入必须受到限制,这样才能找到每个单词的最低首字母:

def first_alpha():
    s = raw_input()
    mn, curr = s[0], s[0]
    i = 1
    while i < len(s):
        if s[i] == " ":
            if curr < mn:
                mn = curr
            curr = s[i+1]
        i += 1
    return mn if curr > mn else curr

就像我说的那样,它适用于限制输入,这些输入是字母,单词用单个空格隔开。

In [5]: first_alpha()
this is a sentence
Out[5]: 'a'    
In [6]: first_alpha()
lowest char is trailing a
Out[6]: 'a'    
In [7]: first_alpha()
lowest char is upper A
Out[7]: 'A'

如果你没有受到限制,显然min(word[0] for word in s.split())可以采用更简单的方法。

仅捕捉非字母的字母并在末尾捕捉空格:

def first_alpha():
    s = raw_input()
    # ord("{") == 123
    mn, curr = "{", "{"
    i = 0
    # catch line ending in a space
    ln = len(s) if s[-1] != " " else len(s) - 1
    while i < ln:
        if s[i] == " ": 
            if curr <= mn:
                mn = curr
            ch = s[i+1]
            # make sure we have a letter
            if "a" <= ch <= "z" or "A" <= ch <= "Z":
                curr = ch
        i += 1
    return mn if curr > mn else curr

输出:

In [29]: first_alpha()
this is sentence where the lowest is ! but we return a
Out[29]: 'a'
In [30]: first_alpha()
lots of   spaces and    but we return a     
Out[30]: 'a'

唯一的边缘情况是一个你没有字母的字符串,然后最低的是}所以你可以决定在那种情况下应该发生什么