Python Forum
accessing value in dict_values class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accessing value in dict_values class
#1
i have this program snippet:

samelist = []
list1 = []
lst = [1,1]
alllist = {
4:[1,1,1,1],
5:[1,1,1],
6:[1,1]
}

for i in lst:
    c1 = Counter(i)
    for j,k in alllists.items():        
        c2 = Counter(k)   
        diff = c2 - c1
        
        print('i',i)
        print('k',k)
        print('diff.keys',diff.keys())
        print('c1.values',type(c1.values()))
        print('diff.values',diff.values())
        print('c1.values',c1.values())
        
        x = c1.values()
        y = diff.values()
        if diff == c2:
            pass
        elif diff == Counter():
            samelist.append(j)
            
        elif diff.keys() == c1.keys() and y == x + 1:
            list1.append(j)
          
the error message i get is :

Error:
Traceback (most recent call last): File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode exec(code, self.locals) File "/home/fook/Documents/pygame/Poker Practice/test2.py", line 37, in <module> elif diff.keys() == c1.keys() and y == x[0] + 1: TypeError: 'dict_values' object is not subscriptable
now i understand the error, because the return value of dict_value is : dict_values(['1']) and it's a <class 'dict_values'>
but i need to get the value in order to compare it, because what i'm trying to do is to compare pairs and triple and quadruples so that if i have a pair i get returned the triple equivalent because the pair is contained in the triple and the triple is only missing one element, but not the quadruple because it is missing two elements. So


Output:
samelist = [6] list1 = [5]
Reply
#2
I have read your post three times and I still don't understand what you are trying to do Huh

(Mar-27-2022, 09:40 PM)CompleteNewb Wrote: i need to get the value in order to compare it

Get the value of what?

It doesn't help that the variables have such generic names that I can't work out what they mean. You have "lst", "samelist", "list1", and "alllist" (which is not even a list!).

I know that this isn't answering your question, but I see further on another problem with your code. You set x and y to dictionary views x = c1.values() and similar for y, but then later on you test y == x + 1 which cannot possibly work.

Perhaps if you explain from the beginning what you are trying to do, without skipping over any steps or assuming we know your intention, we can guide you better.

But first watch this video, and remember that the computer is just like that. You must be precise and exact in your instructions.
ndc85430 likes this post
Reply
#3
Also mystified, but taking a shot:
from collections import Counter

whatever = [1, 1, 3, 4]
possibilities = {
    4:[1, 1, 1, 1, 2, 2],
    5:[1, 1, 1, 3, 5],
    6:[1, 1, 4, 7]
}

diffs = {key:(Counter(value) - Counter(whatever)) for key, value in possibilities.items()}
counts = {key:sum(value.values()) for key, value in diffs.items()}
for key in possibilities:
    print(f"{key}: {str(diffs[key].most_common()):<20} {counts[key]}")
Output:
4: [(1, 2), (2, 2)] 4 5: [(1, 1), (5, 1)] 2 6: [(7, 1)] 1
Ones weren't interesting, so I added some extra numbers. The output says that for whatever has 2 fewer 1 and 2 fewer 2 than possibilites[4], for a grand total of 4.

Is that somewhat the kind of thing you are looking for?
stevendaprano likes this post
Reply
#4
(Mar-27-2022, 11:08 PM)stevendaprano Wrote: I have read your post three times and I still don't understand what you are trying to do Huh

(Mar-27-2022, 09:40 PM)CompleteNewb Wrote: i need to get the value in order to compare it

Get the value of what?

It doesn't help that the variables have such generic names that I can't work out what they mean. You have "lst", "samelist", "list1", and "alllist" (which is not even a list!).

I know that this isn't answering your question, but I see further on another problem with your code. You set x and y to dictionary views x = c1.values() and similar for y, but then later on you test y == x + 1 which cannot possibly work.

Perhaps if you explain from the beginning what you are trying to do, without skipping over any steps or assuming we know your intention, we can guide you better.

But first watch this video, and remember that the computer is just like that. You must be precise and exact in your instructions.

okay, i'll try to be clearer

in my post i said:

the return value of dict_value is :
"dict_values(['1']) and it's a <class 'dict_values'>, but i need to get the value in order to compare it"

so when i do

x = c1.values()

i get x = dict_values(['1'])

but i want to get

x = 1 so i can do y == x + 1

How do i do that?
Reply
#5
Still not understanding. If c1 is a Counter, x = c1.values() will be a sequence of numbers (actually a generator).
from collections import Counter
values = (1, 2, 3, 4, 3, 2, 1, 1, 1)
counts = Counter(values)
print("keys", counts.keys())
print("values", counts.values())
print(sum(counts.values()), len(values), len(counts))
Output:
keys dict_keys([1, 2, 3, 4]) values dict_values([4, 2, 2, 1]) 9 9 4
So what is x + 1 supposed to do? Does it add 1 to the total count? Does it add 1 to the counts[4], counts[1]?

You should describe what you are trying to achieve, not how you are trying to do it. What is your problem? Why do you think a counting dictionary is a good choice for solving this problem? How do you plan to use the counting dictionary?
ndc85430 likes this post
Reply
#6
(Mar-28-2022, 07:10 PM)deanhystad Wrote: Still not understanding. If c1 is a Counter, x = c1.values() will be a sequence of numbers (actually a generator).
from collections import Counter
values = (1, 2, 3, 4, 3, 2, 1, 1, 1)
counts = Counter(values)
print("keys", counts.keys())
print("values", counts.values())
print(sum(counts.values()), len(values), len(counts))
Output:
keys dict_keys([1, 2, 3, 4]) values dict_values([4, 2, 2, 1]) 9 9 4
So what is x + 1 supposed to do? Does it add 1 to the total count? Does it add 1 to the counts[4], counts[1]?

You should describe what you are trying to achieve, not how you are trying to do it. What is your problem? Why do you think a counting dictionary is a good choice for solving this problem? How do you plan to use the counting dictionary?

my lists all contains ones so i'll only have one key:value pair
Reply
#7
If your list only contains 1's why are you using a counting dictionary? The purpose of a counting dictionary is to group items by value and count them. If you only have one value there is no reason to use it. Just use len().

Still no information about what you are trying to do? Ask better questions you will get better answers.
ndc85430 likes this post
Reply
#8
(Mar-28-2022, 08:00 PM)deanhystad Wrote: If your list only contains 1's why are you using a counting dictionary? The purpose of a counting dictionary is to group items by value and count them. If you only have one value there is no reason to use it. Just use len().

Still no information about what you are trying to do? Ask better questions you will get better answers.

so what's up with that?

"because what i'm trying to do is to compare pairs and triple and quadruples so that if i have a pair, i get returned the triple equivalent because the pair is contained in the triple and the triple is only missing one element, but not the quadruple because it is missing two elements. So


samelist = [6]
list1 = [5]

i want to know, why i get this:
[python]
list = [2,2,3,3]
c1 = Counter(list)
x  = c1.values()
x = dict_values([2, 2])
and what can i do to get this:
x = [2,2]
Reply
#9
Repeating the same is not explaining. Pairs of what? Triples of what? What is the list of ones supposed to represent? Is [1, 1] a pair, [1, 1, 1] a triple? Why not 2 or 3 instead of lists of ones? Why do you have lists at all?

And your original post had all this stuff:
samelist = []
list1 = []
lst = [1,1]
alllist = {
4:[1,1,1,1],
5:[1,1,1],
6:[1,1]
}
What is all that about? You provide no description, and as mentioned earlier, those variable names do little to aid understanding. alllist is a dictionary? A dictionary of what? What do the keys mean? What do the values mean?

You need to remember when posting a question that nobody here has any idea what you are talking about. Your post probably makes perfect sense to you because you know what problem you are trying to solve and you have been working on it and you have access to all the code and all the history. I read your post and that is all I know about it. Only the code an the text in that one post. It is up to you to provide enough context for me understand your question. If you fail doing this it results in a lot of back and forth before you finally get the answer you are looking for.

A side benefit of writing a better question is it takes real work. The work you put into describing your problem to a degree that others can understand often results in you not having to post a question at all. "How can I explain what my list of 1's mean? Hey, the list only has ones in it! I don't have to use a counting dictionary when all the items are the same. Yippee! I just saved myself 2 days!"
ndc85430 likes this post
Reply
#10
in case you haven't noticed, answering question is a voluntary act
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,315 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  How to iterate dict_values data type nisusavi 2 7,547 Apr-17-2020, 08:06 PM
Last Post: nisusavi
  accessing local variable outside class priyanka08 3 2,189 Sep-24-2019, 10:00 AM
Last Post: buran
  Global accessing of parameter to class Mahamutha 3 3,549 Aug-23-2017, 07:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020