简体   繁体   English

我正在尝试对列表进行分段,然后根据给定的输入值删除重复项? 但是我错过了一些东西

[英]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 个输入

  1. A string at N length.长度为N的字符串。
  2. An integer k that satisfies the condition: N % k = 0一个满足条件的 integer kN % k = 0

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.

相关问题 这是一个错误还是我错过了什么? - Is this a bug or am I missing something? 我正在尝试验证用户输入是数字还是在给定范围内,试图删除以前单元格的历史记录 - i am trying to validiate a user input whether its is a number or in a given range,trying to remove the history of previous cells 我错过了什么吗? TensorFlow 中的简单分类器输入 function 出错 - Am I missing something? Error with simple classifier input function in TensorFlow 我的正则表达式错过了我要匹配的项目之一。 有什么我想念的吗? - My regex formula misses one of the items I am trying to match. Is there something I am missing? 我是否缺少缩进或其他问题? - Am I missing indentation or is something else wrong with this? 有没有其他方法可以做到这一点,或者我错过了什么 - is there a another way to do this or i am missing something 多处理/线程-高估了,或者我缺少什么? - Multiprocessing/Threading - Overrated, or am I missing something? 我是否在 django 上的模板上遗漏了什么 - Am i missing something on templates on django 奇怪的Python行为 - 或者我错过了什么 - Weird Python behaviour - or am I missing something 我是否缺少对给定 state 中的操作的检查? - Am I missing a check for the actions in the given state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM