将所有嵌套列表调整为相同的长度 [英] Adjust all nested lists to the same length

查看:172
本文介绍了将所有嵌套列表调整为相同的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这个特定答案使嵌套列表的长度相同.由于我还没有发表评论的权限,并且回答该主题的问题将违反规则,因此我要提出一个新问题.

I am referring to this this specific answer Making nested lists same length. Since I don't have the permissions to comment yet and answering with a question to that topic would violate the rules, I am asking a new question.

我不完全理解答案. 在我的理解中,for循环中的迭代器row通常是一个整数值,它对myList中的每个元素进行迭代.那么,由于len(row)只是一个整数,怎么可能将其用作条件的一部分?有什么我想念的吗?

I don't fully understand the answer. In my understanding the iterator row in the for-loop is usually an integer value which iterates over each element in myList. So how is it possible to use len(row) as part of the condition since it is just an integer? Is there something I am missing?

我已尝试将此解决方案应用于我的代码,但正如预期的那样,我收到一条错误消息

I have tried to apply this solution to my code but as expected I receive an error saying

TypeError: object of type 'int' has no len()
 args = ("object of type 'int' has no len()",)
 with_traceback = <built-in method with_traceback of TypeError object> 

引用此行

row.extend(['null'*(len(maxSS7) - len(row))])

另外,我不理解将.extendrow一起使用,它是迭代器而不是列表.

Further I don't understand the use of .extend with row which is the iterator and not a list.

这是答案的相关部分.

maxLen = max(map(len, myList))
for row in myList:
    if len(row) < maxLen:
        row.extend(...)

一个简短的演练将不胜感激.

A brief walkthrough would be greatly appreciated.

或者也许有更好的方法将所有嵌套列表的长度调整为相同的长度.

Or maybe there is a better way to adjust the lengths of all nested list to same length.

推荐答案

好,让我们逐行浏览.我个人认为map在Python中不是很惯用,所以我会这样写:

Ok, lets go through it line by line. Personally I don't think map is very idiomatic in Python so I would write this:

maxLen = max(map(len, myList))

作为生成器表达式:

max_len = max(len(item) for item in my_list)

第二个版本几乎是英文:max_lenmy_list中每个项目的长度中的最大值.

The second version is almost plain English: let max_len be the maximum value among the length of each item in my_list.

了解Python内容的最好方法是启动REPL并尝试.因此,如果您将my_list作为列表列表:

The best way to understand something in Python is just fire up the REPL and try it. So if you have my_list as a list of lists:

my_list = [[1], [1, 2], [1, 2, 3]]

以上内容将使您获得最大商品的长度:3

The above will get you the length of the largest item: 3

现在,您要使每个项目都具有相同的大小.你该怎么做?一种方法是将None项目附加到它.对于列表中的每个项目,您要测试项目的长度是否小于列表中最大的项目,并且它几乎是普通的英语:

Now you want to make every item the same size. How can you do that? One way is to append None items to it. For each item in the list, you test if the length of the item is smaller then the largest item in the list and it is almost plain English:

for item in list:                # for each item in the list
    while len(item) < max_len:   # while the item length is smaller than 3
        item.append(None)        # append None to the item

您可能需要做一些过早的优化,然后一次调用extend而不是多次调用append,因为您认为这样会提高性能(但是除非您同时分析了两者,否则您无法真正看出来解决方案):

You may want to do a bit of premature optimization and call extend once instead of calling append several times because you think performance will be better this way (but you can't really tell it unless you have profiled both solutions):

for item in list:
    if len(item) < max_len:
        item.extend([None] * (max_len - len(item)))

现在这是怎么回事?在Python中,list + list连接list的两个副本,并且list * 3list + list + list相同.因此,在for循环的第一次迭代中,item[1]len(item)是1,而max_len - len(item)3 - 1.最后,[None] * 2[None, None],因此在调用extend之后,第一项将是[1, None, None].第二个项目的内容相同,长度为2、3减2为1,最终结果为[1, 2, None].第三项的长度与max_len(3)相同,因此if条件为false.结果将是:

Now what is going on here? In Python, list + list concatenates two copies of list, and list * 3 is the same as list + list + list. So in the first iteration of the for loop, item is [1], len(item) is 1 and max_len - len(item) is 3 - 1. Finally, [None] * 2 is [None, None] so after the call to extend the first item will be [1, None, None]. Same thing for the second item, its length is 2, 3 minus 2 is one and it will end up as [1, 2, None]. The 3rd item has the same length as max_len (3) so the if condition is false. The result will be:

[[1, None, None], [1, 2, None], [1, 2, 3]]

列表列表中的所有列表现在都具有相同的大小3.为了完整起见,对于这么小的列表,extend版本比append版本快不到1微秒,因此几乎不值得麻烦(在Mac上运行的Python 3.6中为1.64 µs,而1.7 µs).

All lists in the list of lists now have the same size, 3. For the sake of completion, for such a small list the extend version is less than 1 microsecond faster than the append one so barely worth the trouble (1.64 µs versus 1.7 µs in Python 3.6 running on my Mac).

这篇关于将所有嵌套列表调整为相同的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆