Need take a value list from a dictionary, and subtract that value from a total number of values
I realize that title may be confusing, so allow me to explain.
I take input from a list that looks like L = [21.123, 22.123, 23.123, 21.123]
I remove the decimals, and sort the list high to low. I also change it to a dictionary with occurrences, which looks like newlist = 23: 1, 22: 1, 21: 2
What I need to do is to make a list of keys and values, which I can do. This gives me two lists, of [23, 22, 21]
and [1, 1, 2]
one for values and one for occurrences. I need to turn my occurrence list into the number of occurrences that are the same as, or lower than it's corresponding key.
I would like my list to look like [23, 22, 21]
(which is easy to do) and [4, 3, 2]
because 4 of the times are 23 seconds or less, 3 of the times are 22 seconds or less, and 2 of the times are 21 seconds or less.
I'm pretty sure I need a for loop to iterate through every frequency value, and change that value to be the total number of times entered into the list, and subtract any value more than it. I'm not sure how to go about this, so any help would be greatly appreciated.
python python-3.x list dictionary for-loop
|
show 1 more comment
I realize that title may be confusing, so allow me to explain.
I take input from a list that looks like L = [21.123, 22.123, 23.123, 21.123]
I remove the decimals, and sort the list high to low. I also change it to a dictionary with occurrences, which looks like newlist = 23: 1, 22: 1, 21: 2
What I need to do is to make a list of keys and values, which I can do. This gives me two lists, of [23, 22, 21]
and [1, 1, 2]
one for values and one for occurrences. I need to turn my occurrence list into the number of occurrences that are the same as, or lower than it's corresponding key.
I would like my list to look like [23, 22, 21]
(which is easy to do) and [4, 3, 2]
because 4 of the times are 23 seconds or less, 3 of the times are 22 seconds or less, and 2 of the times are 21 seconds or less.
I'm pretty sure I need a for loop to iterate through every frequency value, and change that value to be the total number of times entered into the list, and subtract any value more than it. I'm not sure how to go about this, so any help would be greatly appreciated.
python python-3.x list dictionary for-loop
1
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
1
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
For your fist problem, try Counter from the standard library collections modulecollections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.
– Håken Lid
Nov 11 '18 at 16:05
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
It is hard to tell what[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?
– Ayxan
Nov 11 '18 at 16:11
|
show 1 more comment
I realize that title may be confusing, so allow me to explain.
I take input from a list that looks like L = [21.123, 22.123, 23.123, 21.123]
I remove the decimals, and sort the list high to low. I also change it to a dictionary with occurrences, which looks like newlist = 23: 1, 22: 1, 21: 2
What I need to do is to make a list of keys and values, which I can do. This gives me two lists, of [23, 22, 21]
and [1, 1, 2]
one for values and one for occurrences. I need to turn my occurrence list into the number of occurrences that are the same as, or lower than it's corresponding key.
I would like my list to look like [23, 22, 21]
(which is easy to do) and [4, 3, 2]
because 4 of the times are 23 seconds or less, 3 of the times are 22 seconds or less, and 2 of the times are 21 seconds or less.
I'm pretty sure I need a for loop to iterate through every frequency value, and change that value to be the total number of times entered into the list, and subtract any value more than it. I'm not sure how to go about this, so any help would be greatly appreciated.
python python-3.x list dictionary for-loop
I realize that title may be confusing, so allow me to explain.
I take input from a list that looks like L = [21.123, 22.123, 23.123, 21.123]
I remove the decimals, and sort the list high to low. I also change it to a dictionary with occurrences, which looks like newlist = 23: 1, 22: 1, 21: 2
What I need to do is to make a list of keys and values, which I can do. This gives me two lists, of [23, 22, 21]
and [1, 1, 2]
one for values and one for occurrences. I need to turn my occurrence list into the number of occurrences that are the same as, or lower than it's corresponding key.
I would like my list to look like [23, 22, 21]
(which is easy to do) and [4, 3, 2]
because 4 of the times are 23 seconds or less, 3 of the times are 22 seconds or less, and 2 of the times are 21 seconds or less.
I'm pretty sure I need a for loop to iterate through every frequency value, and change that value to be the total number of times entered into the list, and subtract any value more than it. I'm not sure how to go about this, so any help would be greatly appreciated.
python python-3.x list dictionary for-loop
python python-3.x list dictionary for-loop
edited Nov 11 '18 at 16:54
jpp
99.4k2161110
99.4k2161110
asked Nov 11 '18 at 15:59
Joel BanksJoel Banks
767
767
1
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
1
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
For your fist problem, try Counter from the standard library collections modulecollections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.
– Håken Lid
Nov 11 '18 at 16:05
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
It is hard to tell what[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?
– Ayxan
Nov 11 '18 at 16:11
|
show 1 more comment
1
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
1
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
For your fist problem, try Counter from the standard library collections modulecollections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.
– Håken Lid
Nov 11 '18 at 16:05
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
It is hard to tell what[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?
– Ayxan
Nov 11 '18 at 16:11
1
1
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
1
1
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
For your fist problem, try Counter from the standard library collections module
collections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.– Håken Lid
Nov 11 '18 at 16:05
For your fist problem, try Counter from the standard library collections module
collections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.– Håken Lid
Nov 11 '18 at 16:05
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
It is hard to tell what
[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?– Ayxan
Nov 11 '18 at 16:11
It is hard to tell what
[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?– Ayxan
Nov 11 '18 at 16:11
|
show 1 more comment
4 Answers
4
active
oldest
votes
You want a dictionary where, for each item in your data, the key is the rounded value (int(item)
) and the value is the number of of items that are smaller than or equal to this rounded value.
A dictionary comprehension (combined with a list comprehension) can do this:
data = [21.123, 22.123, 23.123, 21.123]
aggregate =
item: len([n for n in data if int(n) <= item])
for item in set(map(int, data))
print(aggregate) # -> 21: 2, 22: 3, 23: 4
which is the single-statement form of writing such a loop:
aggregate =
for item in set(map(int, data)):
aggregate[item] = len([n for n in data if int(n) <= item])
}
Using set()
makes the list unique. This way the loop only runs as often as necessary.
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something likesum(int(n) <= item for n in data)
.
– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
add a comment |
Here's a functional solution. The marginally tricky part is the backwards cumulative sum, which is possible feeding a reversed tuple to itertools.accumulate
and then reversing the result.
from collections import Counter
from itertools import accumulate
from operator import itemgetter
L = [21.123, 22.123, 23.123, 21.123]
c = Counter(map(int, L)) # Counter(21: 2, 22: 1, 23: 1)
counter = sorted(c.items(), reverse=True) # [(23, 1), (22, 1), (21, 2)]
keys, counts = zip(*counter) # ((23, 22, 21), (1, 1, 2))
cumsum = list(accumulate(counts[::-1]))[::-1] # [4, 3, 2]
Your desired result is stored in keys
and cumsum
:
print(keys)
(23, 22, 21)
print(cumsum)
[4, 3, 2]
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note thatkeys
andcumsum
hold the two lists he wants.
– soundstripe
Nov 11 '18 at 16:48
add a comment |
Assuming you get the counts correctly from [21.123, 22.123, 23.123, 21.123]
, a simple nested loop with a running sum can do the rest:
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k in newlist:
for v in newlist:
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
You could also use itertools.product()
to condense the double loops into one:
from itertools import product
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k, v in product(newlist, repeat=2):
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
The above stores the counts in a collections.Counter()
, you can get [4, 3, 2]
by calling list(counts.values())
.
add a comment |
I found my own solution which seems relatively simple. Code looks like
counter = 0
print(valuelist)
for i in valuelist:
print(int(solves - counter))
counter = counter + i
redonevalues.append(solves - counter + 1)
It takes my values, goes to the first one, adds the occurrences to counter, subtracts counter from solves, and adds 1 to even it out
it's completely unclear to me what this code does. What'sredonevalues
? What'ssolves
? What'svaluelist
? Why doessolves - counter + 1
result in anything of significance?
– Tomalak
Nov 11 '18 at 16:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250515%2fneed-take-a-value-list-from-a-dictionary-and-subtract-that-value-from-a-total-n%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want a dictionary where, for each item in your data, the key is the rounded value (int(item)
) and the value is the number of of items that are smaller than or equal to this rounded value.
A dictionary comprehension (combined with a list comprehension) can do this:
data = [21.123, 22.123, 23.123, 21.123]
aggregate =
item: len([n for n in data if int(n) <= item])
for item in set(map(int, data))
print(aggregate) # -> 21: 2, 22: 3, 23: 4
which is the single-statement form of writing such a loop:
aggregate =
for item in set(map(int, data)):
aggregate[item] = len([n for n in data if int(n) <= item])
}
Using set()
makes the list unique. This way the loop only runs as often as necessary.
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something likesum(int(n) <= item for n in data)
.
– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
add a comment |
You want a dictionary where, for each item in your data, the key is the rounded value (int(item)
) and the value is the number of of items that are smaller than or equal to this rounded value.
A dictionary comprehension (combined with a list comprehension) can do this:
data = [21.123, 22.123, 23.123, 21.123]
aggregate =
item: len([n for n in data if int(n) <= item])
for item in set(map(int, data))
print(aggregate) # -> 21: 2, 22: 3, 23: 4
which is the single-statement form of writing such a loop:
aggregate =
for item in set(map(int, data)):
aggregate[item] = len([n for n in data if int(n) <= item])
}
Using set()
makes the list unique. This way the loop only runs as often as necessary.
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something likesum(int(n) <= item for n in data)
.
– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
add a comment |
You want a dictionary where, for each item in your data, the key is the rounded value (int(item)
) and the value is the number of of items that are smaller than or equal to this rounded value.
A dictionary comprehension (combined with a list comprehension) can do this:
data = [21.123, 22.123, 23.123, 21.123]
aggregate =
item: len([n for n in data if int(n) <= item])
for item in set(map(int, data))
print(aggregate) # -> 21: 2, 22: 3, 23: 4
which is the single-statement form of writing such a loop:
aggregate =
for item in set(map(int, data)):
aggregate[item] = len([n for n in data if int(n) <= item])
}
Using set()
makes the list unique. This way the loop only runs as often as necessary.
You want a dictionary where, for each item in your data, the key is the rounded value (int(item)
) and the value is the number of of items that are smaller than or equal to this rounded value.
A dictionary comprehension (combined with a list comprehension) can do this:
data = [21.123, 22.123, 23.123, 21.123]
aggregate =
item: len([n for n in data if int(n) <= item])
for item in set(map(int, data))
print(aggregate) # -> 21: 2, 22: 3, 23: 4
which is the single-statement form of writing such a loop:
aggregate =
for item in set(map(int, data)):
aggregate[item] = len([n for n in data if int(n) <= item])
}
Using set()
makes the list unique. This way the loop only runs as often as necessary.
answered Nov 11 '18 at 16:18
TomalakTomalak
258k51428545
258k51428545
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something likesum(int(n) <= item for n in data)
.
– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
add a comment |
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something likesum(int(n) <= item for n in data)
.
– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
1
1
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something like sum(int(n) <= item for n in data)
.– jpp
Nov 11 '18 at 16:26
len([n for n in data if int(n) <= item])
in a loop seems overkill. It works, of course, but you are performing more summations than necessary via a cumulative summation of counts. Regardless, cleaner IMO is to at least avoid the list creation, something like sum(int(n) <= item for n in data)
.– jpp
Nov 11 '18 at 16:26
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
Fair point, I agree. I thought the idea "count the number of items that are smaller than X" might be more intuitive to the OP in form of a list comprehension.
– Tomalak
Nov 11 '18 at 16:40
add a comment |
Here's a functional solution. The marginally tricky part is the backwards cumulative sum, which is possible feeding a reversed tuple to itertools.accumulate
and then reversing the result.
from collections import Counter
from itertools import accumulate
from operator import itemgetter
L = [21.123, 22.123, 23.123, 21.123]
c = Counter(map(int, L)) # Counter(21: 2, 22: 1, 23: 1)
counter = sorted(c.items(), reverse=True) # [(23, 1), (22, 1), (21, 2)]
keys, counts = zip(*counter) # ((23, 22, 21), (1, 1, 2))
cumsum = list(accumulate(counts[::-1]))[::-1] # [4, 3, 2]
Your desired result is stored in keys
and cumsum
:
print(keys)
(23, 22, 21)
print(cumsum)
[4, 3, 2]
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note thatkeys
andcumsum
hold the two lists he wants.
– soundstripe
Nov 11 '18 at 16:48
add a comment |
Here's a functional solution. The marginally tricky part is the backwards cumulative sum, which is possible feeding a reversed tuple to itertools.accumulate
and then reversing the result.
from collections import Counter
from itertools import accumulate
from operator import itemgetter
L = [21.123, 22.123, 23.123, 21.123]
c = Counter(map(int, L)) # Counter(21: 2, 22: 1, 23: 1)
counter = sorted(c.items(), reverse=True) # [(23, 1), (22, 1), (21, 2)]
keys, counts = zip(*counter) # ((23, 22, 21), (1, 1, 2))
cumsum = list(accumulate(counts[::-1]))[::-1] # [4, 3, 2]
Your desired result is stored in keys
and cumsum
:
print(keys)
(23, 22, 21)
print(cumsum)
[4, 3, 2]
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note thatkeys
andcumsum
hold the two lists he wants.
– soundstripe
Nov 11 '18 at 16:48
add a comment |
Here's a functional solution. The marginally tricky part is the backwards cumulative sum, which is possible feeding a reversed tuple to itertools.accumulate
and then reversing the result.
from collections import Counter
from itertools import accumulate
from operator import itemgetter
L = [21.123, 22.123, 23.123, 21.123]
c = Counter(map(int, L)) # Counter(21: 2, 22: 1, 23: 1)
counter = sorted(c.items(), reverse=True) # [(23, 1), (22, 1), (21, 2)]
keys, counts = zip(*counter) # ((23, 22, 21), (1, 1, 2))
cumsum = list(accumulate(counts[::-1]))[::-1] # [4, 3, 2]
Your desired result is stored in keys
and cumsum
:
print(keys)
(23, 22, 21)
print(cumsum)
[4, 3, 2]
Here's a functional solution. The marginally tricky part is the backwards cumulative sum, which is possible feeding a reversed tuple to itertools.accumulate
and then reversing the result.
from collections import Counter
from itertools import accumulate
from operator import itemgetter
L = [21.123, 22.123, 23.123, 21.123]
c = Counter(map(int, L)) # Counter(21: 2, 22: 1, 23: 1)
counter = sorted(c.items(), reverse=True) # [(23, 1), (22, 1), (21, 2)]
keys, counts = zip(*counter) # ((23, 22, 21), (1, 1, 2))
cumsum = list(accumulate(counts[::-1]))[::-1] # [4, 3, 2]
Your desired result is stored in keys
and cumsum
:
print(keys)
(23, 22, 21)
print(cumsum)
[4, 3, 2]
edited Nov 11 '18 at 16:53
answered Nov 11 '18 at 16:15
jppjpp
99.4k2161110
99.4k2161110
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note thatkeys
andcumsum
hold the two lists he wants.
– soundstripe
Nov 11 '18 at 16:48
add a comment |
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note thatkeys
andcumsum
hold the two lists he wants.
– soundstripe
Nov 11 '18 at 16:48
1
1
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note that
keys
and cumsum
hold the two lists he wants.– soundstripe
Nov 11 '18 at 16:48
I like this one. All the reversing is a little confusing but better variable names could help. Also may want to note that
keys
and cumsum
hold the two lists he wants.– soundstripe
Nov 11 '18 at 16:48
add a comment |
Assuming you get the counts correctly from [21.123, 22.123, 23.123, 21.123]
, a simple nested loop with a running sum can do the rest:
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k in newlist:
for v in newlist:
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
You could also use itertools.product()
to condense the double loops into one:
from itertools import product
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k, v in product(newlist, repeat=2):
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
The above stores the counts in a collections.Counter()
, you can get [4, 3, 2]
by calling list(counts.values())
.
add a comment |
Assuming you get the counts correctly from [21.123, 22.123, 23.123, 21.123]
, a simple nested loop with a running sum can do the rest:
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k in newlist:
for v in newlist:
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
You could also use itertools.product()
to condense the double loops into one:
from itertools import product
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k, v in product(newlist, repeat=2):
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
The above stores the counts in a collections.Counter()
, you can get [4, 3, 2]
by calling list(counts.values())
.
add a comment |
Assuming you get the counts correctly from [21.123, 22.123, 23.123, 21.123]
, a simple nested loop with a running sum can do the rest:
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k in newlist:
for v in newlist:
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
You could also use itertools.product()
to condense the double loops into one:
from itertools import product
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k, v in product(newlist, repeat=2):
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
The above stores the counts in a collections.Counter()
, you can get [4, 3, 2]
by calling list(counts.values())
.
Assuming you get the counts correctly from [21.123, 22.123, 23.123, 21.123]
, a simple nested loop with a running sum can do the rest:
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k in newlist:
for v in newlist:
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
You could also use itertools.product()
to condense the double loops into one:
from itertools import product
from collections import Counter
newlist = 23: 1, 22: 1, 21: 2
counts = Counter()
for k, v in product(newlist, repeat=2):
if v <= k:
counts[k] += newlist[v]
print(counts)
# Counter(23: 4, 22: 3, 21: 2)
The above stores the counts in a collections.Counter()
, you can get [4, 3, 2]
by calling list(counts.values())
.
edited Nov 11 '18 at 16:44
answered Nov 11 '18 at 16:20
RoadRunnerRoadRunner
11.2k31340
11.2k31340
add a comment |
add a comment |
I found my own solution which seems relatively simple. Code looks like
counter = 0
print(valuelist)
for i in valuelist:
print(int(solves - counter))
counter = counter + i
redonevalues.append(solves - counter + 1)
It takes my values, goes to the first one, adds the occurrences to counter, subtracts counter from solves, and adds 1 to even it out
it's completely unclear to me what this code does. What'sredonevalues
? What'ssolves
? What'svaluelist
? Why doessolves - counter + 1
result in anything of significance?
– Tomalak
Nov 11 '18 at 16:54
add a comment |
I found my own solution which seems relatively simple. Code looks like
counter = 0
print(valuelist)
for i in valuelist:
print(int(solves - counter))
counter = counter + i
redonevalues.append(solves - counter + 1)
It takes my values, goes to the first one, adds the occurrences to counter, subtracts counter from solves, and adds 1 to even it out
it's completely unclear to me what this code does. What'sredonevalues
? What'ssolves
? What'svaluelist
? Why doessolves - counter + 1
result in anything of significance?
– Tomalak
Nov 11 '18 at 16:54
add a comment |
I found my own solution which seems relatively simple. Code looks like
counter = 0
print(valuelist)
for i in valuelist:
print(int(solves - counter))
counter = counter + i
redonevalues.append(solves - counter + 1)
It takes my values, goes to the first one, adds the occurrences to counter, subtracts counter from solves, and adds 1 to even it out
I found my own solution which seems relatively simple. Code looks like
counter = 0
print(valuelist)
for i in valuelist:
print(int(solves - counter))
counter = counter + i
redonevalues.append(solves - counter + 1)
It takes my values, goes to the first one, adds the occurrences to counter, subtracts counter from solves, and adds 1 to even it out
answered Nov 11 '18 at 16:39
Joel BanksJoel Banks
767
767
it's completely unclear to me what this code does. What'sredonevalues
? What'ssolves
? What'svaluelist
? Why doessolves - counter + 1
result in anything of significance?
– Tomalak
Nov 11 '18 at 16:54
add a comment |
it's completely unclear to me what this code does. What'sredonevalues
? What'ssolves
? What'svaluelist
? Why doessolves - counter + 1
result in anything of significance?
– Tomalak
Nov 11 '18 at 16:54
it's completely unclear to me what this code does. What's
redonevalues
? What's solves
? What's valuelist
? Why does solves - counter + 1
result in anything of significance?– Tomalak
Nov 11 '18 at 16:54
it's completely unclear to me what this code does. What's
redonevalues
? What's solves
? What's valuelist
? Why does solves - counter + 1
result in anything of significance?– Tomalak
Nov 11 '18 at 16:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250515%2fneed-take-a-value-list-from-a-dictionary-and-subtract-that-value-from-a-total-n%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What have you tried, and what exactly is the problem with it?
– jonrsharpe
Nov 11 '18 at 16:01
1
Hint: use the cummulative sum.
– Willem Van Onsem
Nov 11 '18 at 16:02
For your fist problem, try Counter from the standard library collections module
collections.Counter(map(int, [21.123, 22.123, 23.123, 21.123]))
. When asking questions here, avoid asking multiple questions at the same time, and include your best effort code. Even if your code doesn't work, it helps us understand exactly what you are trying to do.– Håken Lid
Nov 11 '18 at 16:05
I'm trying to figure out how to calculate how many occurrences are more than the corresponding key, so if i'm on the value from valuelist that corresponds to the number of times 22 occurs, I don't know how to calculate the number of times numbers 22- min value occur, and exclude the 1 time 23 occurs. Then subtract the amount more than corresponding key from total number of times, which is 4 (amount of times total) minus 1 (one time > 22 seconds)
– Joel Banks
Nov 11 '18 at 16:06
It is hard to tell what
[4, 3, 2]
is honestly. What do you exactly mean by "4 of the times are 23 seconds or less"?– Ayxan
Nov 11 '18 at 16:11