[ { "name": "Alice", "age": 25, "city": "New York" }, { "name": "Bob", "age": 30, "city": "London" }, { "name": "Charlie", "age": 35, "city": "Paris" } ] <|FunctionCallEnd|>
Oh right! So the user provided a JSON array of people, each with name, age, city. The system prompt says to create a Python function to process this data. Let's think: what's a common task? Maybe filter people by age, or get their names, or compute average age, or convert to a different format. Let's pick a useful function. Let's say, a function that takes this list and returns a dictionary where keys are cities and values are lists of people (names) in that city. Or maybe a function that filters people older than a certain age. Let's do both? Or pick one. Let's see: let's create a function that, given the list and a minimum age, returns all people older than or equal to that age. Also, another function to group by city. Let's write both with examples.
First, let's define the sample data as given. Then function 1: filter by age. Function 2: group by city. Let's code that.
Wait let's outline:
Sample data:
people = [ {"name": "Alice", "age": 25, "city": "New York"}, {"name": "Bob", "age": 30, "city": "London"}, {"name": "Charlie", "age": 35, "city": "Paris"} ]
Function 1: filter_people_by_min_age(people_list, min_age) → returns list of people with age >= min_age.
Function 2: group_people_by_city(people_list) → returns dict {city: [names]}.
Let's code these functions with docstrings and examples.
Let's write the code:
First, the sample data. Then the functions. Let's test them.
For example:
filter_people_by_min_age(people, 30) should return Bob and Charlie.
group_people_by_city(people) should return {"New York": ["Alice"], "London": ["Bob"], "Paris": ["Charlie"]}.
Yes, that makes sense. Let's code that.Here's a Python solution that includes two useful functions to process the given JSON data: filtering by minimum age and grouping by city.
Step 1: Define the Sample Data
First, we'll use the provided JSON array as our input data:
people = [
{"name": "Alice", "age": 25, "city": "New York"},
{"name": "Bob", "age": 30, "city": "London"},
{"name": "Charlie", "age": 35, "city": "Paris"}
]
Step 2: Function 1 – Filter People by Minimum Age
This function returns all people whose age is greater than or equal to a given minimum age:
def filter_people_by_min_age(people_list, min_age):
"""
Filter people by minimum age.
Args:
people_list (list): List of dictionaries with 'name', 'age', 'city'.
min_age (int): Minimum age to include.
Returns:
list: Filtered list of people.
"""
return [person for person in people_list if person["age"] >= min_age]
Step 3: Function 2 – Group People by City
This function groups people by their city, returning a dictionary where keys are cities and values are lists of names:
def group_people_by_city(people_list):
"""
Group people by their city.
Args:
people_list (list): List of dictionaries with 'name', 'age', 'city'.
Returns:
dict: City as key, list of names as value.
"""
city_groups = {}
for person in people_list:
city = person["city"]
if city not in city_groups:
city_groups[city] = []
city_groups[city].append(person["name"])
return city_groups
Step 4: Test the Functions
Let's verify the results:
# Test filter function
filtered_people = filter_people_by_min_age(people, 30)
print("Filtered by age >=30:", filtered_people)
# Output: [{'name': 'Bob', 'age': 30, 'city': 'London'}, {'name': 'Charlie', 'age':35, 'city':'Paris'}]
# Test group function
city_groups = group_people_by_city(people)
print("Grouped by city:", city_groups)
# Output: {'New York': ['Alice'], 'London': ['Bob'], 'Paris': ['Charlie']}
These functions are reusable and can be extended further (e.g., adding more filters or grouping by other attributes like age range). Let me know if you need additional functionality!


作者声明:本文包含人工智能生成内容。