创建一个包含值作为另一个列表的字典

时间:2017-06-15 13:47:03

标签: python csv dictionary

我有一些数据如下:

United Kingdom,57533000,London,6756000,Europe,1066,1945,-,English
United States,252177000,Washington DC,606900,North America,1776,1945,-,English
Venezuela,19733000,Caracas,1290087,Latin America,1811,1945,-,Spanish
Vietnam,67589000,Hanoi,1088862,Orient,1945,1977,-,Vietnamese
Yemen,1184300,San'a,427150,Asia,1918,1957,Islam,Arabic
Argentina,36955182,Buenos Aires,2033445,Latin America,1853,1945,-,Spanish
Brazil,172860370,Brasilia,286037,0Latin America,1822,1945,-,Portuguese
Bahrain,634137,Manama,34137,Persian Gulf,1973,1977,Islamic,Arabic
Cameroon,15421937,Yaounde,421937,Africa,1960,1974,-,Franch
Djibouti,451442,Djibouti,1442,Africa,1977,1980,-,Franch
Equatorial Guinea,474214,Malabo,74214,Africa,1991,1995,-,Franch
Fiji,832494,Suva,32494,Oceania,1970,1975,-,English
France,59329691,Paris,329691,Europe,486,1945,-,Franch
Greece,10601527,Athens,601527,Europe,1829,1945,-,Greek
Germany,82797408,Berlin,1797408,Europe,1871,1960,-,German
Honduras,6249598,Tegucigalpa,1249598,Latin America,1821,1945,-,Spanish
China,1261832482,Beijing,61832482,Asia,-221,1945,-,Chinese
Canada,31281092,Ottawa,1281092,North America,1867,1945,-,English
Hungary,10138844,Budapest,1138844,Europe,1001,1945,-,Hungerian
India,1014003817,New Delhi,14003817,Asia,1947,1950,-,Indian
Italy,57634327,Rome,3634327,Europe,1861,1950,-,Italian
Ireland,3797257,Dublin,797257,Europe,1921,1945,-,English
Japan,126549976,Tokio,16549976,Asia,-660,1955,-,Japanese

我的要求是将数据存储在字典中,如下所示:

{'Portuguese': ['Brazil'], 'Franch': ['Cameroon', 'Djibouti', 'Equatorial Guinea', 'France'], 'Chinese': ['China'], 'Vietnamese': ['Vietnam'], 'German': ['Germany'], 'English': ['United Kingdom', 'United States', 'Fiji', 'Canada', 'Ireland'], 'Japanese': ['Japan'], 'Greek': ['Greece'], 'Indian': ['India'], 'Spanish': ['Venezuela', 'Argentina', 'Honduras'], 'Arabic': ['Yemen', 'Bahrain'], 'Hungerian': ['Hungary'], 'Italian': ['Italy']}

有人能帮助我实现这个目标吗?

发布为重复的其他查询对我没有帮助。我添加了我尝试过的代码。请帮帮我。

3 个答案:

答案 0 :(得分:0)

您的问题上有很多评论要求您在问题本身上正确发布格式的代码。这肯定会让你早些回复。

我把你粘贴的代码作为评论并尝试格式化,我想出了这个:

countryData=open('filename.txt','r') 
dictCountry={} 
langList=[] 
countryList=[] 

def createList(langL,input): 
    input=input.rstrip()
    country=input.split(',')[0] 
    if input.split(',')[-1] in langL: 
        countryList.append(country) 
    return countryList 

for line1 in countryData: 
    line1=line1.rstrip() 
    lang1=line1.split(',')[-1] 
    if lang1 not in langList: 
        langList.append(lang1) 

for line2 in countryData: 
    line2=line2.rstrip() 
    lang=line2.split(',')[-1] 
    dictCountry[lang]=createList(lang, line2) 

print(dictCountry) 

您的尝试存在一些问题:

  • 在同一个打开的文件上多次迭代。这将使第二个for循环永远不会运行
  • countryList是全球定义的,因此它永远不会只保留当前语言的国家/地区
  • 填充langList的内容后,从未使用

我将其改为一个有效的解决方案:

f = open('filename.txt','r')
countryData = f.readlines()
f.close()

dictCountry = {}

for line in countryData:
    line = line.rstrip()
    lang = line.split(',')[-1]
    country = line.split(',')[0]
    if lang not in dictCountry:
        dictCountry[lang] = []

    dictCountry[lang].append(country)

print(dictCountry)

答案 1 :(得分:0)

希望下面给出的解决方案能有所帮助。我还附加了输出内容:

        f = open('lang.txt','r')
        countryData = f.readlines() //reading all contents of file
        f.close()

        dictCountry = {}

        for line in countryData:
            pos = 0

            line = line.rstrip()
            lang = line.split(',')[-1] //getting the lang name from eachline
            #updating the list of languages
            if lang not in dictCountry:
                dictCountry[lang] = []

            country = line.split(',') //splitting the line fro country names
            sub = ' '
            for i in country:
                if i in dictCountry.keys(): //ignoring lang name present in 
                    pass
                elif i.isalpha():
                    dictCountry[lang].append(i)
                elif i.find(sub)!= -1:
                    dictCountry[lang].append(i)
                else:
                    pass

        for i in dictCountry.keys():
            print i," : ",dictCountry[i]


**Output :
Portuguese  :  ['Brazil', 'Brasilia', '0Latin America']
Franch  :  ['Cameroon', 'Yaounde', 'Africa', 'Djibouti', 'Djibouti', 'Africa', 'Equatorial Guinea', 'Malabo', 'Africa', 'France', 'Paris', 'Europe']
Chinese  :  ['China', 'Beijing', 'Asia']
Vietnamese  :  ['Vietnam', 'Hanoi', 'Orient']
German  :  ['Germany', 'Berlin', 'Europe']
English  :  ['United Kingdom', 'London', 'Europe', 'United States', 'Washington DC', 'North America', 'Fiji', 'Suva', 'Oceania', 'Canada', 'Ottawa', 'North America', 'Ireland', 'Dublin', 'Europe']
Japanese  :  ['Japan', 'Tokio', 'Asia']
Greek  :  ['Greece', 'Athens', 'Europe']
Indian  :  ['India', 'New Delhi', 'Asia']
Spanish  :  ['Venezuela', 'Caracas', 'Latin America', 'Argentina', 'Buenos Aires', 'Latin America', 'Honduras', 'Tegucigalpa', 'Latin America']
Arabic  :  ['Yemen', 'Asia', 'Islam', 'Bahrain', 'Manama', 'Persian Gulf', 'Islamic']
Hungerian  :  ['Hungary', 'Budapest', 'Europe']
Italian  :  ['Italy', 'Rome', 'Europe']**

答案 2 :(得分:0)

Here is the answer I could reach to:

f=open('country.txt','r')
countryData1=f.readlines()
f.close()

sourceDict={}
destDict={}

for lines in countryData1:
    lines=lines.rstrip()
    lang=lines.split(',')[-1]
    if lang not in sourceDict.keys():
        sourceDict[lang]=lines.split(',')[0]+","
    else:
        sourceDict[lang]+=lines.split(',')[0]+","    

for item in sourceDict.keys():
    sourceDict[item]=str(sourceDict[item]).rstrip(',')
    destDict[item]=str(sourceDict[item]).split(',')


for item in destDict.keys():
    print(item+ "   "+ str(destDict[item]))