2017-04-04 42 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