Python list duplicate element

Asked 2 years ago, Updated 2 years ago, 16 views

Hello, I have a question that I didn't know while doing my homework "T" I'm working on a task to make a board game, and the end of the game is a game that moves according to certain rules. However, I have to make a function that tracks the position of the end of the organ, but I have a headache;

For example, list=[(1,2),(3,4),(5,),(7,8)(9,10),(4,5),(7,8)(9,10),(4,5),(7,8)(9,10),(4,5),(7,8)(9,10)...] Suppose that there is a list that repeats (4,5), (7,8) (9,10) in this way. In this case, I know what elements are repeated, so there is no problem looking for overlapping elements, but what should I do if I say that I don't know exactly the elements of the list and I don't know the repetitive sequence (?)?

python

2022-09-22 19:48

1 Answers

As Jung Younghoon said in the comments, the question is ambiguous. It would have been nice if you could organize the questions better.

If the length of the repeated pattern is 3, we can squeeze it like this. In the task of finding a iteration, you created a coordinate or a specific number based on a specific number.

It's a more complicated problem if the length of the pattern is not 3, but if you can get a random pattern length.

I hope you can apply the code below well.

def unique_list(l):
  x = []
  for a in l:
    if a not in x:
      x.append(a)
  return x

numbers = [1,2,3,4,5,4,5,4,5]
patterns = []

for i, number in enumerate(numbers):
    if i == len(numbers)-2:
        break
    patterns.append(numbers[i:i+3])
unique_patterns=unique_list(patterns)

for pattern in unique_patterns:
    print("{}: {}".format(pattern, patterns.count(pattern)))


2022-09-22 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.