简体   繁体   English

Python中列表列表的列表

[英]A list of lists of lists in Python

I'm working with the Flask framework in Python, and need to hand off a list of lists to a renderer. 我正在使用Python中的Flask框架,需要将列表列表传递给渲染器。

I step through a loop and create a list, sort it, append it to another list, then call the render function with the masterlist, like so: 我遍历一个循环并创建一个列表,对其进行排序,将其追加到另一个列表,然后使用主列表调用render函数,如下所示:

for itemID in itemsArray:
    avgQuantity = getJitaQuantity(itemID)
    lowestJitaSell = getJitaLowest(itemID)
    candidateArray = findLowestPrices(itemID, lowestJitaSell, candidateArray, avgQuantity)
    candidateArray.sort()
    multiCandidateArray.append(candidateArray)
renderPage(multiCandidateArray)

My problem is that I need to clear the candidateArray and create a new one each time through the loop, but it looks like the candidateArray that I append to the multiCandidateArray is actually a pointer, not the values themselves. 我的问题是,每次循环时我都需要清除候选数组并创建一个新数组,但是看起来我附加到multiCandidateArray的候选数组实际上是一个指针,而不是值本身。

When I do this: 当我这样做时:

for itemID in itemsArray:
    avgQuantity = getJitaQuantity(itemID)
    lowestJitaSell = getJitaLowest(itemID)
    candidateArray = findLowestPrices(itemID, lowestJitaSell, candidateArray, avgQuantity)
    candidateArray.sort()
    multiCandidateArray.append(candidateArray)
    **del candidateArray[:]**
renderPage(multiCandidateArray)

I end up with no values. 我最终没有价值。

Is there a way to handle this situation that I'm missing? 有什么办法可以解决我所缺少的这种情况?

I would probably go with something like: 我可能会喜欢这样的东西:

for itemID in itemsArray:
    avgQuantity = getJitaQuantity(itemID)
    lowestJitaSell = getJitaLowest(itemID)
    candidateArray = findLowestPrices(itemID, lowestJitaSell, candidateArray, avgQuantity)
    multiCandidateArray.append(sorted(candidateArray))

No need to del anything here, and sorted returns a new list , so even if FindLowestPrices is for some reason returning references to the same list (which is unlikely), then you'll still have unique lists in the multiCandidateArray (although your unique lists could hold references to the same objects). 无需在此处del任何内容,并且sorted返回一个新list ,因此,即使因为某种原因FindLowestPrices返回对同一列表的引用(这不太可能),但在multiCandidateArray仍然会有唯一列表(尽管唯一列表)可以包含对相同对象的引用)。

Your code already creates a new one each time through the loop. 您的代码每次循环都已经创建了一个新代码。

candidateArray = findLowestPrices(...)

This assigns a new list to the variable, candidateArray . 这会将新列表分配给变量candidateArray数组。 It should work fine. 它应该工作正常。

When you do this: 执行此操作时:

del candidateArray[:]

...you're deleting the contents of the same list you just appended to the master list. ...您正在删除刚添加到主列表的同一列表的内容。

Don't think about pointers or variables; 不要考虑指针或变量; just think about objects, and remember nothing in Python is ever implicitly copied. 只考虑对象,切记Python中没有任何内容被隐式复制。 A list is an object. 列表是一个对象。 At the end of the loop, candidateArray names the same list object as multiCandidateArray[-1] . 在循环结束时, candidateArray数组与multiCandidateArray[-1]命名相同的列表对象。 They're different names for the same thing. 它们是同一事物的不同名称 On the next run through the loop, candidateArray becomes a name for a new list as produced by findLowestPrices , and the list at the end of the master list is unaffected. 在下一个循环中, candidateArray数组将成为由findLowestPrices生成的列表的名称,并且主列表末尾的列表不受影响。

I've written about this before ; 我以前已经写过这本书 ; the C way of thinking about variables as being predetermined blocks of memory just doesn't apply to Python at all. 将变量视为预定的内存块的C语言方式根本不适用于Python。 Names are moved onto values, rather than values being copied into some fixed number of buckets. 名称会移到值上,而不是将值复制到固定数量的存储桶中。

(Also, nitpicking, but Python code generally uses under_scores and doesn't bother with types in names unless it's really ambiguous. So you might have candidates and multi_candidates . Definitely don't call anything an "array", since there's an array module in the standard library that does something different and generally not too useful. :)) (此外,挑剔,但是Python代码通常使用under_scores ,并且不会打扰名称中的类型,除非它确实模棱两可。因此,您可能会有candidatesmulti_candidates 。绝对不要将任何东西称为“数组”,因为在其中有一个array模块标准库做了一些不同的事情,通常不太有用。:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM