Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用列表项创建不同的输出?_Python_List_For Loop - Fatal编程技术网

Python 如何使用列表项创建不同的输出?

Python 如何使用列表项创建不同的输出?,python,list,for-loop,Python,List,For Loop,我有一个以国家为项目和两个变量的国家列表。对于变量,我想循环浏览根据特定国家创造不同产出的国家列表。现在我每个国家只有3行。我想有3行*我的列表长度 现在我必须一次指定一个国家,而我想通过一个完整的列表。举例说明: import pandas as pd df = pd.read_excel('Sales', sheet_name='unique enroll- Dataset') #What I do now: country = 'Arabian Emirates' #What

我有一个以国家为项目和两个变量的国家列表。对于变量,我想循环浏览根据特定国家创造不同产出的国家列表。现在我每个国家只有3行。我想有3行*我的列表长度

现在我必须一次指定一个国家,而我想通过一个完整的列表。举例说明:

import pandas as pd
df = pd.read_excel('Sales', sheet_name='unique enroll- Dataset')

#What I do now:
country = 'Arabian Emirates'    

#What I want to do: 
country_lst = [ 'Arabian Emirates', 'Argentina', 'Australia']

def country(country):
     for country in country_lst:
         return country
变量:

Renewal1 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")]

Renewal2 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")& (df["License Model"].isin(['A'])
输出:

print("These numbers are for: " + country)
print("#1 of Renewals per sub for Q4: " + str(Renewal1["Count_Contract"].sum()))
print("# Non SCE of Renewals per sub for Q4: " + str(Renewal2["Count_Contract"].sum()))
立即输出(列表的最后一项):

期望输出:

These numbers are for: Arabian Emirates
#1 of Renewals per sub for Q4: 5
# Non SCE of Renewals per sub for Q4: 3

These numbers are for: Argentina
#1 of Renewals per sub for Q4: 6
# Non SCE of Renewals per sub for Q4: 1

These numbers are for: Australia
#1 of Renewals per sub for Q4: 1
# Non SCE of Renewals per sub for Q4: 2

我想这就行了:

def country_data(country):
    Renewal1 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")]
    Renewal2 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")& (df["License Model"].isin(['A'])
    return (str(Renewal1["Count_Contract"].sum()),str(Renewal2["Count_Contract"].sum()))

for c in country_lst:
    cd = country_data(c)
    print("These numbers are for: " + c)
    print("#1 of Renewals per sub for Q4: " + cd[0])
    print("# Non SCE of Renewals per sub for Q4: " + cd[1])

您希望为每个国家/地区执行的所有操作都应在for循环中:

def country(country_lst):
     for country in country_lst:
         Renewal1 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")]
         Renewal2 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")& (df["License Model"].isin(['A'])
         print("These numbers are for: " + country)
         print("#1 of Renewals per sub for Q4: " + str(Renewal1["Count_Contract"].sum()))
         print("# Non SCE of Renewals per sub for Q4: " + str(Renewal2["Count_Contract"].sum()))
def country(country_lst):
     for country in country_lst:
         Renewal1 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")]
         Renewal2 = df[(df["Country2"] == country) & (df["Quarter"] == "2000-Q4")& (df["License Model"].isin(['A'])
         print("These numbers are for: " + country)
         print("#1 of Renewals per sub for Q4: " + str(Renewal1["Count_Contract"].sum()))
         print("# Non SCE of Renewals per sub for Q4: " + str(Renewal2["Count_Contract"].sum()))