2

したがって、私のプログラムには、get_codes と get_data の 2 つの定義があり、以下に示すように合計 8 つのリストを作成しました。

CountryCodes=open("CountryCodes.csv",'r')
CountryData=open("CountryData.csv", 'r')

def get_codes(file):
    country_code=[]
    country_name=[]
    continent=[]

    for line in CountryCodes: 
        c_fields=line.split(",")

        c_field1=c_fields[0].strip()
        c_field2=c_fields[1].strip()
        c_field3=c_fields[2].strip() 

        country_code.append(c_field1)
        country_name.append(c_field2)
        continent.append(c_field3)

    return country_code, country_name, continent

def get_data(file): 
    data_country_code=[]
    country_pop=[]
    country_area=[]
    country_gdp=[]
    country_lit_rate=[]

    for line in CountryData: 
        d_fields=line.split(",")

        d_field1=d_fields[0].strip()
        d_field2=d_fields[1].strip()
        d_field3=d_fields[2].strip()
        d_field4=d_fields[3].strip()
        d_field5=d_fields[4].strip()

        data_country_code.append(d_field1)
        country_pop.append(int(d_field2))
        country_area.append(d_field3)
        country_gdp.append(int(d_field4))
        country_lit_rate.append(d_field5)

    return data_country_code, country_pop, country_area, country_gdp, country_lit_rate

ここで私がしなければならないことは、country_pop を昇順で、後で降順にするメニュー オプション (私はメニューを持っています) を作成することです。これが私がこれまでに持っているものです:

def asc_sort():
    for x in range (0,len(country_pop)):
        country_pop2=sorted(country_pop)

私は並べ替えをしていますが、私の教授はcountry_pop2の印刷だけを望んでいません。彼女はまた、人口が存在する CountryData ではなく、CountryCodes にある country_name も必要としています。したがって、country_pop のインデックス x は、data_country_code の x でもある必要があります。次に、data_country_code で x の入力を取得する必要があります。これが AL であるとしましょう。それを country_code で見つけます。次に、country_code、AL に対応する country_name、Albania を見つけ、country_name を country_pop とともにリストする必要があります。これは次のようになると思います。

print("%-10s \t %-10s \t" %("NAMES","POPULATION"))
for ind in range (0,len(list1)):
    if ind%10==9:
        input("Press ENTER to continue")
    print("%-2s \t %-10s \t %-10s \t %-10s \t %-10s \t" %(I'd need help here)

(リストが非常に長いため、一度にいくつかしか表示したくないため、書式設定と if ステートメントに %-10s の部分が必要です)助けをいただければ幸いです。さらに説明が必要な場合は、最善を尽くします。

4

2 に答える 2

2

次のコードはあなたが望むことをすると思います:

with open("CountryCodes.csv",'r') as CountryCodes:
    genc = (line.split(',') for line in CountryCodes)

    c2n = dict((c_fields[0].strip(),c_fields[1].strip())
               for c_fields in genc)

with open("CountryData.csv",'r') as CountryData:
    gend = (line.split(',') for line in CountryData)

    the_data = [ (c2n[d_fields[0].strip()], # data_country_code
                  int(d_fields[1]),         # country_pop
                  d_fields[2].strip(),      # country_area
                  d_fields[3].strip(),      # country_gdp
                  d_fields[4].strip())      # country_lit_rate
                 for d_fields in gend ]

the_data.sort(key = lambda x: x[1])

p = 10
for i in xrange(0,len(the_data),p):
    if i:  raw_input("  Press ENTER to continue\n")
    print ('\n'.join("%-10s \t %-10s \t %-10s \t %-10s \t %s"
                     % el for el in the_data[i:i+p]) )

辞書c2nはコードに対応する名前を与えます。

人口フィールドは確かに、ストライプ化する必要のない整数を表す文字列です。空白が含まれていても (空白、タブ... 改行ではありません)

.

編集

辞書を使用する権限がなく、リストのみを使用する権限がある場合は、次のようにすることができます。

with open("CountryCodes.csv",'r') as CountryCodes:
    genc = (line.split(',') for line in CountryCodes)
    lic = [map(str.strip,row) for row in genc]

def name_of(x,lic=lic):
  for code,name,continent in lic:
    if x==code:
      return name

with open("CountryData.csv",'r') as CountryData:
    gend = (line.split(',') for line in CountryData)

    the_data = [ (name_of(d_fields[0].strip()), # data_country_code
                  int(d_fields[1]),         # country_pop
                  d_fields[2].strip(),      # country_area
                  d_fields[3].strip(),      # country_gdp
                  d_fields[4].strip())      # country_lit_rate
                 for d_fields in gend ]
于 2013-10-01T22:30:34.910 に答える