你好,
我有许多长度不同的列表,我想将列表中的所有项目合并到一个列表中。
例如:
list1 = {"a", "b", "c"};
list2 = {"d", "e"};<BR />list3 = {"f", "g", "h", "i"};<BR />.... the number of lists ranges from 7 to 10.
想要创造
list = {"a", "b", "c", "d", "e"};
我一直在创建最终列表
Insert Into(list1, list2);
Insert Into(list1, list3);
.... and so on. <BR />(I don't mind the list items are added to list1)
有没有更有效的方法来做到这一点? Insert Into() 似乎不接受超过 1 个附加参数。
谢谢。
如果您乐意更新 list1,这就是您能做的最好的事情。
如果您想要一个新列表,而保持旧列表不变,那么最好的选择是
lnew = l1 || l2 || l3;
// vs
lnew = insert( l1, l2 ); lnew = insert( lnew, l3 );
取决于列表的长度,但 ||最终在下图中领先。
所有三个都在相同速度的 2 倍以内。 我喜欢 insertInto() 因为它避免了复制 list1。 我喜欢||连接运算符,因为很清楚它在做什么。
InsertInto wins if you don't mind that it does what its name says.如果您不介意 InsertInto 的功能如其名称所示,那么 InsertInto 会获胜。