简体   繁体   English

用另一个反向扩展列表的最有效方法是什么?

[英]What is the most pythonic way to extend a list with the reversal of another?

I have one list that I want to take a slice of, reverse that slice and append each of those items onto the end of another list. 我有一个清单,我想切成薄片,将其切开,然后将这些项目的每一个附加到另一个列表的末尾。 The following are the options I have thought of (although if you have others please share), which of these is the most pythonic? 以下是我想到的选项(尽管如果您还有其他人也可以分享),那么哪个选项最适合python?

# Option 1
tmp = color[-bits:]
tmp.reverse()
my_list.extend(tmp)

# Option 2
my_list.extend(list(reversed(color[-bits:])))

# Option 3
my_list.extend((color[-bits:])[::-1])

I like 我喜欢

my_list.extend(reversed(color[-bits:]))

It explains what you are doing ( extending a list by reverse of another list's slice) and is short too. 它说明了您在做什么(通过反向扩展另一个列表的切片来扩展列表),并且它也很简短。

and a obligatory itertools solution 和强制性的itertools解决方案

my_list.extend( itertools.islice( reversed(color), 0, bits))
my_list.extend(color[:-(bits + 1):-1])

For Option #2, you can cut out the call to list . 对于选项2,您可以剪切对list的呼叫。 You could also use += instead of extend , like so: 您也可以使用+=代替extend ,如下所示:

my_list += reversed(color[-bits:])

暂无
暂无

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

相关问题 将列表中的值用作另一个列表的索引的最pythonic方法 - most pythonic way to use a value from a list as an index to another list 列表的另一个合并列表,但大多数pythonic方式 - Yet another merging list of lists, but most pythonic way 以最pythonic的方式将列表列表复制到另一个列表的一部分中 - Copying a list of lists into part of another one in the most pythonic way 在条件满足之前迭代列表的最pythonic方法是什么? - What is the most pythonic way to iterate over a list until a condition is met? 从数字中筛选列表的最pythonic方法是什么? - What is the most pythonic way to filter list from number? 什么是确保列表中所有元素不同的最pythonic方法? - What's the most pythonic way to ensure that all elements of a list are different? 什么是将字符串拆分为连续,重叠的单词列表的最pythonic方法 - What is the most pythonic way to split a string into contiguous, overlapping list of words 将 function 应用于列表的某些项目的最pythonic方法是什么? - What is the most pythonic way to apply function to some items of a list? 忽略带有括号的列表中的单词的最有效(pythonic)方法是什么? - What is the most efficient (pythonic) way to ignore words in a list that has parantheses? 从 OrderedDict 列表中查找项目的最pythonic 方法是什么? - what would be the most pythonic way of finding items from a list of OrderedDict?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM