Python Removing a list element that repeats itself -- Solved

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Element List
AI Thread Summary
The discussion centers around the need to remove repeated sequences from an array, specifically the array A=[0,0,1,1,2,5,6,3,7,7,0,0,1,1,2,5,6,3,7,7]. The user initially sought a method to achieve this without using the set() function and expressed difficulty in coding a solution. After some back and forth, they indicated they had resolved the issue but were encouraged to share their solution for the benefit of others. The provided solution involves a function that checks for repeating patterns in the array and creates sublists based on the identified pattern length. The code includes a function to generate sublists and checks if all sublists match the identified pattern, ultimately returning the pattern when found.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have an array that looks like

A=[0,0,1,1,2,5,6,3,7,7,0,0,1,1,2,5,6,3,7,7]

since the "0,0,1,1,2,5,6,3,7,7" part reapeats itself I don't need the second part so for a given array it should give me

A=[0,0,1,1,2,5,6,3,7,7]

I can't use the set() function and I don't know what else I can use in this case.Is there a function which can do this operation ?

I tried to write a code but I couldnt
 
Technology news on Phys.org
Your problem statement seems ill defined. How long must the sequence be before you remove it? In your example, "0" is repeated 4 times.
 
  • Like
Likes FactChecker and jedishrfu
oh umm I solved it..but thanks
 
  • Like
Likes jedishrfu
@Arman777 Can you share your solution so others can benefit?
 
  • Like
Likes jedishrfu
jim mcnamara said:
@Arman777 Can you share your solution so others can benefit?
Python:
def check_repeat(l):
    for i in range(1,len(l)//2+1):
        pattern=l[:i]
        sub_lists=create_sublists(l, i, True)
        if all([x==pattern for x in sub_lists]):
            print("Found pattern {} with length {}".format(pattern, i))
            return patterndef create_sublists(l, n, only_full=False):
    sub_lists=[]
    for j in range(n, len(l),n):
        if only_full and len(l[j:j+n])<n:
            continue
        sub_lists.append(l[j:j+n])
    return sub_lists

but I didnt write it myself. Someone helped me
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
955
Replies
4
Views
2K
Replies
43
Views
4K
Replies
29
Views
3K
Replies
9
Views
1K
Replies
3
Views
1K
Replies
4
Views
4K
Back
Top