2016-12-15 3 views
2

column1のような列の1つに数字の列が含まれるpythonデータフレームがあります。私はこれらの数字のそれぞれが細胞変異の結果であることを言及しなければならないので、数字nの細胞は、以下の数字を有する2つの細胞から逸脱する:2*nおよび2*n+1。私はすべての列が特定の番号kの娘に対応していることを見つけるためにこの列を検索したい。すべて{2*k, 2*k+1, 2*(2*k), 2*(2*k+1), ... }column1を含む行を意味します。ツリー構造を使いたくないのですが、どのように解決策にアプローチできますか?ありがとうデータフレームに格納された一連の数字の中で可能なすべての娘を見つける方法

+0

あなたは* K *の観点から、一連の複数の用語を供給していただけますか? –

+0

はい私の列は次のようなものです: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、...} 。 – ga97rasl

+0

ありがとう、私はこれについて考える必要があります。 –

答えて

0

醜いが、うまくいくようです。あなたが知る必要があったかもしれないと思うことは、より新しいyield fromの建設です。このコードでは2回使用されます。私は決して考えなかった。

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 
+0

ありがとうございましたビル、しかしこれはまさに私が子供のたびに両方の子供を生成する方法を知らない私の問題です。私は '2 * k + 1'も必要ですが、結果をトレースすると、13、15、...は '2 * 6 + 1'と '2 * 7 + 1'にはありません。 – ga97rasl

+0

だからこそ私は「これのようなもの」を書いたのです。 :)私はあなたが何を意味するかについて明確ではなかった。 –

+0

これは質問への答えを提供しません。批評をしたり、著者の説明を求めるには、投稿の下にコメントを残してください。 - [レビューから](/レビュー/低品質の投稿/ 14608765) –

1

2つの配列は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] 

EDIT:任意の出発点については

、あなたは私はあなたの基準を満たしていると考え、以下を行うことができます。

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. 
+0

ありがとうございますが、私は番号を与えて娘を取得する必要があります。あなたの解決策はnに関する情報を持つことに基づいていますが、残念ながらdfに格納されているより大きい数値になると、セルの順序に関する情報はありません。私は番号 "k"を指定し、すべての可能な娘2 * kと2 * k + 1を得る必要があります。 – ga97rasl

+0

大きな 'k'の例を挙げることができますか? – Benjamin

+0

セル生成中のセル番号なので、時々6842のような数がかかります。 – ga97rasl

関連する問題