2017-07-22 88 views
-1

我已經給出了一個賦值來查找字符串中使用Python的最長長度的大寫字母。使用Python查找字符串中大寫字母的最長長度

假如我有一個新的電子郵件,我怎麼能計算出以下特點?:

  • capital_run_length_average:連續
  • capital_run_length_longest:連續
  • capital_run_length_total:連續

這裏是一個鏈接到我正在使用的Spambase數據集: https://archive.ics.uci.edu/ml/datasets/Spambase

+1

歡迎SO!你試過什麼了?爲了幫助我們,請添加[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – ti7

+0

如何使用Python在字符串中查找大寫字母的最長長度。 – user3087269

+0

我試圖通過編輯來清理您的問題,但它確實不完整,需要您提供更多信息。 – ti7

回答

0

在這個以及許多類似的案例中,您可能最適合使用正則表達式。

import re # library for regular expression wrangling 

def get_max_uppercase_run_from_string(s): 
    # construct a list of all the uppercase segments in your string 
    list_of_uppercase_runs = re.findall(r"[A-Z]+", s) 

    # find out what the longest string is in your list 
    longest_string = max(list_of_uppercase_segments, key=len) 

    # return the length of this string to the user 
    return len(longest_string) 

這裏的正則表達式的一個活生生的例子,你可以玩: https://regex101.com/r/0LUYEo/1

+0

偉大的解決方案,謝謝。它工作 – user3087269

+0

輝煌 - 請使用這個答案左邊的小複選標記您的問題已解決。 – ti7

+0

能詳細說明如何從字符串中提取以下特徵 – user3087269