l = ["a", "b", "c", "d", "e"]
Run Code Online (Sandbox Code Playgroud)
我想将此列表转换为如下字典:
d = {"a": "b", "c": "d", "e": ""}
Run Code Online (Sandbox Code Playgroud)
所以基本上,均衡将是关键,而赔率将是价值.我知道我可以用"非pythonic"的方式来实现它,例如if语句的for循环,但我相信应该有更"pythonic"的方法来实现这一点.所以,我感谢任何帮助:)
说我有这个清单:
li = ["a", "b", "a", "c", "x", "d", "a", "6"]
Run Code Online (Sandbox Code Playgroud)
至于帮助告诉我,没有内置函数返回最后一次出现的字符串(如反之index).基本上,我怎样才能找到"a"给定列表中的最后一次出现?
为什么Request["parameterName"]在视图中返回null?我知道我可以从控制器获取它,但我必须在视图中进行一些检查.我正在使用ASP.NET MVC 3.
谁能告诉我为什么要使用NonAction?我的意思是说我有一个包含多个提交值的表单:Update,Delete或Insert.由于所有提交按钮具有相同的共同形式,因此我在控制器内切换提交值并采取相应措施.
像这样:
public ActionResult asd(string submitButton){
switch(submitButton){
case "Insert":
return Insert();
// bla bla bla
}
}
[NonAction]
public ActionResult Insert(){
// some code inside here
return View();
}
Run Code Online (Sandbox Code Playgroud)
再一次,为什么我应该使用NonAction而不是这样的东西:
public void Insert(){
// some code inside here
}
Run Code Online (Sandbox Code Playgroud) 好的,在c#中我们有类似的东西:
public static string Destroy(this string s) {
return "";
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,当你有一个字符串,你可以做:
str = "This is my string to be destroyed";
newstr = str.Destroy()
# instead of
newstr = Destroy(str)
Run Code Online (Sandbox Code Playgroud)
现在这很酷,因为在我看来它更具可读性.python有类似的东西吗?我的意思是代替写这样的:
x = SomeClass()
div = x.getMyDiv()
span = x.FirstChild(x.FirstChild(div)) # so instead of this
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
在试图弄清楚BeautifulSoup是如何工作的时候,我顺便学会了这个__str__方法(我是python的新手).因此,如果我没有误解,那么该__str__方法有助于塑造如何打印出来的类的表示方式.例如:
class Foo:
def __str__(self):
return "bar"
>>> x = Foo()
>>> print x
bar
Run Code Online (Sandbox Code Playgroud)
对?所以断言我是对的,是否有可能覆盖__str__字典列表的方法?我的意思是说在Foo课你有:
class Foo:
def __init__(self):
self.l = [{"Susan": ("Boyle", 50, "alive")}, {"Albert": ("Speer", 106, "dead")}]
Run Code Online (Sandbox Code Playgroud)
现在有可能得到以下结果吗?
>>> x = Foo()
>>> print x.l
"Susan Boyle is 50 and alive. Albert Speer is 106 and dead."
Run Code Online (Sandbox Code Playgroud)
编辑
考虑到agf的解决方案,如何再次访问字典?我的意思是,如果我定义__str__方法然后显然我应该定义其他东西来检索字典.请考虑以下示例:
class PClass(dict):
def __str__(self):
# code to return the result that I want
class Foo:
def __init__(self):
self.l = PClass({"Susan": …Run Code Online (Sandbox Code Playgroud) 在编写pdo语句时,是否可以重复变量的值?我的意思是:
$query = "UPDATE users SET firstname = :name WHERE firstname = :name";
$stmt = $dbh -> prepare($query);
$stmt -> execute(array(":name" => "Jackie"));
Run Code Online (Sandbox Code Playgroud)
请注意,我重复":name"名称,而我只提供一次值.我怎样才能做到这一点?
我有以下结构:
Controller.cs
public ActionResult PageMain(string param)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
PageMain.cs
namespace project1.Models
{
public class PageMain
{
public DataTable dtable
{
get {
// some code that returns a DataTable
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后在视图中:
@using project1.Models
@model PageMain
var datatable = Model.dtable // but this is throwing an error since the model is null
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我的模型返回null?如何访问PageMain.cs中的数据表?我是MVC的新手,所以如果我在结构中有任何逻辑错误,请不要犹豫警告我:)
虽然这在C#中是可能的:(在这个例子中,User是一个L2S类)
User user = // function to get user
Session["User"] = user;
Run Code Online (Sandbox Code Playgroud)
为什么这不可能?
User user = // function to get user
HttpCookie cookie = new HttpCookie();
cookie.Value = user;
Run Code Online (Sandbox Code Playgroud)
怎么做呢?我不想将用户的id存储在cookie中,然后进行一些验证.
顺便说一句,如果可能的话,将对象存储在cookie中而不仅仅是ID是安全的吗?
假设您在页面中有人员A列表和人员B列表.这两个是L2S中的单独类,代表两个不同的表.因此,您无法传递单个模型,如下所示:
...
@model PeopleA
...
@foreach(var peopleA in Model.People) ...
@foreach(var peopleB in //what?)
Run Code Online (Sandbox Code Playgroud)
因此,我想,我有三个选择可以遵循.
RenderAction帮助程序传递模型.因为我只会使用这些部分视图一次此选项似乎不吸引我.ModelMyPage.cs
public List<PeopleA> peopleA { get; set; }
public List<PeopleB> peopleB { get; set; }
Run Code Online (Sandbox Code Playgroud)
MyController.cs
...
ModelMyPage m = new ModelMyPage();
m.peopleA = // query
m.peopleB = // another query
return(m);
Run Code Online (Sandbox Code Playgroud)
你明白了.这是完成我的任务的有效方法还是有更好的c#方式来做我想要的?