我有這個信息的字典:對於在dictonary項目,拆一個項目的名稱,添加和刪除一些信息蟒蛇
x = {Country:{City:Population},.....}
但城市名稱現在是這樣的:Country_City_Neighbourhood_Population
(出於某種原因) 。我需要刪除所有的信息,並保持這樣的城市和社區:CityNeighbourhood
。
我這樣做:
for country in x:
for city, pop in x[country].iteritems():
isCountry = city.split("_").count("Ecuador")
if isCountry > 0:
city1 = city.split("_")
city1.remove("Ecuador")
city2 = city1[0:-1]
city3 = ""
for i in range(len(city2)-1):
city3 = city3 + city2[i]
,但我沒有獲得任何合理的結果。
謝謝你們......你永遠是最棒的! :) – Alejandro
整潔:遍歷'x.items()'並移除'x [country]'lookup – katrielalex