# [Tutor] how to unique the string

lina lina.lastname at gmail.com
Sun Oct 23 10:33:52 CEST 2011

```On Sun, Oct 23, 2011 at 12:50 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> lina wrote:
>>
>> Hi,
>>
>> I googled for a while, but failed to find the perfect answer,
>>
>> for a string
>>
>> ['85CUR', '85CUR']
>
>
> That's not a string, it is a list.
>
>
>> how can I unique it as:
>>
>> ['85CUR']
>
> Your question is unclear. If you have this:
>
> ['85CUR', '99bcd', '85CUR', '85CUR']
>
> what do you expect to get?
>
>
> # keep only the very first item
> ['85CUR']
> # keep the first copy of each string, in order
> ['85CUR', '99bcd']
> # keep the last copy of each string, in order
> ['99bcd', '85CUR']
> # ignore duplicates only when next to each other
> ['85CUR', '99bcd', '85CUR']
>
>
> Does the order of the result matter?
>
> If order matters, and you want to keep the first copy of each string:
>
> unique = []
> for item in items:
>    if item not in unique:
>        unique.append(item)
>
>
> If order doesn't matter, then use this:
>
> unique = list(set(items))

Thanks, this one unique=list(set(item)) works well.

I have a further question:

#!/usr/bin/python3

import os.path

mapping={}

DICTIONARYFILE="dictionary.pdb"
TOBETRANSLATEDFILEEXT=".out"
OUTPUTFILEEXT=".txt"

def generate_dict(dictionarysourcefile):
parts=line.strip().split()
mapping[parts[2]]=parts[0]

def translate_process(dictionary,tobetranslatedfile):
results=[]
unique={}
tobetranslatedparts=line.strip().split()
results.append(dictionary[tobetranslatedparts[2]])
for residue in results:
if residue not in unique:
unique[residue]=1
else:
unique[residue]+=1
for residue, numbers in unique.items():
print(residue,numbers)
with open(base+OUTPUTFILEEXT,"w") as f:
f.write(str(unique))      ########### How can I output the
results the same as the print one. Thanks.

if __name__=="__main__":
generate_dict(DICTIONARYFILE)
for infilename in os.listdir("."):
base, ext =  os.path.splitext(infilename)
if ext == TOBETRANSLATEDFILEEXT:
translate_process(mapping, infilename)

Welcome anyone help me transform the code to another form.

>
>
>
> --
> Steven
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
```