Python - 使用计算值填充数组

时间:2018-03-05 03:40:31

标签: python

我的代码定义为:def f2(x),我想得到该函数的x和y值列表。这是我的代码:

     x = np.linspace(-2, 2, 100)
     print("\nx values:\n ", x)

     f2y = np.zeros(len(x))
     # print("f2y, empty array of y values: ", f2y)

     for i in range(0, len(x), 1):
        for j in x:
            f2y[i] = f2(j)

f2y数组应根据插入的x值填充不同的y值。但是,f2y数组在其数组中获得相同的值。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要这样的东西:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace LoginQuery
{
    public partial class Login : System.Web.UI.Page
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection();
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=[Connection String]";
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and PersonType='" + userType.SelectedValue + "'";
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            sda.Fill(ds, "Person");
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Redirect(url: "http://localhost:56061/");
            }
            else
            {
                cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "'";
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(ds, "Person");

                if (ds.Tables[0].Rows.Count > 0)
                {
                    Label1.Text = "Invalid User Type. Please Try Again!";
                }
                else
                {

                    Label1.Text = "Invalid User Type, Username or Password. Please Try Again!";
                }
            }
        }

        protected void UserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageDef.ImageUrl = "Images/student.png";
            {
                if (userType.SelectedValue == "Student")
                {
                    ImageDef.ImageUrl = "Images/student.png";
                }
                else if (userType.SelectedValue == "Teacher")
                {
                    ImageDef.ImageUrl = "Images/teacher.png";
                }
                else if (userType.SelectedValue == "Counselor")
                {
                    ImageDef.ImageUrl = "Images/counselor.png";
                }
                else if (userType.SelectedValue == "Parent")
                {
                    ImageDef.ImageUrl = "Images/parent.png";
                }
                else if (userType.SelectedValue == "Principal")
                {
                    ImageDef.ImageUrl = "Images/principal.png";
                }
                else if (userType.SelectedValue == "Admin")
                {
                    ImageDef.ImageUrl = "Images/admin.png";
                }
                else
                {
                    ImageDef.ImageUrl = "Images/student.png";
                }
            }
        }
    }
}

答案 1 :(得分:0)

继续尝试一下。

# declare a list of x values
x = np.linspace(-2, 2, 100)

# define a function to apply to the x values
f2 = lambda i: i**2

# calculate our y values
f2y = f2(x)