Reputation: 77
I'm trying to get data in JSON file, but before I print out, I want to sort the money
. This is my code
with open("cash.json", "r", encoding="utf-8") as f:
data = json.load(f)
for c in data['NotAdmin']:
a_new_list = sorted(c["money"])
print(a_new_list)
This is what is inside my cash.json
file
{
"NotAdmin": [
{
"money": 200.0,
"name": "Sam",
"password": "1234"
}, {
"money": 150.0,
"name": "Jasmin",
"password": "1573"
}, {
"money": 100.0,
"name": "Ali",
"password": "1856"
}
],
"admin": [
{
"name": "Adam",
"password": "6767"
}, {
"name": "Eva",
"password": "2222"
}
]
}
Im keep getting this error TypeError: 'float' object is not iterable
Upvotes: 2
Views: 498
Reputation:
This is one line solution , You can try
Approach is , first check if key is 'NotAdmin'
and then sorted key is 'money'
import json
print([sorted(value, key=lambda x: x['money']) for key,value in json.loads(open('data.json','r').read()).items() if key=='NotAdmin'])
output:
[[{'name': 'Ali', 'money': 100.0, 'password': '1856'}, {'name': 'Jasmin', 'money': 150.0, 'password': '1573'}, {'name': 'Sam', 'money': 200.0, 'password': '1234'}]]
Upvotes: 0
Reputation: 31670
What you want to do is:
with open("cash.json", "r", encoding="utf-8") as f:
data = json.load(f)
a_new_list = sorted(data['NotAdmin'], key=lambda k: k['money'])
print(a_new_list)
By doing sorted(c["money"])
, you want to sort one float
, the money
of the dictionary c
, which is not possible, nor what you want.
Upvotes: 2
Reputation: 71451
You can try this:
with open("cash.json", "r", encoding="utf-8") as f:
data = json.load(f)
new_data = {a:sorted(b, key=lambda x:x['money']) if all('money' in c for c in b) else b for a, b in data.items()}
Output:
{'NotAdmin': [{'money': 100.0, 'password': '1856', 'name': 'Ali'}, {'money': 150.0, 'password': '1573', 'name': 'Jasmin'}, {'money': 200.0, 'password': '1234', 'name': 'Sam'}], 'admin': [{'password': '6767', 'name': 'Adam'}, {'password': '2222', 'name': 'Eva'}]}
Upvotes: 1
Reputation: 20424
The reason that you are getting the TypeError
is that you were iterating through the dictionaries
in the NotAdmin
list
and trying to apply to the sorted
functions to the money
attributes. This will obviously fail as you are trying to "sort" a float
(?).
So, the way the sorted
(or sort
function which modifies the iterable
in place) works is to take an iterable
and apply an "evaluation" function to each element, in order to sort them. This function can either be passed in under the key
parameter, or if not passed in, Python will automatically decide how to sort the elements.
To apply this to our code, we want to take the data["NotAdmin"]
list and apply the .sort
method with a key
to select the money
attribute of each of the dictionaries. We can do this with an (anonymous) lambda
function.
So, here is the full code:
with open("cash.json", "r", encoding="utf-8") as f:
data = json.load(f)
data["NotAdmin"].sort(key = lambda d: d["money"])
print(data)
output:
{'admin': [{'password': '6767', 'name': 'Adam'}, {'password': '2222', 'name': 'Eva'}], 'NotAdmin': [{'password': '1856', 'money': 100.0, 'name': 'Ali'}, {'password': '1573', 'money': 150.0, 'name': 'Jasmin'}, {'password': '1234', 'money': 200.0, 'name': 'Sam'}]}
And also, if you want to have the elements in descending order, then we can take a look at the documentation and see that we can set the reverse
flag to True
.
Which would just change the line to:
...
...
data["NotAdmin"].sort(key = lambda d: d["money"], reverse = True)
...
Upvotes: 2
Reputation: 1519
The error you got is due to its definition
sorted??
Docstring: sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
Type: builtin_function_or_method
To tackle your problem, you need to sort array object using their attributes, example
with open("cash.json", "r", encoding="utf-8") as f:
data = json.load(f)
for c in data['NotAdmin']:
a_new_list = sorted(c, key=lambda x: x['money'], reverse=True)
print(a_new_list)
see more: How to sort a list of objects based on an attribute of the objects?
Upvotes: 0