【问题标题】:How to find all possible daughters in a sequence of numbers stored in dataframe如何在存储在数据框中的数字序列中找到所有可能的女儿
【发布时间】:2017-05-01 07:40:46
【问题描述】:

我有一个 python 数据框,其中一个列(例如column1)包含一系列数字。我不得不提一下,这些数字中的每一个都是细胞突变的结果,因此编号为n 的细胞会偏离具有以下编号的两个细胞:2*n2*n+1。我想在此列中搜索以查找与特定编号k 的女儿相对应的所有行。我的意思是在它们的column1 中包含所有可能的{2*k, 2*k+1, 2*(2*k), 2*(2*k+1), ... } 的行。我不想使用树结构,我该如何解决?谢谢

【问题讨论】:

  • 能否请您提供更多关于 k 的系列术语?
  • 是的,我的列是这样的:1,2,3,4,5,6,7,8,9, ...其中 1 对应于第一个单元格。然后第一个单元格区分为 2 和 3。然后 2 区分为 4 和 5,其中 3 区分为 6 和 7。这意味着对于数字 2,我想在这些项目之间搜索 {4,5,8,9,10, 11,16,17,18,19 ,...} 和数字 3 我想在这些项目之间搜索 {6,7,12,13,14,15,24,25,26,27,...} .
  • 谢谢,我需要考虑一下。

标签: python numpy search dataframe


【解决方案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.

【讨论】:

  • 谢谢,但我需要提供号码并获取其女儿。您的解决方案基于有关 n 的信息,但不幸的是,当涉及存储在 df 中的较大数字时,我没有任何有关单元格顺序的信息。我需要指定数字“k”并获得所有可能的女儿 2*k 和 2*k+1。
  • 你能举一个更大的k的例子吗?
  • 因为它是单元格生成过程中的单元格编号,因此有时需要像 6842 这样的数字。
  • 系列变成了什么?
  • 它会在某个时候停止,我必须自己检查它,例如考虑女儿的深度小于 1000。
【解决方案2】:

丑陋但似乎工作正常。我认为您可能需要知道的是较新的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

【讨论】:

  • 谢谢比尔,但这正是我的问题,我不知道如何每次都生成两个孩子。我也需要 '2*k+1' ,但是如果您跟踪结果,您会看到 13, 15 , ... 不存在 '2*6+1' 和 '2*7+1'。
  • 这就是我写“类似这样的东西”的原因。 :) 我不清楚你的意思。
  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
  • @GregGlockner:请再看一遍。只是停滞了一段时间。
  • 非常感谢。看来你付出了这么多努力,对我帮助很大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2019-10-29
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
相关资源
最近更新 更多