简体   繁体   中英

Reading strings from a file to find unique characters

I'm trying to find words with the most unique letters from a list of strings. The problem for me is not finding the unique words for a string as I know how to do that, no—, my problem is going step-by-step in the list of strings to find each words unique characters.

Example: Say that my list of strings is...

[Apple, Banana, Tiki]

and what I want the list to look like is

[Aple, Ban, Tik]

Whenever I tried to go through step by step, I end up having the entire list smashed together instead of comma separated and all my other solutions have yielded nothing. I can't use any packages or the set() function.

def unique_letters(words_list):

    count = 0 
    while(count < len(words_list)):
        for i in lines[count]:
        if i not in temp:
            temp.append(i)
            dupes = ''.join(temp) 
    count += 1
    return dupes

What I end up getting is...

'ApleBanTik' ### when I want ---> [Aple, Ban, Tik]

I've been working on another solution, but I end up getting the same thing. Any suggestions to how I can fix?

You could do this (with list comprehension):

def unique_letters(words_list):
    return [''.join(dict.fromkeys(word)) for word in words_list]

Here is the expanded version:

def unique_letters(words_list):
    result = []
    for word in words_list:
        result.append(''.join(dict.fromkeys(word)))
    return result

When you convert a word into a dictionary, it removes all duplicates. Then, we just convert the dictionary into a string.

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