标签: flatten

将邻接列表层次结构展平为所有路径的列表

我有一个表使用Adjacency List模型存储分层信息.(使用自引用键 - 下面的示例.此表可能看起来很熟悉):

category_id name                 parent
----------- -------------------- -----------
1           ELECTRONICS          NULL
2           TELEVISIONS          1
3           TUBE                 2
4           LCD                  2
5           PLASMA               2
6           PORTABLE ELECTRONICS 1
7           MP3 PLAYERS          6
8           FLASH                7
9           CD PLAYERS           6
10          2 WAY RADIOS         6
Run Code Online (Sandbox Code Playgroud)

将上述数据"压扁"成这样的东西的最佳方法是什么?

category_id lvl1        lvl2        lvl3        lvl4
----------- ----------- ----------- ----------- -----------
1           1           NULL        NULL        NULL
2           1           2           NULL        NULL
6           1           6           NULL        NULL
3           1           2           3           NULL
4           1 …
Run Code Online (Sandbox Code Playgroud)

sql hierarchy flatten adjacency-list

8
推荐指数
2
解决办法
9763
查看次数

如何从一个将多行返回到单个结果的查询中"合并","展平"或"旋转"结果?

我对表有一个简单的查询,它返回如下结果:

id    id_type  id_ref
2702  5        31
2702  16       14
2702  17       3
2702  40       1
2703  23       4
2703  23       5
2703  34       6
2704  1        14
Run Code Online (Sandbox Code Playgroud)

我想将结果合并为一行,例如:

id    concatenation 
2702  5,16,17,40:31,14,3,1
2703  23,23,34:4,5,6
2704  1:14
Run Code Online (Sandbox Code Playgroud)

有没有办法在触发器内执行此操作?

注意:我知道我可以使用光标,但我真的不愿意,除非没有更好的方法.

该数据库是Sybase 12.5.4版.

sql sybase pivot flatten

8
推荐指数
1
解决办法
9068
查看次数

AutoMapper和展平嵌套数组

我正在尝试使用AutoMapper来压缩多个级别的数组.

考虑以下源类:

class X {
    public string A { get; set; }
    public Y[] B { get; set; }
}

class Y {
    public string C { get; set; }
    public Z[] D { get; set; }
}

class Z {
    public string E { get; set; }
    public string F { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以下目的地:

class Destination {
    public string A { get; set; }
    public string C { get; set; }
    public string E { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# flatten automapper-2

8
推荐指数
1
解决办法
1万
查看次数

如何展平嵌套的结果?

我正在使用一个第三方库,该库提供了我必须“按原样”使用的基于树的数据结构。API 返回Result<T, Error>. 我必须进行一些顺序调用并将错误转换为我的应用程序的内部错误。

use std::error::Error;
use std::fmt;

pub struct Tree {
    branches: Vec<Tree>,
}

impl Tree {
    pub fn new(branches: Vec<Tree>) -> Self {
        Tree { branches }
    }

    pub fn get_branch(&self, id: usize) -> Result<&Tree, TreeError> {
        self.branches.get(id).ok_or(TreeError {
            description: "not found".to_string(),
        })
    }
}

#[derive(Debug)]
pub struct TreeError {
    description: String,
}

impl Error for TreeError {
    fn description(&self) -> &str {
        self.description.as_str()
    }
}

impl fmt::Display for TreeError {
    fn fmt(&self, f: &mut fmt::Formatter) …
Run Code Online (Sandbox Code Playgroud)

error-handling flatten rust

8
推荐指数
1
解决办法
3944
查看次数

使用连续的“要么/可能”时减少嵌套

这可能是一个非常基本的 Haskell 问题,但让我们假设以下函数签名

-- helper functions
getWeatherInfo :: Day -> IO (Either WeatherException WeatherInfo)
craftQuery :: WeatherInfo -> Either QueryException ModelQuery
makePrediction :: ModelQuery -> IO (Either ModelException ModelResult)
Run Code Online (Sandbox Code Playgroud)

将上述所有内容链接到一个predict day函数中的天真方法可能是:

predict :: Day -> IO (Maybe Prediction)
predict day = do
    weather <- getWeatherInfo day
    pure $ case weather of
        Left ex -> do
            log "could not get weather: " <> msg ex
            Nothing
        Right wi -> do
            let query = craftQuery wi
            case query of
                Left …
Run Code Online (Sandbox Code Playgroud)

monads haskell flatten either maybe

8
推荐指数
1
解决办法
138
查看次数

Python pandas:通过代理键将 JSON 扁平化为行的快速方法

我对诸如此类的包的了解pandas相当浅,我一直在寻找将数据展平为行的解决方案。有了dict这样的,有一个代理键名为entry_id

data = [
    {
        "id": 1,
        "entry_id": 123,
        "type": "ticker",
        "value": "IBM"
    },
    {
        "id": 2,
        "entry_id": 123,
        "type": "company_name",
        "value": "International Business Machines"
    },
    {
        "id": 3,
        "entry_id": 123,
        "type": "cusip",
        "value": "01234567"
    },
    {
        "id": 4,
        "entry_id": 321,
        "type": "ticker",
        "value": "AAPL"
    },
    {
        "id": 5,
        "entry_id": 321,
        "type": "permno",
        "value": "123456"
    },
    {
        "id": 6,
        "entry_id": 321,
        "type": "company_name",
        "value": "Apple, Inc."
    },
    {
        "id": 7,
        "entry_id": 321,
        "type": "formation_date", …
Run Code Online (Sandbox Code Playgroud)

python flatten dataframe pandas

8
推荐指数
1
解决办法
228
查看次数

在 Python 中展平复杂的目录结构

我想将文件从复杂的目录结构移动到一个地方。例如,我有这个深层次的层次结构:

foo/
    foo2/
        1.jpg
    2.jpg
    ...
Run Code Online (Sandbox Code Playgroud)

我希望它是:

1.jpg
2.jpg
...
Run Code Online (Sandbox Code Playgroud)

我目前的解决方案:

def move(destination):
    for_removal = os.path.join(destination, '\\')
    is_in_parent = lambda x: x.find(for_removal) > -1
    with directory(destination):
        files_to_move = filter(is_in_parent,
                               glob_recursive(path='.'))
    for file in files_to_move:
        shutil.move(file, destination)
Run Code Online (Sandbox Code Playgroud)

定义:directoryglob_recursive。请注意,我的代码仅将文件移动到它们的公共父目录,而不是任意目标。

如何简洁优雅地将所有文件从复杂的层次结构移动到一个地方?

python directory-structure flatten file-move

7
推荐指数
3
解决办法
8443
查看次数

如何使用各种数据类型展平列表(int,tuple)

假设我有一个包含一个或多个元组的列表:

[0, 2, (1, 2), 5, 2, (3, 5)]
Run Code Online (Sandbox Code Playgroud)

什么是摆脱元组的最佳方法,以便它只是一个int列表?

[0, 2, 1, 2, 5, 2, 3, 5]
Run Code Online (Sandbox Code Playgroud)

python tuples list flatten

7
推荐指数
2
解决办法
1037
查看次数

在Kotlin展平阵列

我有一个二维节点数组,我希望使用Kotlin数组的flatten函数将其展平为所有节点的单个数组.

    val nodes = kotlin.Array(width, { width ->
    kotlin.Array(height, { height -> Node(width, height) })
})
Run Code Online (Sandbox Code Playgroud)

然后我尝试在2D数组上调用flatten函数

nodes.flatten()
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:Type mismatch: inferred type is Array<Array<Node>> but Array<Array<out ???>> was expected.还有另一种方法我应该这样做吗?

arrays flatten multidimensional-array kotlin

7
推荐指数
2
解决办法
2974
查看次数

列表上的哪些函数满足连接法则?

哪些函数f :: [a] -> [a]满足定律:

f . concat = concat . f . map f
Run Code Online (Sandbox Code Playgroud)

我能想到id、、reverse还有const []-还有其他的吗?

haskell flatten

7
推荐指数
1
解决办法
149
查看次数