Login

listutils.py

Author:
statico
Posted:
January 18, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

Various list utilities.

  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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months 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 ...)

#

Please login first before commenting.