List comprehension += operator - python

How to convert this for loop:
smoke_ray = [18, 14]
total = 0
for i in smoke_ray:
total += i
Into a list comprehension? I tried:
smoke_ray = [18, 14]
total = 0
[total += i for i in smoke_ray]
Is the problem the += operator?
that’s where I get an error
Updated with full code:
days = [
{ "day_name": "wed",
"smoked_at": {
'15:30': 1,
'16:30': 1,
'16:50': 2,
'17:30': 1,
'18:30': 1,
'20:20': 1,
'21:30': 1,
'22:30': 1,
'25:00': 5
}
},
{ "day_name": "thurs",
"smoked_at": {
'08:15': 1,
'08:40': 1,
'09:20': 1,
'10:00': 1,
'11:20': 1,
'11:38': 1,
'12:10': 1,
'13:00': 1,
'14:26': 1,
'15:40': 1,
'17:08': 1,
'18:10': 1,
'19:30': 1,
'20:20': 1,
'22:00': 1,
'23:00': 1,
'25:00': 2
}
}
]
smoke_ray = []
for i in days:
print(i["day_name"])
smokes = i["smoked_at"].values()
smokes_day = sum(smokes)
print(smokes_day)
smoke_ray.append(i)
total = 0
for i in smoke_ray:
total += i
print(total)
When trying to convert the last for loop to a list comprehension (are you telling me it’s not a shorthand way of writing a loop? I heard it was faster)
I get this error:
File "compiler.py", line 47
[total += i for i in smoke_ray]
^
SyntaxError: invalid syntax
When trying to use sum, it just won’t work:
sum(smoke_ray)
wed
14
thurs
18
Traceback (most recent call last):
File "compiler.py", line 47, in
sum(smoke_ray)
TypeError: unsupported operand type(s) for +: 'int' and 'dict'

You don't even need a list comprehension. Just use sum:
total = sum(smoke_ray)

Looking at the code you've now added for context, I think the issue you're having isn't actually in summing the list, it's in constructing the list itself. When I run your original code, with the for loop, that also fails for me.
I think the problem is in the line smoke_ray.append(i): Here, you're appending an entire element of the dictionary (e.g: { "day_name": "...", "smoked_at": { ... } }) to the smoke_ray list. Then, it doesn't make sense to sum over values in that list, since they're dictionaries. If you wanted to add each smokes_day to the list, and then sum over those, you'd do smoke_ray.append(smoke_day) within that loop. Then, you should be able to just use sum as mentioned in other answers to sum over the list.
Edit: This isn't to say that there aren't more improvements that could be done to the code btw, a simple change that would preserve the original structure would be to change your for loop to be something like this:
total = 0
for i in days:
print(i['day_name'])
smokes = i['smoked_at'].values()
smokes_day = sum(smokes)
print(smokes_day)
total += smokes_day
print(total)
That way you can sum the values like you want within one loop, without the need to construct another list/use a list comprehension.

If you want to see the sum value list wise then you can use
import itertools
smoke_ray = [18, 14]
print(list(itertools.accumulate(smoke_ray)))
This will show you the sum of the series by element
Output
[18, 32]

List comprehension is specifically an operation to transform an input list into an output list via two actions:
transformation of each element (also known as “mapping”)
filtering based on some filtering criterion
That’s it.
The operation you want is fundamentally different: you want to accumulate the elements in a list using a given operation. This action is also known as a “reduction” and is available in the Python library via functools.reduce.
So you could write
import functools
import operator
functools.reduce(operator.add, smoke_ray)
… but this is such a common operation that there’s a shorthand for it: as mentioned, you can also just use sum(smoke_ray).

You can do it in multiple ways, since you asking here for list comprehension (although list comprehension is a bad option here, a battery option is sum(your_list) ) try this:
sum([i for i in [18, 14]])
24

Related

How to pull elements from a list that are nested between particular elements, and add them to new lists?

I have a daypart column (str), which has 1s or 0s for each hour of the day, depending if we choose to run a campaign during that hour.
Example:
daypart = '110011100111111100011110'
I want to convert this to the following string format:
'0-1, 4-6, 9-15, 19-22'
The above format is more readable, and shows during which hours the campaign ran.
Here's what I'm doing:
hours_list = []
ind = 0
for x in daypart:
if int(x) == 1:
hours_list.append(ind)
else:
hours_list.append('exclude')
ind += 1
The above gives me a list like this:
[0, 1, 'exclude', 'exclude', 4, 5, 6, 'exclude', 'exclude', 9, 10, 11, 12, 13, 14, 15, 'exclude', 'exclude', 'exclude', 19, 20, 21, 22, 'exclude']
Now I want to find a way to make the above into my desired output. What I am thinking of doing is finding which elements exist between 'exclude', and start adding them to new lists. I can then take the smallest and largest element from each list, join them with a '-', and append all such lists together.
Any ideas how I can do this, or a simpler way to do all of this?
Here's simple, readable code to get all intervals:
daypart = '1111111111111111111111'
hours= []
start, end = -1, -1
for i in range(len(daypart)):
if daypart[i] == "1":
if end != -1:
end += 1
else:
start = i
end = i
else:
if end!=-1:
hours.append([start, end])
start, end = -1,-1
if end!=-1:
hours.append([start, end])
start, end = -1,-1
print(hours)
I suggest that you convert directly to your desired format rather than using an intermediate representation that has the exact same information as the original input. Let's think about how we can do this in words:
Look for the first 1 in the input string
Add the index to a list
Look for the next 0 in the string.
Append one less than found index to a list. (Or maybe append the index from steps 2 and 4 as a pair?)
Continue by looking for the next 1 and repeat steps 2-4.
I leave translating this into code as an exercise for the reader.
This can be done using itertools.groupby, operator.itemgetter, enumerate in a comprehension to achieve this as well:
from itertools import groupby
from operator import itemgetter
daypart = '110011100111111100011110'
get_ends, get_one = itemgetter(0,-1), itemgetter(1)
output = ', '.join('{0[0]}-{1[0]}'.format(*get_ends(list(g))) for k,g in groupby(enumerate(daypart), get_one) if k=='1')
print(output)
0-1, 4-6, 9-15, 19-22
get_ends gets the first and last elements in each group and get_one just gets element 1 so to use it as a key.

Writing list with criteria 1 line - Python

I am trying to get better at writing more 'clean' and/or elegant code. I have seen examples around but I can't make them work for this particular example. Logic is very simple and I'm hoping that with some pointers on punctuation or syntax I can pick up the habit.
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
elist = []
evenlist = [i for i in a if a % 2 == 0]
print(evenlist)
I have only been able to make this work on the longer format here below:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
elist = []
for i in a:
if i % 2 == 0:
elist.append(i)
print(elist)
Should be this:
evenlist = [i for i in a if i % 2 == 0]
evenlist = [i for i in a if a % 2 == 0]
# ^
# Hmmm!
You probably wat to be checking i (an element) for evenness, rather than a (the entire list). Checking a list for evenness doesn't make much sense unless there's some magic happening in the background that ensures every element in the list is even. But there's not:
>>> [1,2,3] % 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'list' and 'int'
As an aside, there's another way to do this, specifically:
list(filter(lambda x: x % 2 == 0, a))
I think this is less readable myself but I suspect it may be more space-efficient if you just want to process them rather than create a list from them:
for item in filter(lambda x: x % 2 == 0, a):
do_something_with(item)
That doesn't create a whole new list, instead simply retrieving values from the existing list that match the filter criteria. Probably won't matter unless your lists tend to get large.

Python groupby itertools method

I am new to python programing.I am unable to create the following code:
Define a function even_or_odd, which takes an integer as input and returns the string even and odd, if the given number is even and odd respectively.
Categorise the numbers of list n = [10, 14, 16, 22, 9, 3 , 37] into two groups namely even and odd based on above defined function.
Hint : Use groupby method of itertools module.
Iterate over the obtained groupby object and print it's group name and list of elements associated with a group.
I have tried below code:
import itertools
def even_or_odd(n):
if n%2==0:
return 'even'
else:
return 'odd'
group=[]
uniquekeys=[]
n = [10, 14, 16, 22, 9, 3 , 37]
for k,g in itertools.groupby(n, even_or_odd(n)):
groups.append(list(g))
uniquekeys.append(k)
It is giving "TypeError: unsupported operand type(s) for %: 'list' and 'int' "
When you try to pass itertools.groupby even_or_odd you call it with n. even_or_odd takes an int, but n is a list. Just change it to itertools.groupby(n, even_or_odd)
Also you append to groups when I think you mean group.

Python split list if sequence of numbers is found

I've been trying to find a relevant question, though I can't seem to search for the right words and all I'm finding is how to check if a list contains an intersection.
Basically, I need to split a list once a certain sequence of numbers is found, similar to doing str.split(sequence)[0], but with lists instead. I have working code, though it doesn't seem very efficient (also no idea if raising an error was the right way to go about it), and I'm sure there must be a better way to do it.
For the record, long_list could potentially have a length of a few million values, which is why I think iterating through them all might not be the best idea.
long_list = [2,6,4,2,7,98,32,5,15,4,2,6,43,23,95,10,31,5,1,73]
end_marker = [6,43,23,95]
end_marker_len = len(end_marker)
class SuccessfulTruncate(Exception):
pass
try:
counter = 0
for i in range(len(long_list)):
if long_list[i] == end_marker[counter]:
counter += 1
else:
counter = 0
if counter == end_marker_len:
raise SuccessfulTruncate()
except SuccessfulTruncate:
long_list = long_list[:2 + i - end_marker_len]
else:
raise IndexError('sequence not found')
>>> long_list
[2,6,4,2,7,98,32,5,15,4,2]
Ok, timing a few answers with a big list of 1 million values (the marker is very near the end):
Tim: 3.55 seconds
Mine: 2.7 seconds
Dan: 0.55 seconds
Andrey: 0.28 seconds
Kasramvd: still executing :P
I have working code, though it doesn't seem very efficient (also no idea if raising an error was the right way to go about it), and I'm sure there must be a better way to do it.
I commented on the exception raising in my comment
Instead of raising an exception and catching it in the same try/except you can just omit the try/except and do if counter == end_marker_len: long_list = long_list[:2 + i - end_marker_len]. Successful is not a word thats fitting for an exception name. Exceptions are used to indicate that something failed
Anyway, here is a shorter way:
>>> long_list = [2,6,4,2,7,98,32,5,15,4,2,6,43,23,95,10,31,5,1,73]
>>> end_marker = [6,43,23,95]
>>> index = [i for i in range(len(long_list)) if long_list[i:i+len(end_marker)] == end_marker][0]
>>> long_list[:index]
[2, 6, 4, 2, 7, 98, 32, 5, 15, 4, 2]
List comprehension inspired by this post
As a more pythonic way instead of multiple slicing you can use itertools.islice within a list comprehension :
>>> from itertools import islice
>>> M,N=len(long_list),len(end_maker)
>>> long_list[:next((i for i in range(0,M) if list(islice(long_list,i,i+N))==end_marker),0)]
[2, 6, 4, 2, 7, 98, 32, 5, 15, 4, 2]
Note that since the default value of next function is 0 if it doesn't find any match it will returns the whole of long_list.
In my solution used approach with index method:
input = [2,6,4,2,7,98,32,5,15,4,2,6,43,23,95,10,31,5,1,73]
brk = [6,43,23,95]
brk_len = len(brk)
brk_idx = 0
brk_offset = brk_idx + brk_len
try:
while input[brk_idx:brk_offset] != brk:
brk_idx = input.index(brk[0], brk_idx + 1)
brk_offset = brk_idx + brk_len
except ValueError:
print("Not found")
else:
print(input[:brk_idx])
If the values are of limited range, say fit in bytes (this can also be adapted to larger types), why not then encode the lists so that the string method find could be used:
long_list = [2,6,4,2,7,98,32,5,15,4,2,6,43,23,95,10,31,5,1,73]
end_marker = [6,43,23,95]
import struct
long_list_p = struct.pack('B'*len(long_list), *long_list)
end_marker_p = struct.pack('B'*len(end_marker), *end_marker)
print long_list[:long_list_p.find(end_marker_p)]
Prints:
[2, 6, 4, 2, 7, 98, 32, 5, 15, 4, 2]
I tried using bytes as in but the find method they had didn't work:
print long_list[:bytes(long_list).find(bytes(end_marker))]

Useful code which uses reduce()? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Does anyone here have any useful code which uses reduce() function in python? Is there any code other than the usual + and * that we see in the examples?
Refer Fate of reduce() in Python 3000 by GvR
The other uses I've found for it besides + and * were with and and or, but now we have any and all to replace those cases.
foldl and foldr do come up in Scheme a lot...
Here's some cute usages:
Flatten a list
Goal: turn [[1, 2, 3], [4, 5], [6, 7, 8]] into [1, 2, 3, 4, 5, 6, 7, 8].
reduce(list.__add__, [[1, 2, 3], [4, 5], [6, 7, 8]], [])
List of digits to a number
Goal: turn [1, 2, 3, 4, 5, 6, 7, 8] into 12345678.
Ugly, slow way:
int("".join(map(str, [1,2,3,4,5,6,7,8])))
Pretty reduce way:
reduce(lambda a,d: 10*a+d, [1,2,3,4,5,6,7,8], 0)
reduce() can be used to find Least common multiple for 3 or more numbers:
#!/usr/bin/env python
from math import gcd
from functools import reduce
def lcm(*args):
return reduce(lambda a,b: a * b // gcd(a, b), args)
Example:
>>> lcm(100, 23, 98)
112700
>>> lcm(*range(1, 20))
232792560
reduce() could be used to resolve dotted names (where eval() is too unsafe to use):
>>> import __main__
>>> reduce(getattr, "os.path.abspath".split('.'), __main__)
<function abspath at 0x009AB530>
Find the intersection of N given lists:
input_list = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]
result = reduce(set.intersection, map(set, input_list))
returns:
result = set([3, 4, 5])
via: Python - Intersection of two lists
I think reduce is a silly command. Hence:
reduce(lambda hold,next:hold+chr(((ord(next.upper())-65)+13)%26+65),'znlorabggbbhfrshy','')
The usage of reduce that I found in my code involved the situation where I had some class structure for logic expression and I needed to convert a list of these expression objects to a conjunction of the expressions. I already had a function make_and to create a conjunction given two expressions, so I wrote reduce(make_and,l). (I knew the list wasn't empty; otherwise it would have been something like reduce(make_and,l,make_true).)
This is exactly the reason that (some) functional programmers like reduce (or fold functions, as such functions are typically called). There are often already many binary functions like +, *, min, max, concatenation and, in my case, make_and and make_or. Having a reduce makes it trivial to lift these operations to lists (or trees or whatever you got, for fold functions in general).
Of course, if certain instantiations (such as sum) are often used, then you don't want to keep writing reduce. However, instead of defining the sum with some for-loop, you can just as easily define it with reduce.
Readability, as mentioned by others, is indeed an issue. You could argue, however, that only reason why people find reduce less "clear" is because it is not a function that many people know and/or use.
Function composition: If you already have a list of functions that you'd like to apply in succession, such as:
color = lambda x: x.replace('brown', 'blue')
speed = lambda x: x.replace('quick', 'slow')
work = lambda x: x.replace('lazy', 'industrious')
fs = [str.lower, color, speed, work, str.title]
Then you can apply them all consecutively with:
>>> call = lambda s, func: func(s)
>>> s = "The Quick Brown Fox Jumps Over the Lazy Dog"
>>> reduce(call, fs, s)
'The Slow Blue Fox Jumps Over The Industrious Dog'
In this case, method chaining may be more readable. But sometimes it isn't possible, and this kind of composition may be more readable and maintainable than a f1(f2(f3(f4(x)))) kind of syntax.
You could replace value = json_obj['a']['b']['c']['d']['e'] with:
value = reduce(dict.__getitem__, 'abcde', json_obj)
If you already have the path a/b/c/.. as a list. For example, Change values in dict of nested dicts using items in a list.
#Blair Conrad: You could also implement your glob/reduce using sum, like so:
files = sum([glob.glob(f) for f in args], [])
This is less verbose than either of your two examples, is perfectly Pythonic, and is still only one line of code.
So to answer the original question, I personally try to avoid using reduce because it's never really necessary and I find it to be less clear than other approaches. However, some people get used to reduce and come to prefer it to list comprehensions (especially Haskell programmers). But if you're not already thinking about a problem in terms of reduce, you probably don't need to worry about using it.
reduce can be used to support chained attribute lookups:
reduce(getattr, ('request', 'user', 'email'), self)
Of course, this is equivalent to
self.request.user.email
but it's useful when your code needs to accept an arbitrary list of attributes.
(Chained attributes of arbitrary length are common when dealing with Django models.)
reduce is useful when you need to find the union or intersection of a sequence of set-like objects.
>>> reduce(operator.or_, ({1}, {1, 2}, {1, 3})) # union
{1, 2, 3}
>>> reduce(operator.and_, ({1}, {1, 2}, {1, 3})) # intersection
{1}
(Apart from actual sets, an example of these are Django's Q objects.)
On the other hand, if you're dealing with bools, you should use any and all:
>>> any((True, False, True))
True
I'm writing a compose function for a language, so I construct the composed function using reduce along with my apply operator.
In a nutshell, compose takes a list of functions to compose into a single function. If I have a complex operation that is applied in stages, I want to put it all together like so:
complexop = compose(stage4, stage3, stage2, stage1)
This way, I can then apply it to an expression like so:
complexop(expression)
And I want it to be equivalent to:
stage4(stage3(stage2(stage1(expression))))
Now, to build my internal objects, I want it to say:
Lambda([Symbol('x')], Apply(stage4, Apply(stage3, Apply(stage2, Apply(stage1, Symbol('x'))))))
(The Lambda class builds a user-defined function, and Apply builds a function application.)
Now, reduce, unfortunately, folds the wrong way, so I wound up using, roughly:
reduce(lambda x,y: Apply(y, x), reversed(args + [Symbol('x')]))
To figure out what reduce produces, try these in the REPL:
reduce(lambda x, y: (x, y), range(1, 11))
reduce(lambda x, y: (y, x), reversed(range(1, 11)))
reduce can be used to get the list with the maximum nth element
reduce(lambda x,y: x if x[2] > y[2] else y,[[1,2,3,4],[5,2,5,7],[1,6,0,2]])
would return [5, 2, 5, 7] as it is the list with max 3rd element +
Reduce isn't limited to scalar operations; it can also be used to sort things into buckets. (This is what I use reduce for most often).
Imagine a case in which you have a list of objects, and you want to re-organize it hierarchically based on properties stored flatly in the object. In the following example, I produce a list of metadata objects related to articles in an XML-encoded newspaper with the articles function. articles generates a list of XML elements, and then maps through them one by one, producing objects that hold some interesting info about them. On the front end, I'm going to want to let the user browse the articles by section/subsection/headline. So I use reduce to take the list of articles and return a single dictionary that reflects the section/subsection/article hierarchy.
from lxml import etree
from Reader import Reader
class IssueReader(Reader):
def articles(self):
arts = self.q('//div3') # inherited ... runs an xpath query against the issue
subsection = etree.XPath('./ancestor::div2/#type')
section = etree.XPath('./ancestor::div1/#type')
header_text = etree.XPath('./head//text()')
return map(lambda art: {
'text_id': self.id,
'path': self.getpath(art)[0],
'subsection': (subsection(art)[0] or '[none]'),
'section': (section(art)[0] or '[none]'),
'headline': (''.join(header_text(art)) or '[none]')
}, arts)
def by_section(self):
arts = self.articles()
def extract(acc, art): # acc for accumulator
section = acc.get(art['section'], False)
if section:
subsection = acc.get(art['subsection'], False)
if subsection:
subsection.append(art)
else:
section[art['subsection']] = [art]
else:
acc[art['section']] = {art['subsection']: [art]}
return acc
return reduce(extract, arts, {})
I give both functions here because I think it shows how map and reduce can complement each other nicely when dealing with objects. The same thing could have been accomplished with a for loop, ... but spending some serious time with a functional language has tended to make me think in terms of map and reduce.
By the way, if anybody has a better way to set properties like I'm doing in extract, where the parents of the property you want to set might not exist yet, please let me know.
Not sure if this is what you are after but you can search source code on Google.
Follow the link for a search on 'function:reduce() lang:python' on Google Code search
At first glance the following projects use reduce()
MoinMoin
Zope
Numeric
ScientificPython
etc. etc. but then these are hardly surprising since they are huge projects.
The functionality of reduce can be done using function recursion which I guess Guido thought was more explicit.
Update:
Since Google's Code Search was discontinued on 15-Jan-2012, besides reverting to regular Google searches, there's something called Code Snippets Collection that looks promising. A number of other resources are mentioned in answers this (closed) question Replacement for Google Code Search?.
Update 2 (29-May-2017):
A good source for Python examples (in open-source code) is the Nullege search engine.
After grepping my code, it seems the only thing I've used reduce for is calculating the factorial:
reduce(operator.mul, xrange(1, x+1) or (1,))
import os
files = [
# full filenames
"var/log/apache/errors.log",
"home/kane/images/avatars/crusader.png",
"home/jane/documents/diary.txt",
"home/kane/images/selfie.jpg",
"var/log/abc.txt",
"home/kane/.vimrc",
"home/kane/images/avatars/paladin.png",
]
# unfolding of plain filiname list to file-tree
fs_tree = ({}, # dict of folders
[]) # list of files
for full_name in files:
path, fn = os.path.split(full_name)
reduce(
# this fucction walks deep into path
# and creates placeholders for subfolders
lambda d, k: d[0].setdefault(k, # walk deep
({}, [])), # or create subfolder storage
path.split(os.path.sep),
fs_tree
)[1].append(fn)
print fs_tree
#({'home': (
# {'jane': (
# {'documents': (
# {},
# ['diary.txt']
# )},
# []
# ),
# 'kane': (
# {'images': (
# {'avatars': (
# {},
# ['crusader.png',
# 'paladin.png']
# )},
# ['selfie.jpg']
# )},
# ['.vimrc']
# )},
# []
# ),
# 'var': (
# {'log': (
# {'apache': (
# {},
# ['errors.log']
# )},
# ['abc.txt']
# )},
# [])
#},
#[])
I just found useful usage of reduce: splitting string without removing the delimiter. The code is entirely from Programatically Speaking blog. Here's the code:
reduce(lambda acc, elem: acc[:-1] + [acc[-1] + elem] if elem == "\n" else acc + [elem], re.split("(\n)", "a\nb\nc\n"), [])
Here's the result:
['a\n', 'b\n', 'c\n', '']
Note that it handles edge cases that popular answer in SO doesn't. For more in-depth explanation, I am redirecting you to original blog post.
I used reduce to concatenate a list of PostgreSQL search vectors with the || operator in sqlalchemy-searchable:
vectors = (self.column_vector(getattr(self.table.c, column_name))
for column_name in self.indexed_columns)
concatenated = reduce(lambda x, y: x.op('||')(y), vectors)
compiled = concatenated.compile(self.conn)
I have an old Python implementation of pipegrep that uses reduce and the glob module to build a list of files to process:
files = []
files.extend(reduce(lambda x, y: x + y, map(glob.glob, args)))
I found it handy at the time, but it's really not necessary, as something similar is just as good, and probably more readable
files = []
for f in args:
files.extend(glob.glob(f))
Let say that there are some yearly statistic data stored a list of Counters.
We want to find the MIN/MAX values in each month across the different years.
For example, for January it would be 10. And for February it would be 15.
We need to store the results in a new Counter.
from collections import Counter
stat2011 = Counter({"January": 12, "February": 20, "March": 50, "April": 70, "May": 15,
"June": 35, "July": 30, "August": 15, "September": 20, "October": 60,
"November": 13, "December": 50})
stat2012 = Counter({"January": 36, "February": 15, "March": 50, "April": 10, "May": 90,
"June": 25, "July": 35, "August": 15, "September": 20, "October": 30,
"November": 10, "December": 25})
stat2013 = Counter({"January": 10, "February": 60, "March": 90, "April": 10, "May": 80,
"June": 50, "July": 30, "August": 15, "September": 20, "October": 75,
"November": 60, "December": 15})
stat_list = [stat2011, stat2012, stat2013]
print reduce(lambda x, y: x & y, stat_list) # MIN
print reduce(lambda x, y: x | y, stat_list) # MAX
I have objects representing some kind of overlapping intervals (genomic exons), and redefined their intersection using __and__:
class Exon:
def __init__(self):
...
def __and__(self,other):
...
length = self.length + other.length # (e.g.)
return self.__class__(...length,...)
Then when I have a collection of them (for instance, in the same gene), I use
intersection = reduce(lambda x,y: x&y, exons)
def dump(fname,iterable):
with open(fname,'w') as f:
reduce(lambda x, y: f.write(unicode(y,'utf-8')), iterable)
Using reduce() to find out if a list of dates are consecutive:
from datetime import date, timedelta
def checked(d1, d2):
"""
We assume the date list is sorted.
If d2 & d1 are different by 1, everything up to d2 is consecutive, so d2
can advance to the next reduction.
If d2 & d1 are not different by 1, returning d1 - 1 for the next reduction
will guarantee the result produced by reduce() to be something other than
the last date in the sorted date list.
Definition 1: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider consecutive
Definition 2: 1/1/14, 1/2/14, 1/2/14, 1/3/14 is consider not consecutive
"""
#if (d2 - d1).days == 1 or (d2 - d1).days == 0: # for Definition 1
if (d2 - d1).days == 1: # for Definition 2
return d2
else:
return d1 + timedelta(days=-1)
# datelist = [date(2014, 1, 1), date(2014, 1, 3),
# date(2013, 12, 31), date(2013, 12, 30)]
# datelist = [date(2014, 2, 19), date(2014, 2, 19), date(2014, 2, 20),
# date(2014, 2, 21), date(2014, 2, 22)]
datelist = [date(2014, 2, 19), date(2014, 2, 21),
date(2014, 2, 22), date(2014, 2, 20)]
datelist.sort()
if datelist[-1] == reduce(checked, datelist):
print "dates are consecutive"
else:
print "dates are not consecutive"

Categories