11

I am wanting to check if a list has a specific sequence of elements. I have sorted the list which contains 7 elements, I now want to check of the first 4 are the same as each other and the last 3 are the same as each other.

For what I want to achieve to be True the list would be like this:

list = ['1','1','1','1','2','2','2'] 

I hope this makes what I want to achieve clearer.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Frazer224
  • 167
  • 6
  • `if len(set(lst[:3])) == 1 and len(set(lst[3:])) == 1:` – Hugh Bothwell Dec 06 '15 at 13:22
  • Possible duplicate of http://stackoverflow.com/questions/3787908/python-determine-if-all-items-of-a-list-are-the-same-item – Learner Dec 06 '15 at 14:08
  • Possible duplicate of [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – Vaibhav Mule Dec 25 '15 at 13:31

5 Answers5

8

You can slice a list. Take the first four elements:

>>> L = ['1','1','1','1','2','2','2'] 
>>> L[:4]
['1', '1', '1', '1']

and the last three:

>>> L[-3:]
['2', '2', '2']

A set does not allow duplicates. Therefore:

 >>> set(L[:4])
 {1}

That means if he length of this set is 1, all elements in the sliced list are the same.

Putting this all together:

>>> len(set(L[:4])) == 1 and len(set(L[-3:])) == 1
True

shows you that your condition is met.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
3

This should work, just pass each of your sublists into the function:

def all_same(items):
    return all(x == items[0] for x in items)

The above was from the following post: Python: determine if all items of a list are the same item

Community
  • 1
  • 1
Simon
  • 9,762
  • 15
  • 62
  • 119
2

If you want to check if list contains 3 items of one element, and 4 items of another, you can omit sorting by using collections.Counter:

content = Counter(['1', '2', '2', '1', '1', '2', '1']).most_common()
print(content) # => [('1', 4), ('2', 3)]

if len(content) == 2 and content[0][1] == 4 and content[1][1] == 3 or
   len(content) == 1 and content[0][1] == 7:
    pass # Your list have desired structure
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
1

Based on the extra details on the question, this can solve the problem:

def check_group_equal(inputList):
    ref = inputList[0]
    for e in inputList[1:]:
        if e != ref:
            return False
    return True

list = some_random_list(length=7)

# Check first group
check_group_equal(list[0:3])

# Check second group
check_group_equal(list[4:7])
SebasSBM
  • 860
  • 2
  • 8
  • 32
  • 1
    The last value helps a lot, so thanks a lot! However I don't think the expression will achieve what I want. I have edited the post to try to explain what I want to check better. – Frazer224 Dec 06 '15 at 13:42
  • this will never be true, a 3 lenght list is always diferent from a 2 lenght list – Netwave Dec 06 '15 at 13:43
  • Dear downvoter, please notice that my first answer's version was directed to the first question's version. After seeing extra information in the edit, I **adapted my answer accordingly**. Please reconsider what you have done – SebasSBM Dec 06 '15 at 13:52
1

If you can transform your list into string, re will do:

re.match(r'^(.)\1{3}(.)\2{2}$', ''.join(['1','1','1','1','2','2','2']))
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52