したがって、私のプログラムには、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 の部分が必要です)助けをいただければ幸いです。さらに説明が必要な場合は、最善を尽くします。