50-The first character that appears only once-python

Title: The first character in a string that appears only once.

def first_not_repeat(strs):
    dic = {}
    for s in strs:
        if s not in dic:
            dic[s] = 1
        else:
            dic[s] += 1
    for k,v in dic.items():
        if v==1:
            return k

  Note: Traverse the string twice. The first time the dictionary is used to count the number of occurrences of each character, the second time the key is output with a value of 1 through the traversal.

Guess you like

Origin blog.csdn.net/wh672843916/article/details/105503745