如何在数据帧中存储的一系列数字中找到所有可能的女儿

时间:2016-12-15 16:13:12

标签: python numpy search dataframe

我有一个python数据框,其中一个列如column1包含一系列数字。我必须提到,每个这些数字都是细胞突变的结果,因此编号为n的细胞偏离两个细胞,其数字如下:2*n2*n+1。我想在此列中搜索以查找对应于特定数字k的女儿的所有行。我指的是{2*k, 2*k+1, 2*(2*k), 2*(2*k+1), ... }中包含所有可能column1的行。我不想使用树形结构,我该如何处理解决方案?感谢

2 个答案:

答案 0 :(得分:1)

这两个序列看起来像是binary expansion starts with 10的数字和binary expansion starts with 11的数字。

可以直接找到两个序列:

import math

def f(n=2):
    while True:
        yield int(n + 2**math.floor(math.log(n,2)))
        n += 1

def g(n=2):
    while True:
        yield int(n + 2 * 2**math.floor(math.log(n,2)))
        n += 1

a, b = f(), g()
print [a.next() for i in range(15)]
print [b.next() for i in range(15)]
>>> [4, 5, 8, 9, 10, 11, 16, 17, 18, 19, 20, 21, 22, 23, 32]
>>> [6, 7, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31, 48]

编辑:

对于任意起点,您可以执行以下操作,我认为这符合您的标准。

import Queue

def f(k):
    q = Queue.Queue()
    q.put(k)

    while not q.empty():
        p = q.get()
        a, b = 2*p, 2*p+1
        q.put(a)
        q.put(b)
        yield a
        yield b

a = f(4)
print [a.next() for i in range(16)]
>>> [8, 9, 16, 17, 18, 19, 32, 33, 34, 35, 36, 37, 38, 39, 64, 65] # ...

a = f(5)
print [a.next() for i in range(16)]
>>> [10, 11, 20, 21, 22, 23, 40, 41, 42, 43, 44, 45, 46, 47, 80, 81] # ...

根据OEIS检查这些序列:

f(2) - Starting 10  - A004754
f(3) - Starting 11  - A004755
f(4) - Starting 100 - A004756
f(5) - Starting 101 - A004756
f(6) - Starting 110 - A004758
f(7) - Starting 111 - A004759
...

这意味着你可以做到:

import math

def f(k, n=2):
    while True:
        yield int(n + (k-1) * 2**math.floor(math.log(n, 2)))
        n+=1

for i in range(2,8):
    a = f(i)
    print i, [a.next() for j in range(16)]

>>> 2 [4, 5, 8, 9, 10, 11, 16, 17, 18, 19, 20, 21, 22, 23, 32]
>>> 3 [6, 7, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31, 48]
>>> 4 [8, 9, 16, 17, 18, 19, 32, 33, 34, 35, 36, 37, 38, 39, 64]
>>> 5 [10, 11, 20, 21, 22, 23, 40, 41, 42, 43, 44, 45, 46, 47, 80]
>>> 6 [12, 13, 24, 25, 26, 27, 48, 49, 50, 51, 52, 53, 54, 55, 96]
>>> 7 [14, 15, 28, 29, 30, 31, 56, 57, 58, 59, 60, 61, 62, 63, 112]
# ... where the first number is shown for clarity.

答案 1 :(得分:0)

丑陋但似乎工作正常。我认为你可能需要知道的是更新的yield from结构。在此代码中使用了两次。没想到我会。

from fractions import Fraction
from itertools import count

def daughters(k):
    print ('daughters of cell', k)
    if k<=0:
        return
    if k==1:
        yield from count(1)

    def locateK():
        cells = 1
        newCells = 2
        generation = 1
        while True:
            generation += 1
            previousCells = cells
            cells += newCells 
            newCells *= 2
            if k > previousCells and k <= cells :
                break
        return ( generation, k - previousCells )

    parentGeneration, parentCell = locateK()

    cells = 1
    newCells = 2
    generation = 1
    while True:
        generation += 1
        previousCells = cells
        if generation > parentGeneration:
            if parentCell%2:
                firstChildCell=previousCells+int(Fraction(parentCell-1, 2**parentGeneration)*newCells)+1
            else:
                firstChildCell=previousCells+int(Fraction(parentCell, 2**parentGeneration)*newCells)+1
            yield from range(firstChildCell, firstChildCell+int(newCells*Fraction(1,2)))
        cells += newCells
        newCells *= 2

for n, d in enumerate(daughters(2)):
    print (d)
    if n > 15:
        break

几个有代表性的结果:

daughters of cell 2
4
5
8
9
10
11
16
17
18
19
20
21
22
23
32
33
34


daughters of cell 3
6
7
12
13
14
15
24
25
26
27
28
29
30
31
48
49
50