[英]I am trying to segment the list and then remove duplicates according to the given input values ? However I am missing something
There are 2 inputs given有 2 个输入
For instance:例如:
"AABCAAADA" “啊啊啊啊啊”
3 3
In this case I need to divide string into (9/3) 3 subparts and then I need to remove the duplicates .在这种情况下,我需要将字符串分成 (9/3) 3 个子部分,然后我需要删除重复项。
explanation -> you can see the demonstration here.解释-> 你可以在这里看到演示。
I wrote a code that does the exact thing, however at the end of my output section I see "None".我写了一个代码来做确切的事情,但是在我的 output 部分的末尾我看到“无”。 I couldn't figure out where did that come from..
我想不通这是从哪里来的。。
Here is my code:这是我的代码:
s = "AABCAAADA"
k = 3
def merge_the_tools(string, k):
ls = segment(string,k)
for l in ls:
print(remove_repetitive(l))
def remove_repetitive(string):
temp = list(dict.fromkeys(string))
return "".join(temp)
def segment(string,k):
ls = []
segment_len = int(len(string) / k)
x = 0
for i in range(segment_len):
ls.append(string[x:x+k])
x += k
return ls
print(merge_the_tools(s,k))
And this is my output:这是我的 output:
AB
CA
AD
None
k = 3
new = [s[((x+1)*3)-3:(x+1)*3] for x in range(k)]
new = list(map(set, new))
print(new)
>>> ({'B', 'A'}, {'C', 'A'}, {'D', 'A'})
This line [((x+1)*3)-3:(x+1)*3]
just gets the next three items and makes a list out of them and adds it to the overall list.这一行
[((x+1)*3)-3:(x+1)*3]
只是获取接下来的三个项目并从中列出一个列表并将其添加到整个列表中。
This line list(map(set, new))
just removes duplicates from each list.此行
list(map(set, new))
只是从每个列表中删除重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.