将列表拆分为多个子列表,更改特定子列表,然后重新加入所有子列表以形成一个新列表?
Splitting a list into multiple sub-lists, changing a particlular sub-list, and then rejoining all the sub-lists to form into a single new list?
我必须编写代码来执行以下步骤。
- 正在加载文件并将文件的内容保存到列表中。 (该文件包含一列和 1223 行数字,在 Matlab 代码中说
1:1:1223
)
- 自动将列表拆分为多个子列表。 (假设每个子列表有 100 个数字,即
1223/100 = 12.23
(大约 13
个子列表))
- 增加特定子列表中的步长(比如子列表编号
3
内容从 201:1:300
到 201:0.5:300
)。
- 更改特定子列表后,将所有列表合并为一个列表。
我已经完成了第一步,但我正在努力执行第 2、3、4 步。我无法找到在 tcl 中实现的方法(我是 TCL 的新手)。我在 MATLAB 中创建了一个原型(特别是为了在 Whosebug 社区中解释我的需求)。以下是我用 MATLAB 编写的代码。但最终,我必须在 TCL 中实现它。 (使用 MATLAB 中的 cell
概念,整个过程变得更容易)
clc
clear all
close all
Data_Initial = 1:1:1223;
Elements_in_List = 100;
% Dividing the list into Multiple sub-lists
Total_SubLists = floor(length(Data_Initial)/Elements_in_List);
if ((Total_SubLists*Elements_in_List) < length(Data_Initial))
Total_SubLists = Total_SubLists + 1;
end
for i = 1: Total_SubLists
Sublist_Begin_RowID = (i - 1)* Elements_in_List + 1;
Sublist_End_RowID = i * Elements_in_List;
if (i < Total_SubLists)
SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:Sublist_End_RowID);
else
SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:end, :);
end
end
% Changing List NO. 3
K = 3;
A = SubLists{K};
A = A(1):0.5:A(end);
SubLists{K} = A;
% Re-Joining all the list to from a New data
Data_New = [];
for i = 1:length(SubLists)
Data_New = horzcat(Data_New, SubLists{i});
end
谁能帮我完成第 2、3 和 4 步。
Loading a file and saving the contents of a file into a list.
Automatically Splitting the list into multiple sub-lists.
查看之前的回答:Split a list of numbers into smaller list based on a range in TCL
Increase the step-size in a particular sublist
您想查看 Tcl 的 lset
,它允许使用 Tcl 的从零开始的索引表达式访问和操作子列表的结构。
% set superList [list [list 201 1 300] [list 202 1 300] [list 203 1 300]]
{201 1 300} {202 1 300} {203 1 300}
% lset superList 2 1 0.5
{201 1 300} {202 1 300} {203 0.5 300}
After changing a particular sub-list, joining all the list to form a
single list.
实现此目的的一种方法是结合使用 concat
和(解析时)扩展运算符 {*}
:
% concat {*}$superList
201 1 300 202 1 300 203 0.5 300
但这也取决于超级列表中的嵌套级别:参见
我必须编写代码来执行以下步骤。
- 正在加载文件并将文件的内容保存到列表中。 (该文件包含一列和 1223 行数字,在 Matlab 代码中说
1:1:1223
) - 自动将列表拆分为多个子列表。 (假设每个子列表有 100 个数字,即
1223/100 = 12.23
(大约13
个子列表)) - 增加特定子列表中的步长(比如子列表编号
3
内容从201:1:300
到201:0.5:300
)。 - 更改特定子列表后,将所有列表合并为一个列表。
我已经完成了第一步,但我正在努力执行第 2、3、4 步。我无法找到在 tcl 中实现的方法(我是 TCL 的新手)。我在 MATLAB 中创建了一个原型(特别是为了在 Whosebug 社区中解释我的需求)。以下是我用 MATLAB 编写的代码。但最终,我必须在 TCL 中实现它。 (使用 MATLAB 中的 cell
概念,整个过程变得更容易)
clc
clear all
close all
Data_Initial = 1:1:1223;
Elements_in_List = 100;
% Dividing the list into Multiple sub-lists
Total_SubLists = floor(length(Data_Initial)/Elements_in_List);
if ((Total_SubLists*Elements_in_List) < length(Data_Initial))
Total_SubLists = Total_SubLists + 1;
end
for i = 1: Total_SubLists
Sublist_Begin_RowID = (i - 1)* Elements_in_List + 1;
Sublist_End_RowID = i * Elements_in_List;
if (i < Total_SubLists)
SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:Sublist_End_RowID);
else
SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:end, :);
end
end
% Changing List NO. 3
K = 3;
A = SubLists{K};
A = A(1):0.5:A(end);
SubLists{K} = A;
% Re-Joining all the list to from a New data
Data_New = [];
for i = 1:length(SubLists)
Data_New = horzcat(Data_New, SubLists{i});
end
谁能帮我完成第 2、3 和 4 步。
Loading a file and saving the contents of a file into a list.
Automatically Splitting the list into multiple sub-lists.
查看之前的回答:Split a list of numbers into smaller list based on a range in TCL
Increase the step-size in a particular sublist
您想查看 Tcl 的 lset
,它允许使用 Tcl 的从零开始的索引表达式访问和操作子列表的结构。
% set superList [list [list 201 1 300] [list 202 1 300] [list 203 1 300]]
{201 1 300} {202 1 300} {203 1 300}
% lset superList 2 1 0.5
{201 1 300} {202 1 300} {203 0.5 300}
After changing a particular sub-list, joining all the list to form a single list.
实现此目的的一种方法是结合使用 concat
和(解析时)扩展运算符 {*}
:
% concat {*}$superList
201 1 300 202 1 300 203 0.5 300
但这也取决于超级列表中的嵌套级别:参见