简体   繁体   中英

How to extract common lines from text files based on their associated values?

I have 3 text files as:

List1.txt:

032_M5, 5
035_M9, 5
036_M4, 3
038_M2, 6
041_M1, 6

List2.txt:

032_M5, 6
035_M9, 6
036_M4, 5
038_M2, 5
041_M1, 6

List3.txt:

032_M5, 6
035_M9, 6
036_M4, 4
038_M2, 5
041_M1, 6

where the 1st part (ie string) of lines in all 3 text files are the same, but the 2nd part (ie number) changes.

I want to get three output files from this:

Output1.txt --> All lines where numbers corresponds to a string are all different. Example:

036_M4 3, 5, 4

Output2.txt --> All lines where numbers corresponds to a string are the same. Example:

041_M1, 6

Output3.txt --> All lines where atleast two numbers corresponds to a string are the same (which includes results of Output2.txt also). Example:

032_M5, 6
035_M9, 6
038_M2, 5
041_M1, 6

Then I need the count of lines with number 1, number 2, number 3, number 4, number 5, and number 6 from Output3.txt.

Here is what I tried. It is giving me the wrong output.

from collections import defaultdict
data = defaultdict(list)
for fileName in ["List1.txt","List2.txt", "List3.txt"]:
    with open(fileName,'r') as file1:
        for line in file1:
            col1,value = line.split(",") 
            data[col1].append(int(value))

with open("Output3.txt","w") as output:
    for (col1),values in data.items():
        if len(values) < 3: continue             
        result = max(x for x in values)                     
        output.write(f"{col1}, {result}\n")

Here is an approach that does not utilize any python modules and it entirely depends on native built-in python functions:

with open("List1.txt", "r") as list1, open("List2.txt", "r") as list2, open("List3.txt", "r") as list3:
  # Forming association between keywords and numbers.
  data1 = list1.readlines()
  totalKeys = [elem.split(',')[0] for elem in data1]
  numbers1 = [elem.split(',')[1].strip() for elem in data1]
  numbers2 = [elem.split(',')[1].strip() for elem in list2.readlines()]
  numbers3 = [elem.split(',')[1].strip() for elem in list3.readlines()]
  totalValues = list(zip(numbers1,numbers2,numbers3))
  totalDict = dict(zip(totalKeys,totalValues))

  #Outputs
  output1 = []
  output2 = []
  output3 = []
  for key in totalDict.keys():
    #Output1
    if len(set(totalDict[key])) == 3:
      output1.append([key, totalDict[key]])
    #Output2
    if len(set(totalDict[key])) == 1:
      output2.append([key, totalDict[key][0]])
    #Output3
    if len(set(totalDict[key])) <= 2:
      output3.append([key, max(totalDict[key], key=lambda elem: totalDict[key].count(elem))])

  #Output1
  print('Output1:')
  for elem in output1:
    print(elem[0] + ' ' + ", ".join(elem[1]))
  print()

  #Output2
  print('Output2:')
  for elem in output2:
    print(elem[0] + ' ' + " ".join(elem[1]))
  print()

  #Output3
  print('Output3:')
  for elem in output3:
    print(elem[0] + ' ' + " ".join(elem[1]))

The result of the above will be:

Output1:
036_M4 3, 5, 4

Output2:
041_M1 6

Output3:
032_M5 6
035_M9 6
038_M2 5
041_M1 6

max gives the biggest number in the list, not the most commonly occurring. For that, use statistics.mode

from collections import defaultdict
from statistics import mode

data = defaultdict(list)
for fileName in ["List1.txt","List2.txt", "List3.txt"]:
    with open(fileName,'r') as file1:
        for line in file1:
            col1,value = line.split(",") 
            data[col1].append(int(value))

with open("Output1.txt","w") as output:
    for (col1),values in data.items():
        if len(values) < 3: continue
        if values[0] != values[1] != values[2] and values[0] != values[2]:
            output.write(f"{col1}, {values[0]}, {values[1]}, {values[2]}\n")

with open("Output2.txt","w") as output:
    for (col1),values in data.items():
        if len(values) < 3: continue
        if values[0] == values[1] == values[2]:
            output.write(f"{col1}, {values[0]}\n")

with open("Output3.txt","w") as output:
    for (col1),values in data.items():
        if len(values) < 3: continue
        if len(set(values)) >= 2:
            output.write(f"{col1}, {mode(values)}\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM