[英]Combine n elements in first half of list with elements in other half of a list, if number of elements in a list is greater than 2
I have a problem with dealing with elements in a list.我在处理列表中的元素时遇到问题。 To be precise in a list of lists.准确地说是在列表列表中。 For example, I have list of elements that are read from a file:例如,我有从文件中读取的元素列表:
list_1 = [['void', None], ['uint8', 'f_MbistTestType_u8'], ['uint8', 'uint32', 'f_MbistTestType_u8', 'f_chip_id_u32'], ['void', None], ['void', None], ['void', None], ['void', None]]
In this case third element has more than two elements.在这种情况下,第三个元素有两个以上的元素。 I want to switch element 2 with element 3. So it would look like this:我想用元素 3 切换元素 2。所以它看起来像这样:
list_1[2] = ['uint8', 'f_MbistTestType_u8', 'uint32', 'f_chip_id_u32']
If there would be 6 elements ie如果有 6 个元素,即
list_example = ['uint8', 'uint32', 'void', 'f_chip_id_u32', 'f_MbistTestType_u8', None]
After the operation it should look like this:操作后应该是这样的:
list_example_sorted = ['uint8', 'f_chip_id_u32', 'uint32', 'f_MbistTestType_u8', 'void', None]
Right now I know how to get those elements in case I have only one occurrence of more than 2 elements, but don't know how to switch their places and also what to do in case I have more than one occurrence:现在我知道如何获取这些元素,以防我只出现一次超过 2 个元素,但不知道如何切换它们的位置,也不知道如果出现不止一次该怎么做:
for elements in list_1:
print(elements)
if len(elements) > 2:
list_el = elements
print(list_el)
I tried to pop them out and append, but it won't scale well with more than 4 elements.我试图弹出它们和 append,但它不能很好地扩展超过 4 个元素。
I tried to use swap function, but it seems that it doesn't work or I used it wrong?我试过用swap function,但是好像不行还是我用错了?
Going by an input of [1, 1, 1, 2, 2, 2]
with the desired output [1, 2, 1, 2, 1, 2]
, ie you want the first element of the left half followed by the first element of the right half and so forth.通过输入[1, 1, 1, 2, 2, 2]
和所需的 output [1, 2, 1, 2, 1, 2]
,即您想要左半部分的第一个元素,然后是第一个右半部分的元素等等。 To make it more obvious:为了使它更明显:
input = [1, 2, 3, 4, 5, 6]
output = [1, 4, 2, 5, 3, 6]
Define a function combine_inplace
that combines the ith element of the left half with the ith element of the right half of l
:定义一个 function combine_inplace
,它将左半部分的第 i 个元素与l
右半部分的第 i 个元素组合在一起:
def combine_inplace(l):
mid = len(l) // 2
ptr = 0
for left, right in zip(l[:mid], l[mid:]):
l[ptr], l[ptr+1] = left, right
# Increment pointer ptr by 2 for the next combination
ptr += 2
combine_inplace
mutates the passed list l
combine_inplace
改变传递的列表l
zip
to iterate over both list使用zip
遍历两个列表ptr
by 2 to get to the next list indices for l
将ptr
递增 2 以获得l
的下一个列表索引If you don't want to mutate the list itself you can instead create a new list combined
that is returned by the function combine
:如果您不想改变列表本身,您可以创建一个由 function combine
返回的新combined
列表:
def combine(l):
mid = len(l) // 2
combined = []
for left, right in zip(l[:mid], l[mid:]):
combined.extend((left, right))
return combined
l
不改变传递的列表l
combined
to store the combined values初始化combined
的空列表以存储组合值zip
to iterate over both list halves使用zip
遍历列表的两半combined
返回combined
列表This uses the same logic as combine_inplace
but you keep the original list intact.这使用与combine_inplace
相同的逻辑,但您保持原始列表不变。
Both functions combine the elements of the left half with the right half of a given list.这两个函数都将给定列表的左半部分和右半部分的元素组合在一起。 The only difference is that with combine
you have to store the returned list in a variable to access it.唯一的区别是使用combine
你必须将返回的列表存储在一个变量中才能访问它。
>> l = [1, 1, 1, 2, 2, 2]
>> combine_inplace(l)
>> print(l)
[1, 2, 1, 2, 1, 2]
>> input_list = [1, 2, 3, 4, 5, 6]
>> output_list = combine(input_list)
>> print(output_list)
[1, 4, 2, 5, 3, 6]
Now using either combine
or combine_inplace
to combine elements of lists with a length > 2
inside a list:现在使用combine
或combine_inplace
将列表中length > 2
的元素组合到列表中:
ll = [[1, 2], [1, 2], [1, 1, 2, 2], [1, 2], [1, 2, 3, 4, 5, 6]]
# Non-destructive way using combine to create a new list comb_ll
comb_ll = []
for el in ll:
if len(el) > 2:
el = combine(el)
comb_ll.append(el)
# Mutates the original list
for i in range(len(ll)):
if len(ll[i]) > 2:
combine_inplace(ll[i])
In both cases you'll get the same result:在这两种情况下,您都会得到相同的结果:
>> print(comb_ll)
[[1, 2], [1, 2], [1, 2, 1, 2], [1, 2], [1, 4, 2, 5, 3, 6]]
>> print(ll)
[[1, 2], [1, 2], [1, 2, 1, 2], [1, 2], [1, 4, 2, 5, 3, 6]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.