How to print cloned list once and load them in dictionary in Python?
<ol>
<li><p>my input file: </p>

<pre><code>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`
</code></pre></li>
<li><p>Expected output:</p>

<pre><code>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
</code></pre></li>
<li><p>my code:</p>

<pre><code> 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]
</code></pre></li>
<li><p>I opened file and read them line by line. Then I extracted those numbers and text using regular expression and stored in list with indexes. Now I want only lists which are unique and not cloned. Then load them to dictionary. Can somebody help me?</p></li>
</ol>

<p>In order to prevent cloning, you may use a <a href="https://docs.python.org/3.6/tutorial/datastructures.html#sets" rel="nofollow"><code>set()</code></a> like so:</p>

<pre><code>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]
</code></pre>

<hr>

<p>I also suggest organizing your code a little. You can just create 1 regex for clarity like so:</p>

<pre><code>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
</code></pre>

python list python-2.7 fileparsing