简体   繁体   中英

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

  1. A string at N length.
  2. An integer k that satisfies the condition: N % k = 0

For instance:

"AABCAAADA"

3

In this case I need to divide string into (9/3) 3 subparts and then I need to remove the duplicates .

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". 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:

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.

This line list(map(set, new)) just removes duplicates from each list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM