2017-04-04 32 views
2

編寫一個名爲remove_duplicates的函數,該函數將接受一個名爲string的參數。 This string input will only have characters between a-z。 函數應該刪除所有重複的字符字符串中,並用兩個值返回一條:只有唯一使用Python進行字符串練習3

  • 一個新的字符串,排序字符。
  • 刪除重複項的總數。

例如:

  • remove_duplicates('aaabbbac')應該產生('abc')
  • remove_duplicates('a')應該產生('a', 0)
  • remove_duplicates('thelexash')應該產生('aehlstx', 2)

我的代碼:

def remove_duplicates(string): 

        for string in "abcdefghijklmnopqrstuvwxyz": 

            k = set(string) 

            x = len(string) - len(set(string)) 

            return k, x 

    print(remove_duplicates("aaabbbccc")) 

預期輸出:

我期待它打印({a, b, c}, 6)而是打印({a}, 0)

上面的代碼有什麼問題?爲什麼它沒有產生我期待的?

回答

1

你如果不遍歷字符串中的每個字符,將會得到預期的結果。

我已經評論了你的代碼,所以你可以看到你的腳本和我的區別。


非工作註釋代碼:

def remove_duplicates(string): 

    #loop through each char in "abcdefghijklmnopqrstuvwxyz" and call it "string" 
    for string in "abcdefghijklmnopqrstuvwxyz": 

     #create variable k that holds a set of 1 char because of the loop 
     k = set(string) 

     # create a variable x that holds the difference between 1 and 1 = 0 
     x = len(string) - len(set(string)) 

     #return these values in each iteration 
     return k, x 

print(remove_duplicates("aaabbbccc")) 

輸出:

({'a'}, 0) 

工作代碼:

def remove_duplicates(string): 

    #create variable k that holds a set of each unique char present in string 
    k = set(string) 

    # create a variable x that holds the difference between 1 and 1 = 0 
    x = len(string) - len(set(string)) 

    #return these values 
    return k, x 

print(remove_duplicates("aaabbbccc")) 

輸出:

({'b', 'c', 'a'}, 6) 

P.S:,如果你想你的結果是爲了,你可以改變return k, xreturn sorted(k), x,但隨後的輸出將是一個列表。

(['a', 'b', 'c'], 6) 

編輯:如果你只想如果某些條件得到滿足你的代碼運行 - 例如,僅運行如果字符串沒有任何號碼 - 你可以添加一個if/else語句:

例如代碼:

def remove_duplicates(s): 

    if not s.isdigit(): 
     k = set(s) 
     x = len(s) - len(set(s)) 
     return sorted(k), x 
    else: 
     msg = "This function only works with strings that doesn't contain any digits.." 
     return msg 


print(remove_duplicates("aaabbbccc")) 
print(remove_duplicates("123123122")) 

輸出:

(['a', 'b', 'c'], 6) 
This function only works with strings that doesn't contain any digits.. 
+0

好的,我如何將字符串限制爲僅用於運行代碼的字母表。就像你的代碼也會運行「22233377」一樣。這就是我想要做的,「在TTT中爲xx」運行 – wapadunk

+0

您必須添加一個「if/else」子句。我已經更新了我的答案,請看一看。 –

+0

幫助!我一直在試圖將這個輸出(['a','b','c'],6)轉換爲(「abc」,6)。 – wapadunk

0

您正在從函數返回的第一個實例中找到一個字符。所以它返回第一個「a」。

試試這個:

def remove_duplicates(string): 
    temp = set(string) 
    return temp,len(string) - len(temp) 


print(remove_duplicates("aaabbbccc")) 

輸出:

({'c', 'b', 'a'}, 6) 

如果你想刪除的一切期望字母(如你在評論中提到的)試試這個:

def remove_duplicates(string): 
    a= set() 
    for i in string: 
     if i.isalpha() and i not in a: 
      a.add(i) 
    return a,len(string) - len(a) 
+0

好了,我怎麼限制字符串是隻爲代碼運行的字母。就像你的代碼也會運行「22233377」一樣。這就是我想要做的,通過「在TTT xx」運行 – wapadunk

+0

我已經更新了我的答案。你確定你想要一組作爲輸出,而不是一個字符串? – Himaprasoon

+0

你以前的回答是可以的。我只需要額外的條款,以確保它不接受字符串「2223333」而只接受「abc .... z」 – wapadunk

0

在您的代碼中,函數將在迭代第一個字符後返回。 由於string引用輸入字符串中的第一個字符。我認爲你正試圖迭代遍歷string可變字符。 爲此,您可以使用collections.Counter,它可以更高效地執行相同的計算。

但是,我們可以使用另一種解決方案,它不涉及計算給定字符串中每個字符的計數。

def remove_duplicates(s): 
    unique_characters = set(s) # extract the unique characters in the given string 
    new_sorted_string = ''.join(sorted(unique_characters)) # create the sorted string with unique characters 
    number_of_duplicates = len(s) - len(unique_characters) # compute the number of duplicates in the original string 
    return new_sorted_string, number_of_duplicates 
+0

好的,我如何限制字符串僅爲代碼的字母表跑步。就像你的代碼也會運行「22233377」一樣。這就是我想要做的,通過「在TTT中的xx」運行 – wapadunk

+0

@wapadunk我不明白你想用字符串和字母表來引用什麼?一個字符串是一個Python'類型'。 –

+0

您之前的回答是可以的。我只是需要額外的條款,以確保它不接受字符串「2223333」而只接受「abc .... z」 – wapadunk

-1

DEF remove_duplicates(S): unique_characters =(多個)集合#提取在給定的 串的唯一的字符 new_sorted_string = ''。加入(排序(unique_characters))#創建排序字符串具有獨特人物 number_of_duplicates = LEN(S) - LEN(unique_characters)#計算重複的原始字符串 回報new_sorted_string數量,number_of_duplicates

+0

請相應地格式化代碼 – Luuklag