我正在寻找能够获取[a,b,c,d]等列表的函数,并输出交换相邻索引的所有排列的列表,即[[b,a,c, d],[a,c,b,d],[a,b,d,c],[d,b,c,a]]
由于
答案 0 :(得分:1)
简单来说,您只需使用 The class of the view I am wanting to use drop down list in:
namespace Fake.Models
{
public class Fake
{
public int Id { get; set; }
public List<SelectList> Categories { get; set; }
}
}
Model of the Category class pulling from database:
namespace Fake.Models
{
public class Categories
{
public int Id { get; set; }
public string CategoryName { get; set; }
}
}
controller of the view intended to use dropdown list:
namespace Fake.Controllers
{
public class FakeController : Controller
{
FakeDb _db = new FakeDb();
public ActionResult Index()
{
var model = from a in _db.Fake1 orderby a.Date ascending select a;
ViewBag.Categories = new SelectList(_db.Categories, "Id", "CategoryName");
return View(model);
}
The view I am trying to get dropdown list to work in:
@model fakename.Models.Modelname
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group col-md-offset-2 col-md-10">
@Html.DropDownList("Categories", "Select Category")
// Omitted Info not related
</div>
}
循环并交换相邻的项目,for
将生成浅层副本,并且不会更改原始列表tmp=l[:]
。
查看What exactly is the difference between shallow copy, deepcopy and normal assignment operation?的更多详情:
l
结果:
l=['a', 'b', 'c', 'd']
for i in range(len(l)):
tmp=l[:]
tmp[i],tmp[i-1]=tmp[i-1],tmp[i]
print tmp