2017-10-18 50 views
0

嗨,我正在使用Python来保存使用纬度,经度和时间戳(unix时间)的位置数据。使用字典使用python为现有密钥添加值

但我尝试添加使用功能字典,追加,我不能运行它,因为“追加”不能与STR

工作,这是我到目前为止的代码

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'} 



def make_timestamped_loc(La, Lng, Time): 
    dic_loc['latitude'].append(La) 
    dic_loc['longitude'].append(Lng) 
    dic_loc['timestamp'].append(Time) 

make_timestamped_loc(37.481236, 126.952733, 1483196400) 
make_timestamped_loc(37.481126, 126.952733, 1498859012) 

print(dic_loc) 

如果我可以,我想消除现有的str值并将新值添加到每个键。非常感谢您

+2

如果您甚至没有指定输出应该是什么样子,您如何期望得到正确答案? –

回答

1

在你的情况,你应该分配但不追加

... 
dic_loc['latitude'] = La 
+0

@YaroslavSurzhikov,再次读到这个问题:*我想* **消除**存在的str值并将**新**值添加到每个键* – RomanPerekhrest

+1

对不起,无视 –

1

离开键空的,然后再追加:

dic_loc = {'latitude': [], 'longitude': [], 'timestamp': []} 

def make_timestamped_loc(La, Lng, Time): 
    dic_loc['latitude'].append(La) 
    dic_loc['longitude'].append(Lng) 
    dic_loc['timestamp'].append(Time) 
+1

但我想补充,不分配。如果发生这种情况,我不能添加更多的值 –

0

尝试:

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'} 


def make_timestamped_loc(lat, lng, timestamp): 
    for key in ['latitude', 'longitude', 'timestamp']: 
     if not isinstance(dic_loc[key], list): 
      dic_loc[key] = [dic_loc[key]] 

    dic_loc['latitude'].append(lat) 
    dic_loc['longitude'].append(lng) 
    dic_loc['timestamp'].append(timestamp) 

make_timestamped_loc(37.481236, 126.952733, 1483196400) 
make_timestamped_loc(37.481126, 126.952733, 1498859012) 

print(dic_loc) 
2

追加只适用于list,因此您不能在dict中使用它,因为它在作业中存储了string。所以,你可以通过两种方式来完成。使用list来存储值,然后使用append it或addstring本身。


使用list

dic_loc = {'latitude': ['Value for latitude '], 'longitude': ['Value for longitude'], 'timestamp': ['Time Stamp']} 

def make_timestamped_loc(La, Lng, Time): 
    dic_loc['latitude'].append(La) 
    dic_loc['longitude'].append(Lng) 
    dic_loc['timestamp'].append(Time) 

make_timestamped_loc(37.481236, 126.952733, 1483196400) 
make_timestamped_loc(37.481126, 126.952733, 1498859012) 

print(dic_loc) 

''' 
{ 
'latitude': ['Value for latitude ', 37.481236, 37.481126], 
'longitude': ['Value for longitude', 126.952733, 126.952733], 
'timestamp': ['Time Stamp', 1483196400, 1498859012] 
} 
''' 

或者strings

dic_loc = {'latitude': 'Value for latitude ', 'longitude': 'Value for longitude', 'timestamp': 'Time Stamp'} 

def make_timestamped_loc(La, Lng, Time): 
    dic_loc['latitude']+= ' '+str(La) 
    dic_loc['longitude']+=' '+str(Lng) 
    dic_loc['timestamp']+=' '+str(Time) 

make_timestamped_loc(37.481236, 126.952733, 1483196400) 
make_timestamped_loc(37.481126, 126.952733, 1498859012) 

print(dic_loc) 

''' 
{ 
'latitude': 'Value for latitude 37.481236 37.481126', 
'longitude': 'Value for longitude 126.952733 126.952733', 
'timestamp': 'Time Stamp 1483196400 1498859012'} 
''' 

如果你正在寻找的只是:

如果可以,我想消除已有的str值并将新值添加到每个键。

您可以直接赋值:

def make_timestamped_loc(La, Lng, Time): 
    dic_loc['latitude'] = La 
    dic_loc['longitude'] = Lng 
    dic_loc['timestamp'] = Time 
0

如果你想取代与最新的一个实际的时间戳,我会用这个去:前面已经

def make_timestamped_loc(La, Lng, Time): 

    return {'latitude': 'Value for latitude ' + str(La), 'longitude': 'Value for longitude' + str(Lng), 'timestamp': 'Time Stamp ' + str(Time)} 

由其他评论指出,.append(obj)只适用于列表对象。