Python 基于位置和时间的Sum数据库
我对Python中的for循环有问题。我想根据时间和地点对这些数据进行汇总,不包括熊猫。此数据位于MySQL数据库(MySQL工作台)中: 现在我希望它是这样的:Python 基于位置和时间的Sum数据库,python,database,Python,Database,我对Python中的for循环有问题。我想根据时间和地点对这些数据进行汇总,不包括熊猫。此数据位于MySQL数据库(MySQL工作台)中: 现在我希望它是这样的: Time No_of_people Location ---------------------------------------- 07:00 116 Liberty City 08:00 120 Liberty City ... 07:00
Time No_of_people Location
----------------------------------------
07:00 116 Liberty City
08:00 120 Liberty City
...
07:00 100 San Andreas
这就是我目前所做的:
views.py:
def getData(request):
api = 'http://localhost:8000/api/myData/'
response = requests.get(api)
myData = response.json()
time = []
no_of_people = []
location = []
for hourly in myData:
time.append(hourly['time'])
no_of_people.append(hourly['no_of_people'])
location.append(hourly['location'])
hour = []
for x in range(7,24):
hour.append(x)
uniqueLocation=[]
for x in location:
if x not in uniqueLocation:
uniqueLocation.append(x)
for uniqueIndex in uniqueLocation:
for x in hour:
sum =0
for index, t in enumerate(time):
x_time = t.split(":")[0]
if int(x_time) == x and uniqueIndex == location[index]:
sum += no_of_people[index]
print(str(sum))
json_obj = {
"time": time,
"no_of_people": no_of_people,
"location": location
}
return JsonResponse(data=json_obj)
您希望按位置分组,因此我建议您选择这种更易于可视化的格式,然后尝试从中构建表格输出(对于每个城市,对于每个时间,打印小时数和人/小时) 在处理几乎任何数据库时,请尝试为每个对象(行、表、bucket、关系,(在此处插入数据库术语)创建一个类。然后,您可以在这里隔离逻辑,而不是使主功能混乱
class Location:
def __init__(self, name):
self.name = name
self.times = list()
def __str__(self):
s = ['{}\t{}\t{}'.format(k, t[k], self.name) for t in self.times for k in t.keys()]
return '\n'.join(s)
def add_time(self, hour, people):
existing_people_for_hour = None
for t in self.times: # loop existing times, looking for the hour
existing_people_for_hour = t.get(hour)
if existing_people_for_hour is not None:
t[hour] += people
break # found the hour to update, so break the loop
if existing_people_for_hour is None: # if the hour was never found, add to the times list
self.times.append({hour : people})
有了它,使用字典对位置值进行分组,最终应该能够打印它们
locations = dict()
for d in myData:
# parse each value out
hour = d['time'][:2] + ':00'
p = int(d['no_of_people'])
loc = d['location']
# get the location from the map, if exists, else create new one
l = locations.get(loc, Location(loc))
l.add_time(hour, p) # add the people for the time
locations[loc] = l # upsert the new location
for l in locations.values():
print(l)
输出
您可以使用一个列表
行,并保留项目[每小时['time'],每小时['no'u of_people'],每小时['location']],而不是三个列表时间
,地点
。您还可以添加hourly['time'][:2]
以获得小时数。uniqueLocation=set(location)
-如果您不需要保持位置的原始顺序。为什么要使用JSON?Python可以查询数据库,熊猫有什么问题?就努比或达斯克怎么样?@furas我来试试。。感谢在def add_time(self,hour-people)中hour
和people
之间应该有一个逗号:
?另外,我得到了一个错误t[hour]+=people#如果找到了,它已经有一个值KeyError:'07:00'
是的,很好。我对答案做了一个编辑,我认为这是无效的。由于某种原因,总数加起来是不正确的。这是输出:Liberty City 07:00 20 San Andreas 07:00 10
它也不显示08:00,我没有对此进行精确测试(您也没有显示实际的JSON数据)。我想你还没有学会如何自己调试代码吗?是的,我是python新手,我在前面提到过它,但有人编辑了我的问题。
class Location:
def __init__(self, name):
self.name = name
self.times = list()
def __str__(self):
s = ['{}\t{}\t{}'.format(k, t[k], self.name) for t in self.times for k in t.keys()]
return '\n'.join(s)
def add_time(self, hour, people):
existing_people_for_hour = None
for t in self.times: # loop existing times, looking for the hour
existing_people_for_hour = t.get(hour)
if existing_people_for_hour is not None:
t[hour] += people
break # found the hour to update, so break the loop
if existing_people_for_hour is None: # if the hour was never found, add to the times list
self.times.append({hour : people})
locations = dict()
for d in myData:
# parse each value out
hour = d['time'][:2] + ':00'
p = int(d['no_of_people'])
loc = d['location']
# get the location from the map, if exists, else create new one
l = locations.get(loc, Location(loc))
l.add_time(hour, p) # add the people for the time
locations[loc] = l # upsert the new location
for l in locations.values():
print(l)
07:00 95 Liberty City
08:00 21 Liberty City
07:00 70 San Andreas
08:00 30 San Andreas