I am reading a text file which contains some numbers and letters in each row.
The first number of each row is a unique ID, and I want to copy all the same IDs into a separate list.
For example, if my list after reading the file is something like this:
[
['507', 'W', '1000', '1'],
['1', 'M', '6', '2'],
['1', 'W', '1400', '3'],
['1', 'M', '8', '8'],
['1', 'T', '101', '10'],
['507', 'M', '4', '12'],
['1', 'W', '1700', '15'],
['1', 'M', '7', '16'],
['507', 'M', '8', '20'],
...
]
The expected output should be the following:
[
['507', 'W', '1000', '1','507', 'M', '4', '12','507', 'M', '8', '20'],
['1', 'M', '6', '2','1', 'M', '8', '8','1', 'T', '101', '10','1', 'W', '1700', '15','1', 'M', '7', '16']
...
]
and so on for all other unique IDs in file.
All the rows starting with "507" should be stored in a different list and the rows starting with "1" stored in another and so forth.
My current code:
import operator
fileName = '/home/salman/Desktop/input.txt'
lineList = []
first_number = []
common_number = []
with open(fileName) as f:
for line in f:
lineList = f.readlines()
lineList.append(line)
lineList = [line.rstrip('\n') for line in open(fileName)]
first_number = [i.split()[0] for i in lineList]
print("Rows in list:" + str(lineList))
print("First number in list : " + str(first_number))
common_number = list(set(first_number))
print("Common Numbers in first number list : "+ str(common_number))
print("Repeated value and their index's are :")