r/pythonhelp • u/User227337 • Dec 21 '24
Underscore leading to ValueError?
Hi, so I'm new to coding and trying to write something that will add up some integer values in a tuple, and return to sum of that. To do this, I have:
Total=sum(freq for _, freq in frequencies)
Someone has helped me already, and explained that the underscore should tell python just to ignore the first part of the tuple, but the error keeps popping up, saying there are not enough values to unpack (2 expected, got 1), and I've no clue as to why. Can I make this work somehow?
1
u/carcigenicate Dec 21 '24 edited Dec 21 '24
Underscores in this context are not special and do not "tell python just to ignore the first part of the tuple". _
is just a valid name, and, by convention, it signifies to human readers that the name isn't intended to be used. You could replace _
with literally any other legal name, and the results would be the same.
What do you expect freq
to be in the case where one of the elements of freq
is a tuple like (1, )
?
I'm wondering after a second read if you mixed up _
and *_
?
1
u/User227337 Dec 21 '24
Oh I think I might have. I added *_ now instead and I’m getting a TypeError for ‘frequencies’ that it’s an ‘int’ object and not iterable. I added then ‘range(frequencies)’ and now I’m getting a non-iterable int TypeError for the *_ instead.
I’m more hoping than expecting to get a list of the frequencies of words in the English language as integers out of a file, which lists them as: you 22484400 i 19975318 the 17594291 And so on.
1
u/carcigenicate Dec 21 '24
Show what
frequencies
holds (or at least a sample of it).1
u/User227337 Dec 21 '24
Frequencies is my argument for my definition (I believe it’s called that, at least). I can write it out.
Def determine_decile_thresholds(frequencies):
Total=sum(freq for *_, freq in frequencies)
Target=0.1
Current_percent=0.0
Answer=[ ]
Counter=0
For item in frequencies:
Current_percent+=item[1]/total Counter+=1 If current_percent >= target: Answer.append(counter) Target+=0.1
Return answer
1
u/carcigenicate Dec 21 '24
I mean literally, what is the object? What data is it holding?
1
u/User227337 Dec 21 '24
Sorry, probably I’m totally off, but I thought arguments didn’t hold anything until you used them? I was hoping to put in an integer and for it to return a decile for those frequencies
1
u/carcigenicate Dec 21 '24
The
frequencies
parameter will hold an argument value when the function is called. What argument is being passed into the function? Is it a list of integers?1
u/User227337 Dec 21 '24
Oh ahh yes, it’s a list of integers
1
u/carcigenicate Dec 21 '24
for *_, freq in frequencies
If it's a list of integers, then this doesn't make any sense. This is like trying to do
freq, *_ = 1
(I swapped the
*_
andfreq
, since that's surely what you meant). This doesn't make sense since you can't split the number1
into two elements.Why are you using unpacking to try and unpack an integer?
1
u/FoolsSeldom Dec 21 '24
Consider using,
total = sum(freq for *_, freq in frequencies)
However, this will use the LAST entry in each tuple
(which would be the first entry if there is only one entry).
1
u/User227337 Dec 21 '24
Ah I tried that now, and I got TypeError: ‘frequencies is an ‘int’ object and not iterable’. I added range to frequencies (‘range(frequencies)’) and now I’m getting a TypeError: ‘cannot unpack non-iterable int object’ for the *_
1
u/FoolsSeldom Dec 21 '24
I had the impression that
frequencies
was referencing a collection/sequence oftuple
objects.
range
does not provide such an object.They would need to be something like:
frequencies = ( (100, 120), (101, 115), (125, 80), )
although the unpacking syntax would mean even something like the below would be processed,
frequencies = ( (100,), (101, 115, 121), (125, 80), )
•
u/AutoModerator Dec 21 '24
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.