itertools

How to read two lines in a data from same column to create combination of values from that column?

江枫思渺然 提交于 2019-12-08 03:28:23
问题 In the following data: M1 M2 M3 M4 M5 M6 M7 M8 Hx Hy S1 S2 S3 S4 A T T A A G A C A C C G C T A T T A A G A C A C C G C T T G C T G T T G T A A T A T C A A C A G T C C G G A C G G T G T A T C T G T C T T T the following code was used: d1 = d1.add('g').add(d1.shift()).dropna() to get: M1 M2 M3 M4 M5 M6 M7 M8 H0 H1 S1 S2 S3 S4 AgA TgT TgT AgA AgA GgG AgA CgC AgA CgC CgC GgG CgC TgT TgA GgT CgT TgA GgA TgG TgA GgC TgA AgC AgC TgG AgC TgT CgT AgG AgC CgT AgG GgT TgT CgG CgT GgA GgA AgT CgA GgT GgC

Average on overlapping windows in Python

旧城冷巷雨未停 提交于 2019-12-08 02:45:11
问题 I'm trying to compute a moving average but with a set step size between each average. For example, if I was computing the average of a 4 element window every 2 elements: data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] This should produce the average of [1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10]. window_avg = [2.5, 4.5, 6.5, 8.5] My data is such that the ending will be truncated before processing so there is no problem with the length with respect to window size. I've read a bit about how

Generating all possible combinations in a nested dictionary

时光怂恿深爱的人放手 提交于 2019-12-08 02:10:07
问题 I need to test all possible installation configurations possible. The configurations are kept in a dictionary array that sometimes contains nested arrays. Here's a sample of the configuration information (actual configuration is much longer): config = {'database': 'sqlite', 'useExisting': False, 'userCredentials': {'authType': 'windows', 'user': r'.\Testing', 'password': 'testing' } } For database , options are ['sqlite','mysql','oracle'] , and for useExisting , the options are [True, False]

python list group by first character

天大地大妈咪最大 提交于 2019-12-08 01:41:02
问题 list1=['hello','hope','hate','hack','bit','basket','code','come','chess'] What I need is: list2=[['hello','hope','hate','hack'],['bit','basket'],['code','come','chess']] If the first character is the same and is the same group, then sublist it. How can I solve this? 回答1: You can use itertools.groupby: >>> from itertools import groupby >>> list1 = ['hello','hope','hate','hack','bit','basket','code','come','chess'] >>> [list(g) for k, g in groupby(list1, key=lambda x: x[0])] [['hello', 'hope',

Exhaustive combinations of lists in python

房东的猫 提交于 2019-12-08 01:30:29
问题 I have a long list of lists in Python that looks something like this: myList=[ ('a',[1,2,3,4,5]), ('b',[6,7,8,9,10]), ('c',[1,3,5,7,9]), ('d',[2,4,6,8,10]), ('e',[4,5,6,7,8]) ] And I would like to enumerate the common values exhaustively ('a:b', ), ('a:c', [1,3,5]), ('a:d', [2,4]), ('a:e', [4,5]), ('b:c', [7,9]), ('b:d', [6,8,10]), ('a:c:e', [5]), ('b:c:e', [7]), ('b:d:e', [6,8]), and the same for groups of four, five, six until all common values have been identified (assuming the lists were

Missing a sequence when using itertools combonations_with_replacement

老子叫甜甜 提交于 2019-12-07 17:13:49
问题 from itertools import combinations_with_replacement x = 'opo' v = combinations_with_replacement(x, len(x)) ans = [''.join(map(str, x)) for x in v] print(" ".join(set(ans))) I'm not sure why im missing the sequence pop here. Why does pop not show but ppo and opp do . expected output opp ppp poo ppo ooo opo oop pop actual output opp ppp poo ppo ooo opo oop 回答1: Consider this: >>> x = 'abc' >>> v = itertools.combinations_with_replacement(x, len(x)) >>> ans = [''.join(map(str, x)) for x in v] >>>

How do I iterate over a large number of tuples of integers in the order of their sum?

泪湿孤枕 提交于 2019-12-07 16:52:01
问题 I'm using itertools.combinations() to iterate over tuples of integers. I am interested in the tuple with the lowest sum that satisfies some conditions: def findLowestNiceTuple: for tup in itertools.combinations(range(1, 6), 2): if niceTuple(tup): return tup The generator's default order is not in the order of the elements' sum. For example: >>> itertools.combinations(range(1, 6), 2) gives a generator which will yield the following elements: [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2,

How to repeat each of a Python list's elements n times with itertools only?

浪子不回头ぞ 提交于 2019-12-07 14:50:03
问题 I have a list with numbers: numbers = [1, 2, 3, 4] . I would like to have a list where they repeat n times like so (for n = 3 ): [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4] . The problem is that I would like to only use itertools for this, since I am very constrained in performance. I tried to use this expression: list(itertools.chain.from_iterable(itertools.repeat(numbers, 3))) But it gives me this kind of result: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] which is obviously not what I need. Is there a

Map on iterators of different length

空扰寡人 提交于 2019-12-07 12:55:17
问题 I was answering this question and faced the following problem: >>> from operator import add >>> map(add,[1,2,3],[1,2]) Traceback (most recent call last): File "<pyshell#47>", line 1, in <module> map(add,[1,2,3],[1,2]) TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' I wanted map to stop as soon as the smallest iterator provided in the parameters is consumed. I found the solution: >>> from itertools import imap >>> list(imap(add,[1,2,3],[1,2])) [2, 4] But, why is that?

Difference between `return iterator` and `yield from iterator`

限于喜欢 提交于 2019-12-07 12:32:01
问题 I'm trying to implement my own version of itertools.compress , the problem is that i stumbled upon the return type. I mean both of these functions return an iterator, but i think the second one is not considered a generator function because there is no yield statement inside. So my question is, are these two implementations equivalent ? def compress (seq, selectors): from operator import itemgetter fst = itemgetter (0) snd = itemgetter (1) yield from map (fst, filter (snd, zip (seq, selectors