Thursday, 15 September 2011

Print all Unique Values in a Python Dictionary -


I am struggling with a small problem in Python (my program is currently version 3.2.3).

I have a dictionary that looks like this (this is just an example, actually taken from any other post):

  [["abc" {"Pqr": "music"}, {"pqr": "music"}, {"pqr"}, {"pcc": "movies"}, {"apc": "game"}, {"ABC": "music"}, {"xyz" : "Movies"}, {"(" Pqr ":" sports "}, {" pqr ":" news "}, {" pqr ":" sports "}]   

I Simply want to print a list of unique () values, eliminating duplicates at the end of this list, I would like to print the number of unique values ​​in the dictionary :

  Movies Sports Music News 4   

Any help was appreciated. Here are some other posts I had found that were related to some extent, But I do not have enough python to implement this specific problem.

here Use as it contains only unique items. >

  • {= "abc": "movies"}, { {"Pcc": "music" "," {"pqr": "music"), {"abc": "sports"}, {"abc": "music"}, {"xyz" : "Movies"}, {"pqr": "sports"}, {"pqr": "news"}, {"pqr": "sports"}]> gt; & Gt; & Gt; S = set (for val in DIC in val in dic.values ​​()) gt; & Gt; & Gt; S set (['movies', 'news', 'music', 'sports'])

    To get the expected output to the loop on this set:

      for x in s: print x print lane (lens) # length of set after loop for print ... movies news music play 4   

    this line S = set (dic.values ​​()) is equal to : .values ​​(): s.add (val)

  • No comments:

    Post a Comment