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
#11
Quote:in case you haven't noticed, answering question is a voluntary act
Yes, it is. People will be more willing to volunteer their time to answer your question if your post does a good job describing the problem you are trying to solve. Put forth little effort to be helpful and others will respond in kind. You leave them no choice. Aside from me that is. An unanswered question is like an itch I can't scratch. That should be enough impetus to write better questions. If you write better questions it is more likely that Larz or ndc85430 or stevendprano will answer it. Write crummy questions and you get me as the default.

So what happened to the ones? Now we are counting 2's and 3's? If you want a list of counts from the counter dictionary you can dot this:
from collections import Counter

items = [2,2,3,3]
c1 = Counter(items)
x = c1.values()
y = list(x)
print(x, y)
dict_values([2, 2]) [2, 2]
But why do you want to know that? What are those disembodied numbers? You have two of something, but two what? If you want to know the counts in order, most to least you can do this.
from collections import Counter
items = [2,2,3,3,3]
c1 = Counter(items)
print(c1.most_common())
Output:
[(3, 3), (2, 2)]
Now you know you have the most 3's, and there are 3 of them. You also have 2 2's. Picking numbers that don't match the count would make for a better example.
from collections import Counter
items = [2, 3, 3, 3, 3, 4, 4]
c1 = Counter(items)
print(c1.most_common())
Output:
[(3, 4), (4, 2), (2, 1)]
ndc85430 likes this post
Reply
#12
The thing is that even when i ask a question that other people understand, you find a way to bitch about it, and if by a miracle you like the question that i ask you find a way to not take into account the code i have written in the question. So you know what, i don't want to deal with you anymore
Reply
#13
(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).

It's not a generator, nor is it a sequence (it doesn't support sequence operations like subscripting). It is a "dictionary values view object" (try saying that five times fast!). View objects:
  • provide a live read-only proxy to the Counter;
  • (usually) with a set-like interface;
  • and are iterable.

The values view is not set-like, but the other two views (dict.keys() and dict.items()) are.

In practice, we can treat views as iterables: we can iterate over them. One difference between a view and a generator is that you don't have to call the view method twice to iterate over it twice:

Output:
>>> view = {'spam': 1, 'eggs': 2}.values() >>> list(view) # iterate over it once [1, 2] >>> list(view) # iterate again [1, 2]
With a generator you have to call the generator again to refresh the state and start it over.
(Mar-28-2022, 09:03 PM)CompleteNewb Wrote: in case you haven't noticed, answering question is a voluntary act

Yes it is. Do you want answers or not? If you don't, why are you bothering to ask the question? And if you do, you shouldn't bite the hand that feeds you. We're not being difficult to be obnoxious. We're trying to understand your problem so we can help.

It's not up to us to go the extra mile to try to work out what you want, we're not being paid. We're donating our time and effort for the good of the community, and to pay back those who helped us when we were newbies. Be respectful of our time and effort, if only because that will maximise the chances of you getting the help you need.

Help us to help you. The better the questions you ask, the more likely we are to give effective answers.

Or you can be snarky, and get ignored, or worse, some vindictive smart arse will give you bad answers.

(Once, many years ago, I saw somebody -- not me, I promise -- give a really obnoxious help-vampire a solution that was obfuscated and destructive, so that if they actually ran the code -- which of course they didn't, help-vampires rarely do -- it would have erased the files in their home directory. Ouch. That's not nice.)
(Mar-28-2022, 10:13 PM)CompleteNewb Wrote: The thing is that even when i ask a question that other people understand, you find a way to bitch about it

Who are these people who are understanding this question? Only two people have even attempted to help you in this thread, me and deanhystad, and you've just told one of them to bugger off. Are you sure that was wise?

If you think that there are other people who understand what you are attempting to do in your code, you could wait for them to answer. Or you could remember that we're not mind-readers, and help us to help you.

My time is not unlimited. I will make one more effort to re-read your posts in this thread and try to decipher what you are trying to do, after which the ball is in your court.
Its still not clear what you want, at one point you said that all of the values are always 1, and you want to get x = 1, but then in another response you want to get x = [2,2].

So I'm deeply confused, but I'm going to try to take a Hail Mary shot and hope for the best. Does this help?

Output:
>>> from collections import Counter >>> c = Counter([1, 2, 3, 1, 1, 2, 1]) >>> c Counter({1: 4, 2: 2, 3: 1}) >>> values = c.values() >>> values dict_values([4, 2, 1]) >>> values = list(c.values()) >>> values[0] 4
If that doesn't help, I am totally out of ideas.

And I have absolutely no clue at all how this relates to your comment that "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".
Reply
#14
I've read through this thread too and can't make sense of what is needed, or why. The reason is the lack of meaning conveyed in the code. When you write a program, you do so to solve a problem. The context in which that problem arises (aka its "domain") has with it words and phrases that describe things.

If I'm writing a program to process files from some experiment and compute various statistical quantities, I might have names like files_to_process, total, mean, standard_deviation, etc.

Again, the names in your program say nothing. Not only would that help us, but it'd help you later on down the line if you need to come back to this program.

Further, we know nothing about the problem you're trying to solve. I doubt you decided to just write a program that does some stuff with a bunch of numbers. There's a reason you're doing this and you'd get better answers if you shared that with us.
Reply
#15
Reading through this one last time I think I might know the question.
Quote:How do I get the values from a dict_values object?
As stevendprano pointed out, dict.values() returns a view object. You can read about them here:

https://docs.python.org/3.2/library/stdt...ew-objects

As pointed out in his post, a view object has several interesting attributes. The most interesting to me is that it is dynamic. If you change the dictionary after making the view, the view reflects the changes. So a view is not a collection, but rather a "view" into another collection.
x = {1:1, 2:4, 3:9}
y = x.values()
print(y)
x[2] = 42
print(y)
Output:
dict_values([1, 4, 9]) dict_values([1, 42, 9])
According to the documentation a view object can be iterated over. You can use a view object in a for loop, in a comprehension, and pass it as an argument to list(). So if you want to treat a views object like it was a list, the easiest way is use list() to make a list.
x = {1:1, 2:4, 3:9}
y = list(x.values())
print(y, y[0])
Output:
[1, 4, 9] 1
I still don't understand how the values will be used, but I can leave that question unanswered without too much discomfort. The pairs, triples and quadruples are all red herring. Now that noob knows how to get the values from the dictionary view he'll figure out how to use them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,316 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  How to iterate dict_values data type nisusavi 2 7,550 Apr-17-2020, 08:06 PM
Last Post: nisusavi
  accessing local variable outside class priyanka08 3 2,191 Sep-24-2019, 10:00 AM
Last Post: buran
  Global accessing of parameter to class Mahamutha 3 3,552 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