【问题标题】:How to print cloned list once and load them in dictionary in Python?如何打印一次克隆列表并将它们加载到 Python 中的字典中?
【发布时间】:2025-12-05 20:25:01
【问题描述】:
  1. 我的输入文件:

    cs 124456 powerful
    cs 124456 powerful
    me     125454 easy
    me    125455 easy
    me    125455 easy
    ec 125555 done
    ec 127678 fine 
    ec 127678 fine 
    ci 127678 fine 
    ci 127678 fine 
    eee   125678 good
    eee   125678 good
    eee   125678 good
    eee   125678 bad`
    
  2. 预期输出:

    no.name reg  perform 
    1.cs 124456 powerful
    2.me  125454 easy
    3.me  125455 easy
    4.ec 125555 done
    5.ec 127678 fine 
    6.ci 127678 fine  
    7.eee   125678 good
    8.eee   125678 bad
    
  3. 我的代码:

     import os
     os.chdir("d:/filer")
     import re          
     def first(line):
         f=re.findall("[a-z]+",line,flags=0)
         return f
     def num(line):
         n=re.findall("\d{6}",line,flags=0)
         return n
     with open("once.txt","r") as sa:
         for line in sa.readlines():
              home=first(line)
              number=num(line)
              x=home[0]
              y=number[0]
              z=home[1]
              if x!=0 and y!=0 and z!=0:    
              print [x,y,z]
    
  4. 我打开文件并逐行读取它们。然后我使用正则表达式提取这些数字和文本并存储在带有索引的列表中。现在我只想要唯一且未克隆的列表。然后将它们加载到字典中。有人可以帮帮我吗?

【问题讨论】:

    标签: python list python-2.7 fileparsing


    【解决方案1】:

    为了防止克隆,您可以像这样使用set()

    results = set()  # Construct a set
     with open("once.txt","r") as sa:
         for line in sa.readlines():
              home=first(line)
              number=num(line)
              x=home[0]
              y=number[0]
              z=home[1]
              if x!=0 and y!=0 and z!=0:
                if (x,y,z) not in results:  # Check if the set already contains the result
                    results.add((x,y,z))  # If it doesn't, add to the set and print.
                    print [x,y,z]
    

    我还建议稍微组织一下您的代码。为了清楚起见,您可以像这样创建 1 个正则表达式:

    results = set()  # Construct a set
    with open("once.txt","r") as sa:
        count = 0
        for line in sa:  # No need for readlines()
            match = re.match(r"(\w+)\s+(\d+)\s+(\w+)")
    
            if match is None:
                continue
    
            result = match.groups()
            if result not in results:  # Check if the set already contains the result
                count += 1
                results.add(result)  # If it doesn't, add to the set and print.
                print count, result
    

    【讨论】:

      最近更新 更多