Recursive key value in a file

There is a file that has two columns [can be understood as a key and a value]:

k1 v1
k2 v2
k3 v3
k4 k1
k5 k4

Now I want the following to be output from this file:

k1 v1
k2 v2
k3 v3
k4 k1 v1
k5 k4 k1 v1

For, if some key value is another key, then bring this value to this line. How to approach this

There may be multi-valued keys, as well as k1 v1 and k1 x1. for multi-valued keys, we would like to have a new line with both values:

k1 v1
k1 x1
k2 k1 

Changes in

k1 v1
k1 x1
k2 k1 v1
k2 k1 x1
+4
source share
6 answers

python code:

from collections import OrderedDict
dict = {}
fp = open('test.txt')
for line in fp.readlines():
    line = line.split(" ")
    dict[line[0]] = line[1].replace('\n','')
fp.close() 
dict = OrderedDict(sorted(dict.items(), key=lambda t: t[0]))
for key in dict:
    if dict.has_key(dict[key]):
        print key,dict[key],dict[dict[key]]
    else:
        print key,dict[key]

original file:

k1 v1
k2 v2
k3 v3
k4 k1
k5 k4
k6 k2
k7 v8

result:

k1 v1
k2 v2
k3 v3
k4 k1 v1
k5 k4 k1
k6 k2 v2
k7 v8
+1
source

Sort of

$  awk '($2 in hash){hash[$1]=$2" "hash[$2]; next} {hash[$1]=$2} END{for (i in hash) print i, hash[i]}' input
k1 v1
k2 v2
k3 v3
k4 k1 v1
k5 k4 k1 v1
0
source

, .

$ awk '{d[$1]=$2" "d[$2]; print $1,d[$1]}' file
k1 v1 
k2 v2 
k3 v3 
k4 k1 v1 
k5 k4 k1 v1 

  • d[$1]=$2" "d[$2]

    . . , , .

  • print $1,d[$1]

    .

. , , .

. , .

0
dict={}
x1=fileobject.read()
for line in x1.splitlines():
    if line.split()[1] in dict.keys():
        dict[line.split()[0]]=line.split()[1]+" "+dict[line.split()[1]]
    else:
        dict[line.split()[0]]=line.split()[1]

print dict

, , .

: {'k3': 'v3', 'k2': 'v2', 'k1': 'v1', 'k5': 'k4 k1 v1', 'k4': 'k1 v1'}

0

Awk

awk '{for(i=(b[$2]>0);i<=b[$2];i++){c[$1" "++b[$1]]=$2" "c[$2" "i];print $1,c[$1" "b[$1]]}}' file

input

k1 v1
k1 x1
k1 y1
k2 k1
k2 k4
k3 k2

output

k1 v1
k1 x1
k1 y1
k2 k1 v1
k2 k1 x1
k2 k1 y1
k2 k4
k3 k2 k1 v1
k3 k2 k1 x1
k3 k2 k1 y1
k3 k2 k4
0

:

fooobar.com/questions/1567922/...

node , . , , , k1 k2, k2 k1 , :

$ cat tst.awk
function descend(node,  child, descendants) {
    stack[node]
    child = map[node]
    if (child in map) {
        if (child in stack) {
            descendants = node "*"
        }
        else {
            descendants = child " " descend(child)
        }
    }
    else {
        descendants = child
    }
    delete stack[node]
    return descendants
}
{ keys[++numKeys] = $1; map[$1] = $2 }
END {
    for (keyNr=1; keyNr<=numKeys; keyNr++) {
        key = keys[keyNr]
        print key, descend(key)
    }
}

.

$ awk -f tst.awk file
k1 v1
k2 v2
k3 v3
k4 k1 v1
k5 k4 k1 v1

k1 v1, k1 x1 - , 2D- , , 1D map , - :

{
    if (!seen[$1]++) {
        keys[++numKeys] = $1
    }
    map[$1,++cnt[$1]] = $2
}

descend() []

for (i=1; i<=cnt[node]; i++) {
    child = map[node,i]
    if (child in map) {
        ...
    }
}

:

child = map[node]
if (child in map) {
    ...
}

This may not be perfect because it is untested, but it is the right idea and will not be too complicated for you to debug (i.e. I am not going to!).

0
source

All Articles