将键与字典中的值匹配

时间:2018-09-25 20:20:00

标签: python key-value

每个键都与值进行比较(您可能会说在键和值之间有拼写检查。如果只有两个单词不匹配,请打印键

input = {“ their”:“ thuor”,“ diksha”,“ dijmno”} 输出= [“他们”]

def find_correct(words_dict):
    count=0
    final_list=[]
    for key,value in words_dict.items():
        for i in range(len(value)): # this may need adjusting for different length words
            if(value[i]!=key[i]):
                count+=1 
            if(count<=2):
                final_list.append(key)

    return final_list

print(find_correct({"their":"thuor","diksha":"dijmno"}))

1 个答案:

答案 0 :(得分:0)

这可以通过列表理解和集合来完成

using static SpotifyAPI.Web.Enums.Scope;

static AutorizationCodeAuth auth;

//...
auth = new AutorizationCodeAuth {
    ClientId = Spotify.ClientId,
    RedirectUri = Spotify.RedirectUrl,
    Scope = PlaylistModifyPublic | PlaylistModifyPrivate | PlaylistReadPrivate | Streaming | UserReadPrivate | UserReadEmail | UserLibraryRead | UserLibraryModify | UserFollowModify | UserFollowRead | UserReadBirthdate | UserTopRead | PlaylistReadCollaborative | UserReadRecentlyPlayed | UserReadPlaybackState | UserModifyPlaybackState,
    ShowDialog = true
};
auth.OnResponseReceivedEvent += Auth_OnResponseReceivedEvent;
auth.StartHttpServer(Spotify.WebPort);
auth.DoAuth();

private static void Auth_OnResponseReceivedEvent(AutorizationCodeAuthResponse response) {
    Token token = auth.ExchangeAuthCode(response.Code, Spotify.ClientSecret);
    SpotifyWebAPI api = new SpotifyWebAPI {
        AccessToken = token.AccessToken,
        TokenType = token.TokenType
    };
    PrintUsefulData(api);
}

private static async void PrintUsefulData(SpotifyWebAPI api) {
    PrivateProfile profile = await api.GetPrivateProfileAsync();
    string name = string.IsNullOrEmpty(profile.DisplayName) ? profile.Id : profile.DisplayName;
    Console.WriteLine($"Hello there, {name}!"); //name is always empty
}