简体   繁体   English

如何在嵌套列表python中旋转索引[1]处的数字

[英]How to rotate number at index[1] in a nested list python

I am working on a problem that need me rotate the numbers at index[1] in a nested loop once toward the right.我正在解决一个问题,需要我在嵌套循环中将索引 [1] 处的数字向右旋转一次。 The nested loop looks like this [(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)] and i want the nested loop to look like this [(1, 5), (2, 1), (3, 2), (4, 3), (1, 4)] (All the number at index[1] moved once to the right) I want to do this at a range of N times.嵌套循环看起来像这样[(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)]我希望嵌套循环看起来像这样[(1, 5), (2, 1), (3, 2), (4, 3), (1, 4)] (索引[1]处的所有数字都向右移动一次)我想在一个N次范围。 I tried using deque from the module collections but it didn't do the thing i wanted.我尝试使用模块集合中的 deque,但它没有做我想要的事情。 Here are my code.这是我的代码。

from collections import deque
n = int(input())
a = [int(i) for i in input().split()]
s, f = map(int, input().split())
num = 0

sequence = [i for i in range(1, len(a) + 1)]
res = list(zip(a, sequence))
print(res)
for i in res:
    res1 = deque(i[1])
    res1.rotate(1)
    print(res1)

I got an error on line 11我在第 11 行出错

   res1 = deque(i[1])
TypeError: 'int' object is not iterable

Could separate them into firsts and seconds, shift the seconds, and recombine:可以将它们分为第一和秒,移动秒,然后重新组合:

a, b = zip(*lst)
lst = [*zip(a, b[-1:] + b[:-1])]

or just要不就

a, b = zip(*lst)
lst = [*zip(a, b[-1:] + b)]

Use -n to rotate n steps (if n can be negative or longer than the list, take it modulo the length of the list first).使用-n旋转 n 步(如果n可以是负数或长于列表,则首先取列表长度的模)。

It could be a one-liner:它可能是单行的:

[(x[0], y[1]) for x, y in zip(l, l[-1:] + l)]

but Superb rain's answer is more explicit/pythonic但精湛的雨的答案更明确/pythonic

Just for the sake of it - Optimized solution:只是为了它 - 优化的解决方案:

from collections import deque
def rotate_second_indices(lst, n=1):
    assert n < len(lst)
    buffer = deque(lst[i][1] for i in range(-n,0))
    for index, (a, b) in enumerate(lst):
        lst[index] = (a, buffer.popleft())
        buffer.append(b)

Original answer原答案

That code should do it:该代码应该这样做:

lst = [(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)]
new = lst[-1][1]
for index, (first, second) in enumerate(lst):
    lst[index] = (first, new)
    new = second

Another option:另外一个选项:

lst = [(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)]
first_lst, second_lst = zip(*lst)
second_lst = list(second_lst)
second_lst.insert(0, second_lst.pop())
lst = list(zip(first_lst, second_lst))

And another option:还有另一个选择:

first, second = zip(*lst)
second = deque(second)
second.rotate()
lst = list(zip(first, second))

Rotate-by-n with O(1) extra space [*] using the old triple-reverse:使用旧的三重反转以 O(1) 额外空间[*]旋转 n:

a = [(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)]
n = 2

def reverse(start, stop):
    i, j = start, stop - 1
    while i < j:
        a[i], a[j] = (a[i][0], a[j][1]), (a[j][0], a[i][1])
        i += 1
        j -= 1
k = -n % len(a)
reverse(0, k)
reverse(k, len(a))
reverse(0, len(a))

print(a)

Output:输出:

[(1, 4), (2, 5), (3, 1), (4, 2), (1, 3)]

[*] : Except when there are other references to the tuples, in which case they can't go to the free list and become reused. [*] :除非有其他对元组的引用,在这种情况下,它们不能进入空闲列表并被重用。

You had a good start with collections.deque :您对collections.deque有一个良好的开端:

from collections import deque

f = [(1, 1), (2, 2), (3, 3), (4, 4), (1, 5)]

d = deque(map(lambda x, y: y, *zip(*f)))

d.rotate(1)

list(zip(map(lambda x, y: x, *zip(*f)), d))
[(1, 5), (2, 1), (3, 2), (4, 3), (1, 4)]

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

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