Python 如何在数据帧中存储的数字序列中查找所有可能的子元素
我有一个python数据框架,它的一个列,如Python 如何在数据帧中存储的数字序列中查找所有可能的子元素,python,numpy,search,dataframe,Python,Numpy,Search,Dataframe,我有一个python数据框架,它的一个列,如column1包含一系列数字。我必须提到的是,每一个数字都是细胞突变的结果,因此编号为n的细胞会偏离成两个具有以下数字的细胞:2*n和2*n+1。我想在此列中搜索,以查找与特定编号的子行相对应的所有行k。我指的是在其列1中包含所有可能{2*k,2*k+1,2*(2*k),2*(2*k+1),…}的行。我不想使用树形结构,我该如何解决这个问题?谢谢很难看,但看起来还不错。我认为您可能需要知道的是施工中较新的产量。在本代码中使用了两次。没想到我会 from
column1
包含一系列数字。我必须提到的是,每一个数字都是细胞突变的结果,因此编号为n
的细胞会偏离成两个具有以下数字的细胞:2*n
和2*n+1
。我想在此列中搜索,以查找与特定编号的子行相对应的所有行k
。我指的是在其列1
中包含所有可能{2*k,2*k+1,2*(2*k),2*(2*k+1),…}
的行。我不想使用树形结构,我该如何解决这个问题?谢谢很难看,但看起来还不错。我认为您可能需要知道的是施工中较新的产量。在本代码中使用了两次。没想到我会
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
这两个序列看起来像是谁的数字和谁的数字
可以直接找到这两个序列:
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.
谢谢比尔,但这正是我的问题,我不知道每次如何生成两个孩子。我也需要'2*k+1',但是如果你追踪结果,你可以看到13,15。。。“2*6+1”和“2*7+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,}。谢谢,我需要考虑一下。谢谢你,但我需要给出数字并得到它的女儿。您的解决方案是基于关于n的信息,但不幸的是,对于df中存储的更大的数字,我没有关于单元格顺序的任何信息。我需要指定数字“k”并得到所有可能的子元素2*k和2*k+1。你能举一个更大的k
的例子吗?因为它是一个单元编号,在单元生成过程中,有时需要6842这样的数字。而序列会变成什么?它会在某个点停止,我得自己检查一下,比如女儿的深度小于1000。
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.