Tcl 比较具有不同数量参数的多个列表的最佳方法是什么

Tcl 比较具有不同数量参数的多个列表的最佳方法是什么,tcl,Tcl,我有4张单子 list1 = "hello" "hi" "bye" "lol" "rofl" "p1" "p2" "etc" list2 = "hello" "hi" "bye" "lol" "lmao" "c1" "apple" "mango" list3 = "hello" "hi" "bye" "lol" "rofl" "p1" "p2" "etc" "mango" "chair" "table" list4 = "hello" "hi" "bye" "lol" "rofl" "p1" "

我有4张单子

list1 = "hello" "hi" "bye" "lol" "rofl" "p1" "p2" "etc"
list2 = "hello" "hi" "bye" "lol" "lmao" "c1" "apple" "mango"
list3 = "hello" "hi" "bye" "lol" "rofl" "p1" "p2" "etc" "mango" "chair" "table"
list4 = "hello" "hi" "bye" "lol" "rofl" "p1" "p2" "etc" "bus" "mango" "apple" "etc"
我想对四个列表进行比较,需要存储所有列表中匹配的参数、3个列表、2个列表和不匹配的参数

当列表中可能有n个参数且每个列表中的n个参数可能不同时,我可以使用什么样的最佳方法来比较多个列表


目前我正在tcl中编码。但在PHP中,任何算法对我来说都很好,解决方案是:

<?php

// define lists (note the array_flip() calls)
$list = array();
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "lmao", "c1", "apple", "mango"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "mango", "chair", "table"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "bus", "mango", "apple", "etc"));

// create a union of all lists where each member is present only once
$keys = array();
for ($idx = 0; $idx<count($list); $idx++) {
    $keys = $keys+$list[$idx];
}

// iterate over the meta-list
foreach (array_keys($keys) as $key) {
    $found = array();
    for ($idx = 0; $idx<count($list); $idx++) {
        if (isset($list[$idx][$key]))
            $found[] = $idx;
    }
    printf('%s found in lists %s'.PHP_EOL, $key, join(', ', $found));
}


?>
。请注意,有一些针对PHP的优化可以降低代码复杂性

首先,我们将所有关键字列表放入一个大列表中。我们使用array_flip将关键字作为键,而不是值

然后,通过简单地添加数组replicate keys overwrite来创建所有关键字的列表,这是本例中的预期行为

然后我们迭代所有关键字的列表。每个关键字都会被计数,并显示它所在的列表数组


在PHP中,解决方案是:

<?php

// define lists (note the array_flip() calls)
$list = array();
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "lmao", "c1", "apple", "mango"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "mango", "chair", "table"));
$list[] = array_flip(array("hello", "hi", "bye", "lol", "rofl", "p1", "p2", "etc", "bus", "mango", "apple", "etc"));

// create a union of all lists where each member is present only once
$keys = array();
for ($idx = 0; $idx<count($list); $idx++) {
    $keys = $keys+$list[$idx];
}

// iterate over the meta-list
foreach (array_keys($keys) as $key) {
    $found = array();
    for ($idx = 0; $idx<count($list); $idx++) {
        if (isset($list[$idx][$key]))
            $found[] = $idx;
    }
    printf('%s found in lists %s'.PHP_EOL, $key, join(', ', $found));
}


?>
。请注意,有一些针对PHP的优化可以降低代码复杂性

首先,我们将所有关键字列表放入一个大列表中。我们使用array_flip将关键字作为键,而不是值

然后,通过简单地添加数组replicate keys overwrite来创建所有关键字的列表,这是本例中的预期行为

然后我们迭代所有关键字的列表。每个关键字都会被计数,并显示它所在的列表数组


创建关键字为单词、值为整数的字典。通过每个列表,如果单词不存在,则插入值1,如果存在,则增加其值


要查找一个单词出现在多少列表中,只需询问字典,并将该单词作为关键字。

创建关键字为单词、值为整数的字典。通过每个列表,如果单词不存在,则插入值1,如果存在,则增加其值

要查找一个单词出现在多少列表中,只需以该单词为关键字查询字典。

以下是一些tcl:

set list1 {hello hi bye lol rofl p1 p2 etc}
set list2 {hello hi bye lol lmao c1 apple mango}
set list3 {hello hi bye lol rofl p1 p2 etc mango chair table}
set list4 {hello hi bye lol rofl p1 p2 etc bus mango apple etc}

foreach var {list1 list2 list3 list4} {
    foreach elem [set $var] {
        lappend in($elem) $var
    }
}
foreach {key lists} [array get in] {
    lappend partition([llength $lists]) $key
}
现在:

以下是一些tcl:

set list1 {hello hi bye lol rofl p1 p2 etc}
set list2 {hello hi bye lol lmao c1 apple mango}
set list3 {hello hi bye lol rofl p1 p2 etc mango chair table}
set list4 {hello hi bye lol rofl p1 p2 etc bus mango apple etc}

foreach var {list1 list2 list3 list4} {
    foreach elem [set $var] {
        lappend in($elem) $var
    }
}
foreach {key lists} [array get in] {
    lappend partition([llength $lists]) $key
}
现在:


假设此列表数组:

set list(1) {hello hi bye lol rofl p1 p2 etc}
set list(2) {hello hi bye lol lmao c1 apple mango}
set list(3) {hello hi bye lol rofl p1 p2 etc mango chair table}
set list(4) {hello hi bye lol rofl p1 p2 etc bus mango apple etc}
方法1

算法:创建所有唯一单词的列表。对于每个这样的单词,记录它是否出现在数组中的每个列表中。返回一个大小相等的列表,其中项目0、2、。。。是一个单词和项目1,3。。。是单词出现的列表编号

方法2

基本相同,但使用tcl8.6lmap命令

方法3

算法:对于每个列表和单词,使用关键字$word将列表编号添加到dictionary res中的项目。注意:此解决方案的缺点是,如果一个单词在同一个列表中出现多次,则列表编号会被重复多次

预打印结果

lsort-unique是因为方法3中的弱点:其他方法不需要它

proc prettyPrint {word in} {
    set in [lsort -unique $in]
    switch [llength $in] {
        0 {}
        1 {
            puts "$word is in list $in"
        }
        2 {
            puts "$word is in list [join $in { and }]"
        }
        3 {
            puts "$word is in lists [join $in {, }]"
        }
        4 {
            puts "$word is in all lists"
        }
    }
}

foreach {w i} $res {
    prettyPrint $w $i
}
如果使用方法3,列表将以不同的顺序打印单词插入顺序

apple is in list 2 and 4
bus is in list 4
bye is in all lists
c1 is in list 2
chair is in list 3
etc is in lists 1, 3, 4
hello is in all lists
hi is in all lists
lmao is in list 2
lol is in all lists
mango is in lists 2, 3, 4
p1 is in lists 1, 3, 4
p2 is in lists 1, 3, 4
rofl is in lists 1, 3, 4
table is in list 3

文档:,,,,替换,,,

假定此列表数组:

set list(1) {hello hi bye lol rofl p1 p2 etc}
set list(2) {hello hi bye lol lmao c1 apple mango}
set list(3) {hello hi bye lol rofl p1 p2 etc mango chair table}
set list(4) {hello hi bye lol rofl p1 p2 etc bus mango apple etc}
方法1

算法:创建所有唯一单词的列表。对于每个这样的单词,记录它是否出现在数组中的每个列表中。返回一个大小相等的列表,其中项目0、2、。。。是一个单词和项目1,3。。。是单词出现的列表编号

方法2

基本相同,但使用tcl8.6lmap命令

方法3

算法:对于每个列表和单词,使用关键字$word将列表编号添加到dictionary res中的项目。注意:此解决方案的缺点是,如果一个单词在同一个列表中出现多次,则列表编号会被重复多次

预打印结果

lsort-unique是因为方法3中的弱点:其他方法不需要它

proc prettyPrint {word in} {
    set in [lsort -unique $in]
    switch [llength $in] {
        0 {}
        1 {
            puts "$word is in list $in"
        }
        2 {
            puts "$word is in list [join $in { and }]"
        }
        3 {
            puts "$word is in lists [join $in {, }]"
        }
        4 {
            puts "$word is in all lists"
        }
    }
}

foreach {w i} $res {
    prettyPrint $w $i
}
如果使用方法3,列表将以不同的顺序打印单词插入顺序

apple is in list 2 and 4
bus is in list 4
bye is in all lists
c1 is in list 2
chair is in list 3
etc is in lists 1, 3, 4
hello is in all lists
hi is in all lists
lmao is in list 2
lol is in all lists
mango is in lists 2, 3, 4
p1 is in lists 1, 3, 4
p2 is in lists 1, 3, 4
rofl is in lists 1, 3, 4
table is in list 3

文档:,我不使用C++。任何算法都可以帮助My不使用C++。任何算法都可以帮助meI认为存在混淆。我不想要出现在所有列表中的元素。我想要每个元素,并想在列表中找到它们有多少个元素名。就像Hello-all、bye-all、apple-only列表2和列表4、mango-list 2和列表3一样,我认为这是一种混乱。我不想要出现在所有列表中的元素。我想要每个元素,并想在列表中找到它们有多少个元素名。像Hello-all、bye-all、apple-only列表2和列表4、mango-list 2和列表3foreach item$list1{dict incr itemcount$item}foreach item$list1{dict incr itemcount$item}很棒。我们可以像苹果公司是listSure的一部分一样打印列表吗。在递增计数行之后,在$elem$var中添加lappend,因此苹果在$inapple中进行了重组,因此我们不再有计数数组。太好了。我们可以像苹果公司是listSure的一部分一样打印列表吗。后
r递增计数行在$elem$var中添加lappend,因此苹果在$inapple中进行了重组,因此我们不再有计数数组。