2013-03-24 23 views
-3

我有隨機的單詞文件,其中一些單詞是palindromes,有些則不是。其中一些迴文是3個或更多字母。我如何數它們?我想知道如何讓條件變得更好。我以爲我可以只是長度,但我一直得到0作爲我的答案,我知道是不正確的,因爲我有.txt文件。 我在哪裏搞亂?Python 3.2 - 在至少有3個字母的單詞列表中將單詞轉換爲小寫字母和數字

number_of_words = [] 

with open('words.txt') as wordFile: 
    for word in wordFile: 
     word = word.strip() 
     for letter in word: 
      letter_lower = letter.lower() 

def count_pali(wordFile): 
    count_pali = 0 
    for word in wordFile: 
     word = word.strip() 
     if word == word[::-1]: 
      count_pali += 1 
    return count_pali 

print(count_pali) 

count = 0 
for number in number_of_words: 
    if number >= 3: 
     count += 1 

print("Number of palindromes in the list of words that have at least 3 letters: {}".format(count)) 

回答

0

你的代碼看起來好權,直到循環:

for number in number_of_words: 
    if number >= 3: 
     count += 1 

這裏有一個在邏輯存在問題。如果您考慮number_of_words的數據結構,以及您實際上要求python與'number> = 3'條件進行比較的結果,那麼我認爲您會很好地理解它。

---修訂的樣子:

# Getting the words into a list 
# word_file = [word1, word2, word3, ..., wordn] 
word_file = open('words.txt').readlines() 

# set up counters 
count_pali, high_score = 0, 0 
# iterate through each word and test it 

for word in word_file: 

    # strip newline character 
    word = word.strip() 

    # if word is palindrome 
    if word == word[::-1]: 
     count_pali += 1 

     # if word is palindrome AND word is longer than 3 letters 
     if len(word) > 3: 
      high_score += 1 

print('Number of palindromes in the list of words that have at least 3 letter: {}'.format(high_score)) 

注: count_pali:統計是迴文 high_score字的總數:計算長度超過3個字母 LEN迴文總數(詞):如果單詞是迴文,將測試單詞的長度

祝你好運!

+0

嗯......我將它與以前定義/發現的迴文相比較。 但是,我怎樣稱呼他們,讓我可以在這個循環中使用它們,並使它們等於我的長度條件?我知道我的迴文長度必須與數字長度相匹配,但我不確定如何寫。 – user2172079 2013-03-24 08:40:44

+0

好吧,不用擔心。如果我正確理解您的代碼: count_pali()函數將計算迴文總數,通過每次找到迴文時遞增。 ...並且您還要計算這些迴文有多少個字母長度超過3個字母。 因此,你可以在迴文測試後直接嘗試if語句嗎?在僞代碼中,它看起來有點像這樣: #你在這裏的代碼... 如果字是迴文: count_pali + 1 – 2013-03-24 09:01:18

+0

增加更多到最後的評論: 「#你前面的代碼在這裏... 如果字是迴文: count_pali + 1 如果字是不是3個字母長: long_word + 1' 我們接近嗎? – 2013-03-24 09:07:13

0

您通過number_of_words爲了計算count循環,但number_of_words被初始化爲空列表,之後從未改變,因此環

for number in number_of_words: 
    if number >= 3: 
     count += 1 

將執行恰好爲0次

+0

這很有道理。出於某種原因,我認爲列出[]的價值意味着它可以是任何數字。有沒有辦法做到這一點?或者我應該循環什麼?它不能是count_pali因爲這是一個函數和wordFile似乎並沒有工作,即使我再次打開它,像這樣: '與打開('words.txt')爲wordFile: count = 0 在wordFile中: 如果number> = 3: count + = 1' – user2172079 2013-03-24 13:32:25

+0

按步驟執行 - 首先嚐試創建所有單詞的列表,然後創建一個函數來測試單詞是否是迴文,檢查它是否有超過3個字符等。測試這些功能。 – xuanji 2013-03-24 13:40:02

+0

謝謝!但是,你如何創建一個所有單詞的列表?我指的是特別是我在開始時打開的.txt文件中的文字。我做了另一個if循環來測試這兩個條件,只返回它 'def count_pali(wordFile): count_pali = 0 for wordFile: word = word.strip() if word == word [:: -1]: 如果字> = 3: count_pali + = 1張 返回count_pali 打印( 「3帕爾斯貨號讓:{}」。格式(count_pali))' 對不起,但我真的很困惑這個! – user2172079 2013-03-24 13:54:02

0

這並未不直接回答你的問題,但它可能有助於理解我們遇到的一些問題。大多數情況下,您可以看到如何添加到列表中,並希望看到獲取字符串,列表和整數(您實際上無法做到!)的長度之間的差異。

嘗試運行下面的代碼,並檢查所發生的事情:

def step_forward(): 
    raw_input('(Press ENTER to continue...)') 
    print('\n.\n.\n.') 

def experiment(): 
    """ Run a whole lot experiments to explore the idea of lists and 
variables""" 

    # create an empty list, test length 
    word_list = [] 
    print('the length of word_list is: {}'.format(len(word_list))) 
    # expect output to be zero 

    step_forward() 

    # add some words to the list 
    print('\nAdding some words...') 
    word_list.append('Hello') 
    word_list.append('Experimentation') 
    word_list.append('Interesting') 
    word_list.append('ending') 

    # test length of word_list again 
    print('\ttesting length again...') 
    print('\tthe length of word_list is: {}'.format(len(word_list))) 

    step_forward() 

    # print the length of each word in the list 
    print('\nget the length of each word...') 
    for each_word in word_list: 
     print('\t{word} has a length of: {length}'.format(word=each_word, length=len(each_word))) 
     # output: 
     # Hello has a length of: 5 
     # Experimentation has a length of: 15 
     # Interesting has a length of: 11 
     # ending has a length of: 6 

    step_forward() 

    # set up a couple of counters 
    short_word = 0 
    long_word = 0 

    # test the length of the counters: 
    print('\nTrying to get the length of our counter variables...') 
    try: 
     len(short_word) 
     len(long_word) 
    except TypeError: 
     print('\tERROR: You can not get the length of an int') 
    # you see, you can't get the length of an int 
    # but, you can get the length of a word, or string! 

    step_forward() 

    # we will make some new tests, and some assumptions: 
    #  short_word: a word is short, if it has less than 9 letters 
    #  long_word:  a word is long, if it has 9 or more letters 

    # this test will count how many short and long words there are 
    print('\nHow many short and long words are there?...') 
    for each_word in word_list: 
     if len(each_word) < 9: 
      short_word += 1 
     else: 
      long_word += 1 
    print('\tThere are {short} short words and {long} long words.'.format(short=short_word, long=long_word)) 

    step_forward() 

    # BUT... what if we want to know which are the SHORT words and which are the LONG words? 
    short_word = 0 
    long_word = 0 
    for each_word in word_list: 
     if len(each_word) < 9: 
      short_word += 1 
      print('\t{word} is a SHORT word'.format(word=each_word)) 
     else: 
      long_word += 1 
      print('\t{word} is a LONG word'.format(word=each_word)) 

    step_forward() 

    # and lastly, if you need to use the short of long words again, you can 
    # create new sublists 
    print('\nMaking two new lists...') 
    shorts = [] 
    longs = [] 
    short_word = 0 
    long_word = 0 

    for each_word in word_list: 
     if len(each_word) < 9: 
      short_word += 1 
      shorts.append(each_word) 
     else: 
      long_word += 1 
      longs.append(each_word) 

    print('short list: {}'.format(shorts)) 
    print('long list: {}'.format(longs)) 
    # now, the counters short_words and long_words should equal the length of the new lists 
    if short_word == len(shorts) and long_word == len(longs): 
     print('Hurray, its works!') 
    else: 
     print('Oh no!') 

experiment() 

希望,當你通過我們的答案在這裏,並檢查上面的小實驗,你將能夠得到您的代碼做你需要它做的事情:)

相關問題