列表成对生成字典list_1 = list(range(3)) list_2 = ['col' + str(i) for i in list_1] dict_1 = { key: value for key, value in zip(list_2, list_1) } dict_1
list_1 = list(range(3))
list_2 = ['col' + str(i) for i in list_1]
dict_1 = {
key: value for key, value in zip(list_2, list_1)
}
dict_1
表格的列元素成对生成列表
'''工具_列元素成对'''
df_zip = pd.DataFrame(data=np.arange(0, 12).reshape(4, 3), columns=['col' + str(i) for i in range(3)])
def fun_zip(arg_df=df_zip):
list_result = [
(col_name, *row) for col_name, row in zip(
arg_df.columns,
zip(*[arg_df.iloc[i] for i in range(arg_df.shape[0])])
)
]
return list_result
def fun_zip(arg_df=df_zip):
list_result = [
tuple(row) for row in zip(*[arg_df.iloc[i] for i in range(arg_df.shape[0])])
]
return list_result
fun_zip()
def fun_zip(
self,
df_arg=pd.DataFrame(),
bool_arg=False
):
# 每列元素打包
zipped = zip(*[df_arg.iloc[i] for i in range(df_arg.shape[0])])
# 是否包含标题
if bool_arg:
list_zip = [(col_name, *row) for col_name, row in zip(df_arg.columns, zipped)]
else:
list_zip = [tuple(row) for row in zipped]
return list_zip
标签:zip,python,list,生成,df,arg,列表,col,row
From: https://blog.51cto.com/u_16055028/7118936