ValueError: not enough values to unpack (expected 3, got 2) Python - json

I'm trying to print out a dictionary in the following format. I want to do this because I need to graph some trends in D3 using the JSON format. For this trend, I am counting the number of murders in each state within each decade (1980s to 2010s).
I am able to output the file and everything but since I am trying to create a graph, the format of the data in the JSON file needs to be very specific in terms of labeling each key, value pair in the dictionary in the output.
xl = pd.ExcelFile('Wyoming.xlsx')
df = xl.parse('Sheet1')
year = df['Year']
state = df['State']
freq = dict()
for i in range(0, len(df)):
currYear = year.iloc[i]
if(currYear >= 1980 and currYear < 1989):
currDecade = 1980
elif(currYear >= 1990 and currYear < 2000):
currDecade = 1990
elif(currYear >= 2000 and currYear < 2010):
currDecade = 2000
elif(currYear >= 2010):
currDecade = 2010
currState = state.iloc[i]
if currDecade in freq:
if currState in freq[currDecade]:
freq[currDecade][currState] += 1
else:
key = {currState: 1}
freq[currDecade].update(key)
else:
key = {currDecade:{currState: 1}}
freq.update(key)
#print(freq)
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f}]} for d, s, f in freq.items()]
print(freq1)
I am getting the error "ValueError: not enough values to unpack (expected 3, got 2)"
I expect the output to be as given below.
[{"Decade": "1980", "State": [{"State": "California", "Freq": 29591}, {"State": "Massachusetts", "Freq": 1742}, ...}]

The dict.items() only iterates tuples with two elements: the key and value.
freq1 = []
for decade, states in freq.items():
freq1.append({
'Decade': decade,
'State': []
})
for state, freq in states.items():
freq1['State'].append([{'State': state, 'Freq': freq}])
print(freq1)
I think the code is more readable this way. However if you still prefer the one-line list comprehension solution, here it is:
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f} for s, f in ss.items()]} for d, ss in freq.items()]

The culprit is for d, s, f in freq.items(), since freq.items() returns an iterable over (key, value) pairs in freq. Since you have nested dicts, try this:
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f} for s, f in sdict.items()]}
for d, sdict in freq.items()
]

Related

COUNTIFS: Excel to pandas and remove counted elements

I have a COUNTIFS equation in excel (COUNTIFS($A$2:$A$6, "<=" & $C4))-SUM(D$2:D3) where A2toA6 is my_list. C4 is current 'bin' with the condition and D* are previous summed results from my_list that meet the condition. I am attempting to implement this in Python
I have looked at previous COUNTIF questions but I am struggling to complete the final '-SUM(D$2:D3)' part of the code.
See the COUNTIFS($A$2:$A$6, "<=" & $C4) section below.
'''
my_list=(-1,-0.5, 0, 1, 2)
bins = (-1, 0, 1)
out = []
for iteration, num in enumerate(bins):
n = []
out.append(n)
count = sum(1 for elem in my_list if elem<=(num))
n.append(count)
print(out)
'''
out = [1, [3], [4]]
I need to sum previous elements, that have already been counted, and remove these elements from the next count so that they are not counted twice ( Excel representation -SUM(D$2:D3) ). This is where I need some help! I used enumerate to track iterations. I have tried the code below in the same loop but I can't resolve this and I get errors:
'''
count1 = sum(out[0:i[0]]) for i in (out)
and
count1 = out(n) - out(n-1)
''''
See expected output values in 'out' array for bin conditions below:
I was able to achieve the required output array values by creating an additional if/elif statement to factor out previous array elements and generate a new output array 'out1'. This works but may not be the most efficient way to achieve the end goal:
'''
import numpy as np
my_list=(-1,-0.5, 0, 1, 2)
#bins = np.arange(-1.0, 1.05, 0.05)
bins = (-1, 0, 1)
out = []
out1 = []
for iteration, num in enumerate(bins):
count = sum(1 for elem in my_list if elem<=(num))
out.append(count)
if iteration == 0:
count1 = out[iteration]
out1.append(count1)
elif iteration > 0:
count1 = out[iteration] - out[iteration - 1]
out1.append(count1)
print(out1)
'''
I also tried using the below code as suggested in other answers but this didn't work for me:
'''
-np.diff([out])
print(out)
'''

Trying to find index of minimum value in a list of vars in Octave

I have a list of vars with different values
a = 2
b = 1
c= 12343243
d = 8998
Can find the smallest value
aSmallestVALUE = min([a, b, c, d])
and index
[v,idx]=min([a, b, c, d])
I want to find the index of variable and sort this list from 0 to up
something like the
sorted list = b, a, d, c
Obviously if you want to treat those four variables as a 'list' to be sorted, you need to be working with a 'list' construct, not 4 isolated variables.
L = [2, 1, 12343243, 8998];
Otherwise it makes no sense to talk about the 'index' of an existing independent variable (though obviously you can construct this L from a bunch of pre-existing variables if desired).
With L in hand, you can now do
[minval, idx] = min( L )
% minval = 1
% idx = 2
to find the minimum and its corresponding index, and
[sorted, sortedindices] = sort( L )
% sorted =
% 1.0000e+00 2.0000e+00 8.9980e+03 1.2343e+07
%
% sortedindices =
% 2 1 4 3
to obtain a sorted array, with corresponding indices.

Elliptic points in Sage

I would like to find elliptic points of congruence subgroups Gamma(N), Gamma_1(N), etc., using Sage. I know there is simple function in MAGMA (EllipticPoints(G)) but can't find anything similar in Sage. Any suggestions?
Sage has functions Gamma, Gamma0, Gamma1 to define congruence subgroups of the modular group.
Given such a group, the methods nu2 and nu3 give the number of
elliptic points of order 2 and of order 3 of this group.
sage: G = Gamma0(13)
sage: G.nu2()
2
sage: G.nu3()
2
The methods ncusps, index, genus give the number of cusps,
index, genus of a congruence group.
sage: G.ncusps()
2
sage: G.index()
14
sage: G.genus()
0
The Magma documentation you are referring to might be this:
https://magma.maths.usyd.edu.au/magma/handbook/text/1554.
In which case you want the actual elliptic points in the
upper half-plane. Here is a way to get them.
Define the function elliptic_points as follows.
def elliptic_points(G):
F = FareySymbol(G)
P = F.pairings()
if all(n > 0 for n in P):
return []
M = F.pairing_matrices()
ell = []
for k, n in enumerate(P):
if n < 0:
a, b, c, d = list(M[k])
R.<x> = QQbar[]
p = c*x^2 + (d-a)*x - b
for r in p.roots(multiplicities=False):
if r.imag() > 0:
ell.append(r)
return ell
Then the following works:
sage: G = Gamma0(13)
sage: ell = elliptic_points(G)
sage: ell
[0.2692307692307693? + 0.06661733875264913?*I,
0.3846153846153846? + 0.07692307692307692?*I,
0.6153846153846154? + 0.07692307692307692?*I,
0.7307692307692308? + 0.06661733875264913?*I]
sage: for p in ell:
....: print p.radical_expression()
....:
1/26*I*sqrt(3) + 7/26
1/13*I + 5/13
1/13*I + 8/13
1/26*I*sqrt(3) + 19/26
I could not find this function in the existing Sage code.
It might be worth adding it.

z3py: how to make constraints for "else" value when inferring a function

I am inferring a function using z3py as follows
f = Function('f',IntSort(),IntSort(),IntSort())
After asserting a set of constraints like:
s.add(f(a1,b1)==c1)
s.add(f(a2,b2)==c2)
s.add(f(a3,b3)==c3)...
The function is inferred as
[(a1,b1) -> c1,
(a2,b2) -> c2,
(a3,b3) -> c3,
...,
else -> 2]
How could I constraint the "else" value to a fixed number? So that the output of inferred f will be
[(a1,b1) -> c1,
(a2,b2) -> c2,
(a3,b3) -> c3,
...,
else -> some number that I assert]
Edit:
from z3 import *
s = Solver()
k = Int('k')
f = Function('f',IntSort(),IntSort())
s.add(And(f(1)==1,f(2)==2))
list1 = []
list1.append(k!=1)
list1.append(k!=2)
s.add(ForAll(k,Implies(And(list1),f(k)==5)))
print s.check()
print s.model()
The output is
sat
[f = [1 -> 1, 2 -> 2, else -> 5]]
This seems to work fine for this simple case.
However, when the input for function 'f' in the constraints is undecided. The output can be weird. For example
from z3 import *
s = Solver()
f = Function('f',IntSort(),IntSort(),IntSort())
i = Int('i')
j = Int('j')
k = Int('k')
l = Int('l')
s.add(i>=0,i<5)
s.add(j>=0,j<5)
s.add(And(f(j,1)==i,f(i,2)==j))
list1 = []
list1.append(And(k==1,l==j))
list1.append(And(k==2,l==i))
s.add(ForAll([k,l],Implies(Not(Or(list1)),f(l,k)==5)))
print s.check()
print s.model()
The output is
[i = 0,
k!6 = 0,
j = 3,
k!12 = 6,
f = [else -> f!15(k!13(Var(0)), k!14(Var(1)))],
f!15 = [(3, 1) -> 0, (0, 2) -> 3, else -> 5],
k!13 = [0 -> 0, 3 -> 3, else -> 6],
k!14 = [2 -> 2, 1 -> 1, else -> 0]]
Then it is hard to interpret the inferred f.
It is a great question and very informed analysis. Yes, you can control the default values by using quantifiers. Z3 will have no choice but agree.
However, the encoding of models is based on how the quantifier instantiation engine (see Complete quantifier instantiation by Yeting Ge and Leonardo de Moura).
Z3 does not beta-reduce the expressions in the models and has left it to applications to perform the beta-reduction, if desired. You can have Z3 beta reduce else branches by plugging in your arguments to the parameters of the functions (use the substitution routines exposed by the API), and then call the model evaluator to reduce the resulting expression with respect to the model.

Lua: Functions with Tables for Beginners - Proper Naming/Retrieving of Tables within Tables

I am having a horrible time at grasping functions and tables. I've asked a question before that is similar to this but still am having problems getting this to work properly. So I will be more descriptive. But just when I think I understand it I completely confuse myself again. Here is what I am trying to accomplish:
I have a program that is receiving its input from an outside source. It needs to take that input, and basically "dissect" the strings to get the required information. Based on the information it receives, it moves onto the next phase or functions to do the appropriate actions. For example:
input is received as NY345,de,M,9900
I created a table that has all of the different ways the specific input can begin, such as:
local t = {["NY"] = 5, ["MS"] = 7, ["HG"] = 10, ["JX"] = 14, ["UY"] = 20}
Now I want to use a function to receive the input and look for k in t{} and use that to gather other variables...
function seek(input)
for k, v in pairs (seek) do
local info = string.match(input,k)
if info then
return {seekData = string.match(input,k..",(%d*),.*"), seekMult = seekData*v}
end
end
end
How far off am I?
If I had the table "t = {...}" above, and that contained other tables; how can I name each table inside of "t = {...}" and retrieve it for other equations? Such as if ["a"] = 8, the rest of that table was to be utilized? For example:
t={["a"] = 2, ["b"] = 3, ["c"] = "IOS"},{["a"] = 8, ["b"] = 9, ["c"] = "NVY"},{["a"] = 1, ["b"] = 5, ["c"] = "CWQ"}}
if a = 8, then b = 9 and c = "NVY"
I would like my function to search k (of each table) and compare it with the input. If that was found, then it would set the other two local variables to b and c?
Thanks for your help!
I will only answer question 1, as 2 and 3 should be separate questions. There are many ways to do this based on specifics you don't mention but assuming you have a table t like this:
t={
{["a"] = 2, ["b"] = 3, ["c"] = "IOS"},
{["a"] = 8, ["b"] = 9, ["c"] = "NVY"},
{["a"] = 1, ["b"] = 5, ["c"] = "CWQ"}
}
then a function that takes an a key value to look for and returns b and c:
function findItem(a, yourTable)
for i,tt in ipairs(yourTable) do
if tt.a == a then
return i, tt.b, tt.c
end
end
end
With this, if the input is k, then
i, b, c = findItem(k, t)
if i == nil then
print('could not find k')
else
print('found k at index ' .. i)
end
The findItem could of course just return the subtable found, and maybe you don't need index:
function findItem(a, yourTable)
for i,tt in ipairs(yourTable) do
if tt.a == a then
return tt
end
end
end