listutils.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#!/usr/bin/env python
#
# Some of these are adapted from other djangosnippets.

def has_consecutive_duplicates_of(seq, item):
  """Returns whether duplicate consecutive elements are in the sequence.

  Args:
    seq: The sequence.
    item: Which item to test for duplicates.

  >>> has_consecutive_duplicates_of([], 5)
  False
  >>> has_consecutive_duplicates_of([1, 2, 2, 3], 5)
  False
  >>> has_consecutive_duplicates_of([1, 2, 2, 3], 2)
  True
  >>> has_consecutive_duplicates_of([2, 0, 2], 2)
  False
  """
  if not seq:
    return False
  for i in range(len(seq) - 1): 
    if seq[i] == item and seq[i + 1] == item:
      return True
  return False


def has_duplicates_of(seq, item):
  """Returns whether seq contains duplicates of the given item.

  Args:
    seq: The sequence.
    item: Which item to test for duplicates.

  >>> has_duplicates_of([], 5)
  False
  >>> has_duplicates_of([1, 2, 3], 5)
  False
  >>> has_duplicates_of([1, 2, 3], 2)
  False
  >>> has_duplicates_of([1, 2, 2, 3], 2)
  True
  """
  return len(list(x for x in seq if x == item)) > 1 


def count_items(seq, item):
  """Returns the number of times the given item appears in the sequence.

  Args:
    seq: The sequence.
    item: Which item to return the count of.

  >>> count_items([], 5)
  0
  >>> count_items([1, 2, 3], 5)
  0
  >>> count_items([1, 2, 3], 2)
  1
  >>> count_items([1, 2, 3, 2], 2)
  2
  """
  return len(list(x for x in seq if x == item))


def slice(seq, count=2):
  """Splits a sequence into a list of count sublists.

  If count > len(seq) the returned list will still contain count sublists, but
  len(seq) - count of the sublists will be empty.

  From http://www.garyrobinson.net/2008/04/splitting-a-pyt.html

  Args:
    seq: The sequence.
    count: The number of sublists.

  >>> slice([], 2)
  [[], []]
  >>> slice([1, 2], 1)
  [[1, 2]]
  >>> slice([1, 2], 2)
  [[1], [2]]
  >>> slice(range(6), 2)
  [[0, 1, 2], [3, 4, 5]]
  >>> slice(range(5), 2)
  [[0, 1, 2], [3, 4]]
  >>> slice([1, 2], 3)
  [[1], [2], []]
  """
  start = 0
  output = []
  for i in xrange(count):
    stop = start + len(seq[i::count])
    output.append(seq[start:stop])
    start = stop
  return output


if __name__ == "__main__":
  import doctest
  doctest.testmod()

More like this

  1. Non model specific CSV export of database content by sethtrain 6 years, 2 months ago
  2. Pattern to integer list function by marinho 6 years, 4 months ago
  3. Integer list to pattern function by marinho 6 years, 4 months ago
  4. More information about users and groups in user admin by ramusus 4 years, 9 months ago
  5. LinkSleeve comment moderation by zgoda 6 years ago

Comments

akaihola (on February 6, 2010):

A shorthand for

list(x for x in ...)

in Python is:

[x for x in ...]

If your sequences are really long, there's a trick to avoid using up lots of memory when counting items. Instead of

len(list(x for x in ...))

it's probably better to use

sum(1 for x in ...)

#

(Forgotten your password?)