使用Python 3进行字符串练习
编写一个名为使用Python 3进行字符串练习,python,python-3.x,Python,Python 3.x,编写一个名为remove_duplicates的函数,该函数将接受一个名为string的参数。 此字符串输入将仅包含介于a-z之间的字符。 函数应删除字符串中的所有重复字符,并返回一个包含两个值的元组: 仅包含唯一排序字符的新字符串 删除的重复项的总数 例如: 删除重复项('aaabbbac')应产生('abc') 删除重复项('a')应产生('a',0) 删除重复项('thelexash')应产生('aehlstx',2) 我的代码: def remove_duplicates
remove_duplicates
的函数,该函数将接受一个名为string
的参数。
此字符串
输入将仅包含介于a-z之间的字符
。
函数应删除字符串中的所有重复字符,并返回一个包含两个值的元组:
- 仅包含唯一排序字符的新字符串
- 删除的重复项的总数
应产生删除重复项('aaabbbac')
('abc')
应产生删除重复项('a')
('a',0)
应产生删除重复项('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"))
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)
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..
预期产出:
我希望它打印({a,b,c},6)
,而不是打印({a},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)
如果不遍历字符串中的每个字符,将得到预期的结果 我已经对您的代码进行了注释,以便您可以看到您的脚本和我的脚本之间的差异
非工作注释代码:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
输出:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
工作代码:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
输出:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
p.s.:如果您希望结果井然有序,可以将返回k,x
更改为返回排序(k),x
,但随后输出将是一个列表
(['a', 'b', 'c'], 6)
编辑:如果希望代码仅在满足特定条件时运行(例如,仅在字符串没有任何数字时运行),则可以添加if/else子句: 示例代码:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
输出:
def remove_duplicates(string):
for string in "abcdefghijklmnopqrstuvwxyz":
k = set(string)
x = len(string) - len(set(string))
return k, x
print(remove_duplicates("aaabbbccc"))
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)
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..
在代码中,函数将在迭代第一个字符后返回。 As
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
好的,如何将字符串限制为仅用于运行代码的字母表。像这样,您的代码也将为“22233377”运行。这就是我试图做的,通过“TTT中的xx”runI更新了我的答案。您确定要将集合作为输出而不是字符串吗?您先前的回答是可以的。我只需要增加一个子句,以确保它不接受“222333”作为字符串,而只接受“abc…z”检查我答案的第二部分。这对您不起作用吗?好的,我如何将字符串限制为仅用于运行代码的字母表。像这样,您的代码也将为“22233377”运行。这就是我想做的,通过“TTT中的xx”run@wapadunk我无法理解你试图用字符串和字母表指的是什么?字符串是Python类型
。前面的答案是可以的。我只需要额外的子句来确保它不接受“222333”作为字符串,而只接受“abc…z”好的,我如何将字符串限制为仅用于运行代码的字母表。像这样,您的代码也将为“22233377”运行。这就是我试图做的,通过“for xx in TTT”运行,您必须添加一个if/else
子句。我已经更新了我的答案,看一看。救命!我一直在尝试将此(['a','b','c'],6)的输出转换为(“abc”,6)。将return sorted(k),x
更改为return'。join(sorted(k)),x
工作正常。但请向我解释一下这种方法。您已将已排序集合并为字符串。所以结果变成一个字符串对吗??????