我有两个像这样的列表:
[[0,3,5],['cat','dog','chicken'], [15,14,11]]
[[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
我如何创建一个列表列表:
[[0,1,2,3,4,5], ['cat', 'whale', 'rabbit','dog','zebra','chicken'], [15,10,9,14,22,11]]
修改 只是为了澄清一下,我需要什么输出 - 按第一个列表排序的列表列表。
答案 0 :(得分:0)
这是怎么回事。按照你想要的方式工作。逻辑是,在第一次迭代中,项目从“l1”然后l2
中获取。在另外两个中,l2
后跟l1
:
l1=[[0,3,5],['cat','dog','chicken'], [15,14,11]]
l2=[[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
l3=[[],[],[]]
for i in range (3):
for j in range(3):
if(j==0):
l3[i].append(l1[i][j])
l3[i].append(l2[i][j])
else:
l3[i].append(l2[i][j])
l3[i].append(l1[i][j])
print(l3)
输出:[[0,1,2,3,4,5], ['cat', 'whale', 'rabbit','dog','zebra','chicken'], [15,10,9,14,22,11]]
行动准则:http://ideone.com/jIlAeK
答案 1 :(得分:0)
这应该可以解决问题:
// getting the image file from the stream and saving to server
HttpPostedFile f1 = (HttpPostedFile)Session["f1"];
int nFileLen = f1.ContentLength;
byte[] myData = new byte[nFileLen];
f1.InputStream.Read(myData, 0, nFileLen);
if (f1.ContentLength != 0)
{
string filename = f1.FileName;
string extention = Path.GetExtension(filename);
if (extention.ToLower() == ".jpg" | extention.ToLower().ToLower() == ".png" | extention.ToLower().ToLower() == ".jpeg")
{
filename = txtMobile.Text;
int width = Convert.ToInt32(ConfigurationManager.AppSettings["ProfilePicWidth"]);
string Destination = Convert.ToString(Server.MapPath("../Images/Members/" + filename + extention));
CommanFunctions.ResizeImage(width, f1.InputStream, Destination);
regi.ProfilePic = Destination;
}
else
{
lblError.Text = "Please Upload a valid Image";
multiview.ActiveViewIndex = 1;
}
}
说明:
In [30]: l1 = [[0,3,5],['cat','dog','chicken'], [15,14,11]]
In [31]: l2 = [[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
In [32]: from operator import itemgetter
In [33]: import itertools
In [37]: list(zip(*sorted(itertools.chain(zip(*l1), zip(*l2)), key=itemgetter(0))))
Out[37]:
[(0, 1, 2, 3, 4, 5),
('cat', 'whale', 'rabbit', 'dog', 'zebra', 'chicken'),
(15, 10, 9, 14, 22, 11)]
In [39]: list(map(list, zip(*sorted(itertools.chain(zip(*l1), zip(*l2)), key=itemgetter(0)))))
Out[39]:
[[0, 1, 2, 3, 4, 5],
['cat', 'whale', 'rabbit', 'dog', 'zebra', 'chicken'],
[15, 10, 9, 14, 22, 11]]
将您的列表收集到元组列表中。每个元组包含三个元素:zip(*l)
。例如:
(id, name, some_integer_field)
元组应按第一个元素排序 - 这就是In [35]: list(itertools.chain(zip(*l1), zip(*l2)))
Out[35]:
[(0, 'cat', 15),
(3, 'dog', 14),
(5, 'chicken', 11),
(1, 'whale', 10),
(2, 'rabbit', 9),
(4, 'zebra', 22)]
然后它应该被收集到单个列表中。另一次致电sorted(..., key=operator.itemgetter(0))
。
不幸的是,zip
会返回一个元组列表。当您需要列表列表时,您应该将zip
的结果打包到zip
答案 2 :(得分:0)
使用列表压缩,您只需要1行代码
aa = [[0,3,5],['cat','dog','chicken'],[15,14,11]]
bb = [[1,2,4],['whale','rabbit','zebra'],[10,9,22]]
new_list = [a + b代表a,b代表zip(aa,bb)]
打印new_list
[[0,3,5,1,2,4],['猫','狗','鸡','鲸','兔子','斑马'],[15,14,11] ,10,9,22]]
答案 3 :(得分:0)
使用列表中的第一个条目作为排序索引,以下脚本将为您提供预期的输出:
list_1 = [[0, 3, 5], ['cat','dog','chicken'], [15, 14, 11]]
list_2 = [[1, 2, 4], ['whale', 'rabbit', 'zebra'], [10, 9, 22]]
sort_order = list_1[0] + list_2[0]
output = []
for i1, i2 in zip(list_1, list_2):
source = i1 + i2
entry = [None] * len(source)
for i, v in enumerate(source):
entry[sort_order[i]] = v
output.append(entry)
print output
这将显示以下内容:
[[0, 1, 2, 3, 4, 5], ['cat', 'whale', 'rabbit', 'dog', 'zebra', 'chicken'], [15, 10, 9, 14, 22, 11]]
答案 4 :(得分:0)
只需要几行:
>>> a = [[0,3,5],['cat','dog','chicken'], [15,14,11]]
>>> b = [[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
>>> the_list = [c+d for c, d, in zip(a,b)]
>>> print the_list
[[0, 3, 5, 1, 2, 4], ['cat', 'dog', 'chicken', 'whale', 'rabbit', 'zebra'], [15, 14, 11, 10, 9, 22]]
>>> the_list[0].sort()
>>> print the_list
[[0, 1, 2, 3, 4, 5], ['cat', 'dog', 'chicken', 'whale', 'rabbit', 'zebra'], [15, 14, 11, 10, 9, 22]]
答案 5 :(得分:0)
此解决方案适用于顶级列表中未指定数量的列表。合并两个输入列表后,它们将根据第一个列表值进行排序:
a = [[0,3,5],['cat','dog','chicken'], [15,14,11]]
b = [[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
combined = list(zip(*a)) + list(zip(*b)) # create tuples with an element from each sublist then merge them
ordered_tuples = sorted(combined) # or more explicitly: sorted(combined, key=lambda x: x[0])
output = list(zip(*ordered_tuples)) # this reverses the sorted tuples into lists as requested
答案 6 :(得分:-2)
查看此脚本,它对我有用
list_1 = [[0,3,5],['cat','dog','chicken'], [15,14,11]]
list_2 = [[1,2,4],['whale', 'rabbit', 'zebra'], [10,9,22]]
list_3 = []
for i in range(len(list_1)):
list_3.append(list_1[i] + list_2[i])
print(list_3)