在访问和更改数据集中的一些值方面,我需要一些指导。我正在使用Strava API,所以它的自行车数据。我有一个由字典组成的列表,其中包含一个字符串作为关键字,还有一个列表作为值:
[{'ride_name_one':[1.3,7.5,8.2,4.8]},{'ride_name_2:'[4.7,6.8,8.9,4.3]}]
我必须将这些值中的每一个从m/s转换为km/h,并将其保存回该列表中。我该如何深入到这些列表中进行更新?我猜它大概是:
streamsTest[0]['ride_name_one']
除了不知道如何使"ride_name_one"动态?
或
有更好的地方进行转换吗?
到目前为止,这是我的代码:
athleteID = [xxxxxxxx]
activityIDs = [xxxxxxxxx,xxxxxxxxx]
def getSpeeds(cIDs,aIDs):
print cIDs
print aIDs
streamsTest = []
for i in aIDs:
aID = i
types = ['velocity_smooth']
streams = client.get_activity_streams(aID, types=types, resolution='low')
activity = client.get_activity(aID)
if 'velocity_smooth' in streams.keys():
actName = activity.name
raw = streams['velocity_smooth'].data
streamsTest.append({actName:raw})
else:
print "velocity_smooth is not available"
print streamsTest
#[{'key':[1,2,3,4]},{'key:'[1,2,3,4]}]
### do m/s to km/h conversion on nested list data
getSpeeds(athleteID,activityIDs)
找到了一种方法,如果有更好的方法,请随时发表评论。
athleteID = [xxxxxxx]
activityIDs = [xxxxxxxxx,xxxxxxxxx]
def getSpeeds(cIDs,aIDs):
streamsDicts = []
streamsFinal = []
for i in aIDs:
aID = i
types = ['velocity_smooth']
streams = client.get_activity_streams(aID, types=types, resolution='low')
activity = client.get_activity(aID)
if 'velocity_smooth' in streams.keys():
actName = activity.name
raw = streams['velocity_smooth'].data
streamsDicts.append({actName:raw[1:]})
else:
print "velocity_smooth is not available"
for i in streamsDicts:
for key, value in i.iteritems():
value = [round((x * 3600) / 1000,2) for x in value]
streamsFinal.append({key:value})
print streamsFinal
getSpeeds(athleteID,activityIDs)