比如说我的输入文件-file1.tsv有以下两列

type         grocery
fruits       orange
fruits       apple
fruits       kiwi
greens       collard
greens       spinach

期望的结果是
type         grocery
fruits       orange, apple, kiwi
greens       collard, spinach

我可以将第1列中的重复项作为字典读取,但无法用逗号追加未重复的第2列值。有没有用python快速解决这个问题的方法?

最佳答案

如果文件按列1分组:

awk 'p==$1{s=s ", " $2; next} {if(p)print s; p=$1; s=$0} END{print s}' file

关于python - 重复列值及其对应的未重复列的解决方案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22740808/

10-13 07:44
查看更多