Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 unique (single) value in dict (or list)
#1
Given any random dictionary (or a list doesnt matter), how to ask the dictionary if there is an unique value (so which only occurs once).

{
"1": "a",
"2": "b",
"1": "c",
"1": "c",
"1": "33",
"1": "a"
}

--> No unique value is found ... or technically 2 (b and 33) are found, but I only want 1.
so not desired result ... therefor errorhandling or so.

{
"1": "a",
"2": "b",
"1": "c",
"1": "c",
"1": "c",
"1": "a"
}

--> Unique value is found (b) ... do something.

What would your approach be?
Set returns all unique values but I'm not after that.
Quote
#2
What have you tried?
Quote
#3
Still unsure about the approach ndc85430, but thinking about something like this.



mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "c"], [1, "a"] ]

test = [a[1] for a in mylist]
# ['a', 'b', 'c', 'c', '33', 'a']

from collections import Counter
collection_counter = Counter(test)
for m in mylist:
    print "element {} num of times: {}".format(m[0], collection_counter.get(m[1]))

num_occurance = 0
newlist = ""
for key in collection_counter: 
    if collection_counter[key] == 1: 
        num_occurance = num_occurance + 1
        newlist = key
print num_occurance

if num_occurance == 1:
    print "unique value found: {}".format(newlist)
    print [x for x in mylist if x[1] == newlist]
else:
    print "bummer, no uniques found"
        
    
mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "c"], [1, "a"] ]
#[[2, 'b']]
#(annoying nested list ..grrr)

mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "33"], [1, "a"] ]
# bummer, no uniques found 
#(annoying nested list ..grrr)
Quote
#4
Like this?
items1 = {1:1, 2:2, 3:3}
items2 = {1:1, 2:1, 3:2, 4:2, 5:3}
items3 = {1:1, 2:1, 3:3, 4:5, 5:4}

def find_unique(d):
    value_list = list(d.values())
    value_set = set(value_list)
    if len(value_set) == len(value_list):
        return None # All values are unique

    unique = None
    for value in value_set:
        if value_list.count(value) == 1:
            if unique is None:
                unique = value
            else:
                return None # Already found a unique value
    return unique

print(items1, '  Unique =', find_unique(items1))
print(items2, '  Unique =', find_unique(items2))
print(items3, '  Unique =', find_unique(items3))
Output:
{1: 1, 2: 2, 3: 3} Unique = None {1: 1, 2: 1, 3: 2, 4: 2, 5: 3} Unique = 3 {1: 1, 2: 1, 3: 3, 4: 5, 5: 4} Unique = None
3Pinter likes this post
Quote
#5
yes. that looks like what I want to achieve.

your approach looks more logical than mine, thanks!
Quote
#6
on second thoughts: how to return the dictionary item (key+val)?

I think that dictionary isn't the logical approach for this?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 127 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  how to merge a list of *.jpg and *.docx into one single PDF file ? smallabc 0 937 Feb-23-2020, 05:16 PM
Last Post: smallabc
  List help to split into single entries paul41 3 262 Nov-25-2019, 08:09 AM
Last Post: perfringo
  Python list - group by dict key karthidec 2 376 Nov-25-2019, 06:58 AM
Last Post: buran
  convert list of dict to dict of array of list with pop up kk230689 1 307 Sep-13-2019, 07:29 AM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 490 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Counting number of occurrences of a single digit in a list python_newbie09 12 711 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Split the list and obtain a single value Gururaj 1 438 Jul-12-2019, 12:01 AM
Last Post: scidam
  How can I change value of dict in list? Mike Ru 1 366 May-13-2019, 09:06 PM
Last Post: ichabod801
  merging sublist into single list in python abhishek8singhai 8 1,002 Mar-22-2019, 11:46 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)