Tcl dict create 使字典变得奇怪

Tcl dict create makes weird dictionary

set table [dict create cells state bits state]
set data [dict create 1 "s" 1 "f"]

puts $table
puts $data

输出:

cells state bits state

1 f

这很奇怪!为什么 dict create 不在 1 s 1 f 上制作完整的字典? 谢谢!

ps 文档说:

dict create ?key value ...?
Return a new dictionary that contains each of the key/value mappings listed as arguments (keys and values alternating, with each key being followed by its associated value.) https://www.tcl.tk/man/tcl/TclCmd/dict.htm#M6

Post Post 脚本: 我发现放置任何两个相同的键都会做同样的事情,例如:

puts [dict create 2 "s" 2 "f"]

2 f

如果您为同一个键输入不同的值(第一种情况下 key = 1,第二种情况下 key = 2),像 Tcl dict 这样的关联容器将只保留这些值中的一个。

您仍然可以拥有具有多个相等键的数据结构,只是不要使用 dict create,它会强制执行唯一键特征(据我所知,这是使用 [=12= 初始化字典的唯一原因) ]):

% set data {1 s 1 f}
1 s 1 f
% dict get $data 1
f
% set data [dict create 1 s 1 f]
1 f
% dict get $data 1
f

字典处理命令将处理这个结构,就好像它每个键只有一个。字典变异命令不会保留原始结构的多个相等键。