将扁平化数据源转变为分层数据源

发布于 2024-10-08 01:15:59 字数 564 浏览 5 评论 0原文

假设我有一个表,例如:

D_ID    C_ID    B_ID    A_ID
1D      1C      1B      1A
4D      2C      6B      1A
6D      1C      1B      1A
9D      1C      1B      1A
8D      2C      6B      1A

假设从结构上来说,我知道以下内容:

A's
 B's
  C's
   D's

即 D 是 C 的子级,C 是 B 的子级,依此类推。

我怎样才能将顶部的表变成分层数据源? 例如,

ID ParentID
1D  1C
4D  2C
6D  1C
9D  1C
8D  2C
1C  1B
2C  6B
1B  1A
6B  1A
1A  Null

这可以作为 Telerik TreeView 或其他分层控件的分层数据源吗? 我知道我可以迭代每个项目并自己构建它,但我想知道是否有更好的方法来迭代它。也许甚至有一种内置的方式来实现这一目标。

Let's say I have a table such as:

D_ID    C_ID    B_ID    A_ID
1D      1C      1B      1A
4D      2C      6B      1A
6D      1C      1B      1A
9D      1C      1B      1A
8D      2C      6B      1A

And let's say that structurally speaking, I know the following:

A's
 B's
  C's
   D's

That is D's are children of C's, C's of B's and so on.

How could I turn the table up top into a hierarchical data source?
Such as

ID ParentID
1D  1C
4D  2C
6D  1C
9D  1C
8D  2C
1C  1B
2C  6B
1B  1A
6B  1A
1A  Null

This could then serve as a hierarchical datasource for a Telerik TreeView or other hierarchical controls?
I know I could iterate over every item and build it myself, but I am wondering if there are better known ways to iterate this. Perhaps even a built in way to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-10-15 01:15:59

您可以编写一个简单的迭代来遍历文件并将元素对添加到字典中。在看起来像Python的伪代码中:

// open the file you want to parse.
file = open(my_log_file)

// create a hash map from ID to parent.
dictionary = {}

// read through the file line by line
for line in file.getlines():
   // ignore first line ...
   // read the 4 columns in each line
   columns[] = line.split(" ")

   // add pairs (id=column[i], parent=column[i+1]) to the hashmap
   dictionary[column[1]] = column[2]
   dictionary[column[2]] = column[3]
   dictionary[column[3]] = column[4]
   dictionary[column[4]] = Nil


// output the hashmap line by line.
print "ID", "Parent ID"
for (id, parent) in dictionary:
  print id, parent

我希望这会有所帮助。

You could write a simple iteration passing through the file and adding pairs of elements to a dictionary. In pythonish looking psuedo code:

// open the file you want to parse.
file = open(my_log_file)

// create a hash map from ID to parent.
dictionary = {}

// read through the file line by line
for line in file.getlines():
   // ignore first line ...
   // read the 4 columns in each line
   columns[] = line.split(" ")

   // add pairs (id=column[i], parent=column[i+1]) to the hashmap
   dictionary[column[1]] = column[2]
   dictionary[column[2]] = column[3]
   dictionary[column[3]] = column[4]
   dictionary[column[4]] = Nil


// output the hashmap line by line.
print "ID", "Parent ID"
for (id, parent) in dictionary:
  print id, parent

I hope this helps.

嘿哥们儿 2024-10-15 01:15:59

这是一个快速的想法...

如果我正确理解这一点,您可以从右到左迭代列表,并读取对...使用字典/地图/哈希表来组织它们。您可能需要为其定义一个自定义比较器,但这并不太难。

因此,当您从右到左、从上到下迭代时,您在阅读时添加对,如此......

parent = "1A";
child = "1B";
list.add(child, parent);
//Then on the next iteration.....
parent = "1B";
child = "1C";

依此类推......

这样使用子项作为键,您将得到所有的列表唯一值,这将成为您的 ID 列。

然后你的 list.values() 就成为你的 ParentID 列,因为它将包含所有拥有多个孩子的父母的重复项。这也是为什么孩子是完美的关键,因为你只能出生一次,但你可以有很多孩子。

编辑:呸!无论如何,有人用我所提议的更完整的版本击败了我……哦,好吧。我更喜欢我的口头描述;)

Here is a quick thought....

If I understand this correctly you can just iterate across the list from right to left, and read pairs... Use a dictionary/map/hashtable to keep them organized. You may need to define a custom comparator for it but thats not too hard.

So as you iterate along from right to left, top to bottom, you add the pairs as you read them so..

parent = "1A";
child = "1B";
list.add(child, parent);
//Then on the next iteration.....
parent = "1B";
child = "1C";

And so on you go...

This way using the child as the key, you are left with a list of all the unique values, this becomes your ID column.

Then your list.values() becomes your ParentID column since it will contain the duplicates for all of the parents that had more than one child. This is also why the child works perfectly as the key since you can only be born once, but you can have many children.

EDIT: BAH! Someone beat me to it with a much more complete version of what I was proposing anyway... oh well. I like my verbal description better ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文