检查 A 列和 B 列值是否双向的最简单方法是什么?
要检查的输出:
Mike John
John Mike
Pamela Barbara
Barbara Pamela
Mike Paul
Roger Paul
所需输出
Mike <-> John
Pamela <-> Barbara
Mike -> Paul
Roger -> Paul
附言。
首先,它可能就像查看 A 和 B 列中的所有可能值,然后对每一行进行字数统计
Mike John 1 1
Pamela Barbara 1 1
Mike Paul 1 0
Roger Paul 1 0
然后将更改输出到所需的更改。
答案1
如果顺序不是问题,您可以使用此哈希解决方案awk
。
BEGIN { d = "|" }
{
if(names[$2 d $1] > 0)
++names[$2 d $1]
else
++names[$1 d $2]
}
END {
for(n in names) {
split(n, a, d)
if(names[n] >= 2)
print a[1] " <-> " a[2]
if(names[n] == 1)
print a[1] " -> " a[2]
}
}
哈希值被初始化为由管道(变量d
)分隔的两个名称的串联;如果这些名称再次以相反的顺序出现,则哈希中的特定元素将增加到 2。
输出:
Pamela <-> Barbara
Mike -> Paul
Roger -> Paul
Mike <-> John
答案2
好吧,既然你已经标记了这个Python,尽管你的头衔是:
#!/usr/bin/env python
input_file = 'input.dat'
out_data = []
relationships = []
in_fh = open(input_file, 'r')
for line in in_fh:
x, y = line.split()
# If the reverse mapping was already seen...
if (y, x) in out_data:
# ... then update the reverse mapping to point both ways
idx = out_data.index( (y, x) )
relationships[idx] = '<->'
# Otherwise, we have no reverse mapping yet...
else:
# if we haven't seen the forward mapping yet either...
if (x, y) not in out_data:
# ...then record the forward mapping
out_data.append( (x, y) )
relationships.append('->')
in_fh.close()
# Print the final mappings
for (x, y), arrow in zip(out_data, relationships):
print "%s %s %s" % (x, arrow, y)
答案3
这是我的失败的bash 脚本尝试避免使用数组或哈希。 (bothways.txt
文件包含示例数据)。
#!/bin/bash
sourcefile=bothways.txt
reversefile=bothways2.txt
dupefile=bothways3.txt
# Create reverse file by swapping the columns
sed -r 's/(\w+)(\s+)(\w+)/\3\2\1/g' <$sourcefile >$reversefile
# Create dupe file by concatenating source and reverse files
# and displaying the duplicate lines
cat $sourcefile $reversefile | sort | uniq -d >$dupefile
while read line
do
if grep "$line" $dupefile >/dev/null
then
arrow='<->';
else
arrow='->';
fi
echo $line | sed -r "s/(\w+)\s+(\w+)/\1 $arrow \2/g"
done < $sourcefile
输出:
Mike <-> John
John <-> Mike
Pamela <-> Barbara
Barbara <-> Pamela
Mike -> Paul
Roger -> Paul
输出的问题在于它包含冗余行。 (尽管关系是正确的。)
答案4
这里 bash 中的算法相同
#!/bin/bash
while read n1 n2; do
n1=${n1//[^[:alpha:]]}
n2=${n2//[^[:alpha:]]}
n=___${n2}_$n1
k=${!n}
if ((k>0)); then
((___${n2}_$n1++))
else
((___${n1}_$n2++))
fi
done
for n in ${!___*}; do
k=${!n}
n=${n:3}
if ((k>=2)); then
echo "${n/_/ <-> }"
elif ((k==1)); then
echo "${n/_/ -> }"
fi
done