列表的连续子集

发布于 2024-12-26 13:25:55 字数 231 浏览 1 评论 0原文

给定一个列表,

{"a", "b", "c", "d"}

是否有更简单的方法来生成这样的顺序子集列表(结果的顺序并不重要)

{
 {"a"},
 {"a b"},
 {"a b c"},
 {"a b c d"},
 {"b"},
 {"b c"},
 {"b c d"},
 {"c"},
 {"c d"},
 {"d"}
}

Given a list say

{"a", "b", "c", "d"}

Is there any easier way to generate list of sequential subsets like this (order of the result is not important)

{
 {"a"},
 {"a b"},
 {"a b c"},
 {"a b c d"},
 {"b"},
 {"b c"},
 {"b c d"},
 {"c"},
 {"c d"},
 {"d"}
}

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

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

发布评论

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

评论(7

春花秋月 2025-01-02 13:25:56
Flatten[Partition[{a, b, c, d}, #, 1] & /@ {1, 2, 3, 4}, 1]

给出

{{a}、{b}、{c}、{d}、{a、b}、{b、c}、{c、d}、{a、b、c}、{b、c , d}, {a,
b、c、d}}

Flatten[Partition[{a, b, c, d}, #, 1] & /@ {1, 2, 3, 4}, 1]

gives

{{a}, {b}, {c}, {d}, {a, b}, {b, c}, {c, d}, {a, b, c}, {b, c, d}, {a,
b, c, d}}

苦行僧 2025-01-02 13:25:56

这个怎么样:

origset = {"a", "b", "c", "d"};

bdidxset = Subsets[Range[4], {1, 2}]

origset[[#[[1]] ;; #[[-1]]]] & /@ bdidxset

这给出了

{{"a"}, {"b"}, {"c"}, {"d"}, {"a", "b"}, {"a", "b", "c"}, {"a", "b", 
  "c", "d"}, {"b", "c"}, {"b", "c", "d"}, {"c", "d"}}

How about this:

origset = {"a", "b", "c", "d"};

bdidxset = Subsets[Range[4], {1, 2}]

origset[[#[[1]] ;; #[[-1]]]] & /@ bdidxset

which gives

{{"a"}, {"b"}, {"c"}, {"d"}, {"a", "b"}, {"a", "b", "c"}, {"a", "b", 
  "c", "d"}, {"b", "c"}, {"b", "c", "d"}, {"c", "d"}}
甜宝宝 2025-01-02 13:25:56

我更喜欢 TomD 的方法,但这就是我想到的,没有字符串处理:

set = {"a", "b", "c", "d"};

n = Length@set;

Join @@ Table[set~Take~{s, f}, {s, n}, {f, s, n}] // Column

Mathematicagraphics

I like TomD's method better, but this is what came to my mind, sans string processing:

set = {"a", "b", "c", "d"};

n = Length@set;

Join @@ Table[set~Take~{s, f}, {s, n}, {f, s, n}] // Column

Mathematica graphics

﹎☆浅夏丿初晴 2025-01-02 13:25:56

这是一种可能的解决方案

a={"a","b","c","d"};
StringJoin@Riffle[#, " "] & /@ 
  DeleteDuplicates[
   LongestCommonSubsequence[a, #] & /@ 
    DeleteCases[Subsets@a, {}]] // Column

结果

a
b
c
d
a b
b c
c d
a b c
b c d
a b c d

Here is one possible solution

a={"a","b","c","d"};
StringJoin@Riffle[#, " "] & /@ 
  DeleteDuplicates[
   LongestCommonSubsequence[a, #] & /@ 
    DeleteCases[Subsets@a, {}]] // Column

Result

a
b
c
d
a b
b c
c d
a b c
b c d
a b c d
调妓 2025-01-02 13:25:56

一种方式:

makeList[lst_] := Map[ Union[lst[[1 ;; #]]] &, Range@Length[lst]]
r = Map[makeList[lst[[# ;; -1]]] &, Range@Length[lst]];
Flatten[r, 1]

给予

{{"a"}, 
 {"a", "b"}, 
 {"a", "b", "c"}, 
 {"a", "b", "c", "d"}, 
 {"b"},
 {"b", "c"},
 {"b", "c", "d"},
 {"c"}, 
 {"c", "d"}, 
 {"d"}}

one way:

makeList[lst_] := Map[ Union[lst[[1 ;; #]]] &, Range@Length[lst]]
r = Map[makeList[lst[[# ;; -1]]] &, Range@Length[lst]];
Flatten[r, 1]

gives

{{"a"}, 
 {"a", "b"}, 
 {"a", "b", "c"}, 
 {"a", "b", "c", "d"}, 
 {"b"},
 {"b", "c"},
 {"b", "c", "d"},
 {"c"}, 
 {"c", "d"}, 
 {"d"}}
葵雨 2025-01-02 13:25:56

你可以这样做:

a = {"a", "b", "c", "d"};
b = List[StringJoin[Riffle[#, " "]]] & /@
  Flatten[Table[c = Drop[a, n];
    Table[Take[c, i], {i, Length[c]}],
    {n, 0, Length[a]}], 1]

输出将如下所示:

{{"a"}, {"a b"}, {"a b c"}, {"a b c d"}, {"b"}, {"b c"}, {"b c d"}, {"c"}, {"c d"}, {"d"}}

You can do it like this:

a = {"a", "b", "c", "d"};
b = List[StringJoin[Riffle[#, " "]]] & /@
  Flatten[Table[c = Drop[a, n];
    Table[Take[c, i], {i, Length[c]}],
    {n, 0, Length[a]}], 1]

the output will look like this:

{{"a"}, {"a b"}, {"a b c"}, {"a b c d"}, {"b"}, {"b c"}, {"b c d"}, {"c"}, {"c d"}, {"d"}}
玩世 2025-01-02 13:25:55

我想我最喜欢这个:

set = {"a", "b", "c", "d"};

ReplaceList[set, {___, x__, ___} :> {x}]

通过字符串连接:

ReplaceList[set, {___, x__, ___} :> "" <> Riffle[{x}, " "]]

以类似的方式,特定于字符串:

StringCases["abcd", __, Overlaps -> All]

因为纳赛尔说我在作弊,所以这里有一个更手动的方法,在大型集合上也具有更高的效率:

ClearAll[f, f2]
f[i_][x_] := NestList[i, x, Length@x - 1]
f2[set_]  := Join @@ ( f[Most] /@ f[Rest][set] )

f2[{"a", "b", "c", "d"}]

I think I like this best of all:

set = {"a", "b", "c", "d"};

ReplaceList[set, {___, x__, ___} :> {x}]

With the string joining:

ReplaceList[set, {___, x__, ___} :> "" <> Riffle[{x}, " "]]

In a similar vein, specific to strings:

StringCases["abcd", __, Overlaps -> All]

Since Nasser says I am cheating, here is a more manual approach that also has greater efficiency on large sets:

ClearAll[f, f2]
f[i_][x_] := NestList[i, x, Length@x - 1]
f2[set_]  := Join @@ ( f[Most] /@ f[Rest][set] )

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