对 Mathematica 列表中每个元素的第二个组件进行运算

发布于 2024-12-22 17:14:02 字数 582 浏览 1 评论 0原文

我可以使用以下方法对列表中每个元素的第一个组件进行一些操作(在本例中为 ToString):

{ToString@#[[1]], Rest@#}~Flatten~1 & /@ {{1, 2}, {3, 4, 5}}

但是,我有几个问题:

  • 它不适用于 {ToString@#[[1]], Rest@#}~压平~1 & /@{{1, 2}, 2, {3, 4, 5}} 原因显而易见。如何让它也能工作 这个案子?预期输出为 {{"1", 2}, 2, {"3", 4, 5}}
  • 如何轻松对第二个(或第三个等)组件执行此操作?即我希望输出为 {{0}, {1, "2"}, {3, "4", 5}, {6, "7", 9, 10}}
  • 是有一种方法可以使用模式/规则(比如 /.{#[[1]]->ToString[#[[1]]]}) 对于这种操作?所以请列出您可能想到的所有解决方案,无论效率如何。

多谢!

I can use the following way to do some operation (in this case ToString) for the first component of each element in a list:

{ToString@#[[1]], Rest@#}~Flatten~1 & /@ {{1, 2}, {3, 4, 5}}

However, I have a few questions:

  • It does not work for {ToString@#[[1]], Rest@#}~Flatten~1 & /@ {{1,
    2}, 2, {3, 4, 5}}
    for obvious reason. How to make it also work in
    this case? The expected output would be {{"1", 2}, 2, {"3", 4, 5}}.
  • How to do this for the second(or third, etc.) component easily? I.e. I want the output to be {{0}, {1, "2"}, {3, "4", 5}, {6, "7", 9, 10}}
  • Is there a way to use pattern/rule (like
    /.{#[[1]]->ToString[#[[1]]]}) for this kind of operation? So please list all solutions you may think of regardless of the efficiency.

Thanks a lot!

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

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

发布评论

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

评论(4

盛夏尉蓝 2024-12-29 17:14:02

直到我编写了它之后,我才意识到这与 Leonid 的核心功能相同。也许这仍然表明这可能比他相当复杂的职能更透明一些。

lst = {{1, 2}, 2, {3, 4, 5}};

Replace[lst, {a_, b__} :> {ToString@a, b}, 1]
{{"1", 2}, 2, {"3", 4, 5}}

然后可以使用 {x:Repeated[_, {4}], a_, b__} :> {x, ToString@a, b}, 1] 用于第五个索引,依此类推。

I didn't realize this was the same as Leonid's core function until I had written it. Still perhaps that says that this may be a little more transparent than his rather elaborate function.

lst = {{1, 2}, 2, {3, 4, 5}};

Replace[lst, {a_, b__} :> {ToString@a, b}, 1]
{{"1", 2}, 2, {"3", 4, 5}}

One can then use {x:Repeated[_, {4}], a_, b__} :> {x, ToString@a, b}, 1] for the fifth index, etc.

音盲 2024-12-29 17:14:02

下面的函数基本上应该做你想做的事:

ClearAll[applyToAll];
applyToAll[f_, list_List, n_Integer] :=
   applyToAll[x_ :> f[x], list, n];
applyToAll[rule : (_Rule | _RuleDelayed), list_List, n_Integer] :=
  Replace[
      list, {left : Repeated[_, {n - 1}], el_, rest___} :> 
        {left, el /. rule, rest}, {1}];

并且可以接受规则。例如:

In[192]:= 
applyToAll[ToString, {{1,2},2,{3,4,5}},1]//InputForm
Out[192]//InputForm=  {{"1", 2}, 2, {"3", 4, 5}}

In[193]:= applyToAll[ToString,{{0},{1,2},{3,4,5},{6,7,9,10}},2]//InputForm
Out[193]//InputForm=  {{0}, {1, "2"}, {3, "4", 5}, {6, "7", 9, 10}}

In[194]:= applyToAll[x_?OddQ:>ToString[x],{{0},{1,2},{3,4,5},{6,7,9,10}},2]//InputForm
Out[194]//InputForm= {{0}, {1, 2}, {3, 4, 5}, {6, "7", 9, 10}}

The following function should basically do what you want:

ClearAll[applyToAll];
applyToAll[f_, list_List, n_Integer] :=
   applyToAll[x_ :> f[x], list, n];
applyToAll[rule : (_Rule | _RuleDelayed), list_List, n_Integer] :=
  Replace[
      list, {left : Repeated[_, {n - 1}], el_, rest___} :> 
        {left, el /. rule, rest}, {1}];

and can accept rules. For example:

In[192]:= 
applyToAll[ToString, {{1,2},2,{3,4,5}},1]//InputForm
Out[192]//InputForm=  {{"1", 2}, 2, {"3", 4, 5}}

In[193]:= applyToAll[ToString,{{0},{1,2},{3,4,5},{6,7,9,10}},2]//InputForm
Out[193]//InputForm=  {{0}, {1, "2"}, {3, "4", 5}, {6, "7", 9, 10}}

In[194]:= applyToAll[x_?OddQ:>ToString[x],{{0},{1,2},{3,4,5},{6,7,9,10}},2]//InputForm
Out[194]//InputForm= {{0}, {1, 2}, {3, 4, 5}, {6, "7", 9, 10}}
别念他 2024-12-29 17:14:02

另一种方便的方法可能是将 ReplacePartRuleDelayed 一起使用,

例如,将每个子列表的第 3 部分(如果存在)转换为字符串:

ReplacePart[#, 3 :>  ToString@#[[3]]] & /@ {{1, 2}, 
   2, {3, 4, 5}, {6, 7, 9, 10}} // InputForm

给出输出:

{{1, 2}, 2, {3, 4, "5"}, {6, 7, "9", 10}}

类似地,要将每个子列表的第 1 部分转换为字符串:

ReplacePart[#, 1 :>  ToString@#[[1]]] & /@ {{1, 2}, 
   2, {3, 4, 5}} // InputForm

给出:

{{"1", 2}, 2, {"3", 4, 5}}

Another convenient method might be to use ReplaceParttogether with RuleDelayed

For example, to turn part 3 of each sublist (if it exists) into a string:

ReplacePart[#, 3 :>  ToString@#[[3]]] & /@ {{1, 2}, 
   2, {3, 4, 5}, {6, 7, 9, 10}} // InputForm

gives as output:

{{1, 2}, 2, {3, 4, "5"}, {6, 7, "9", 10}}

Similarly, to turn part 1 of each sublist into a string:

ReplacePart[#, 1 :>  ToString@#[[1]]] & /@ {{1, 2}, 
   2, {3, 4, 5}} // InputForm

giving:

{{"1", 2}, 2, {"3", 4, 5}}

红焚 2024-12-29 17:14:02

从长远来看,我认为这可能是一种更简单的方法,即使它并不完全符合您的要求:

rep[f_, pos_][x_List] := MapAt[f, x, pos]
rep[__][x_] := x

lst = {{1, 2}, 2, {3, 4, 5}};

rep[ToString, 2] /@ lst
{{1, "2"}, 2, {3, "4", 5}}

您可以根据需要向 rep 的定义添加任意模式和条件。

In the long run I think that this might be an easier approach, even if it is not exactly what you requested:

rep[f_, pos_][x_List] := MapAt[f, x, pos]
rep[__][x_] := x

lst = {{1, 2}, 2, {3, 4, 5}};

rep[ToString, 2] /@ lst
{{1, "2"}, 2, {3, "4", 5}}

You can add arbitrary patterns and conditions to the definition of rep as needed.

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