I would like to join two lines in file, based on whether or not they start with the same element.
I could turn the first element of each line into a list, and use the elements in this list to search each line, but that hardly seems the most efficient way?
I have the following file
1,AF534061.1,T,A
1,K02718.1,T,A
16,AF534061.1,G,-
16,K02718.1,G,-
17,AF534061.1,T,-
17,K02718.1,T,-
18,AF534061.1,A,-
18,K02718.1,A,-
19,AF534061.1,T,-
19,K02718.1,T,-
20,AF534061.1,A,-
20,K02718.1,A,-
21,AF534061.1,A,-
21,K02718.1,A,-
24,AF534061.1,C,T
I would like to join lines if the first item is shared between the lines. So I would like to get the following output
1,AF534061.1,T,A,1,K02718.1,T,A
16,AF534061.1,G,-,16,K02718.1,G,-
17,AF534061.1,T,-,17,K02718.1,T,-
18,AF534061.1,A,-,18,K02718.1,A,-
19,AF534061.1,T,-,19,K02718.1,T,-
20,AF534061.1,A,-,20,K02718.1,A,-
21,AF534061.1,A,-,21,K02718.1,A,-
24,AF534061.1,C,T
In this example, it looks like I might just be able to join every-other line, but I want (need) to make the code more general!
I do not think this is hard, but I cannot seem to figure it out!
thanks for the help
The Python standard library is full of tools. For this job, use itertools.groupby.