如何将单元格与数字链接?Python3(我尝试了zip函数和其他方法)

如何将单元格与数字链接?Python3(我尝试了zip函数和其他方法),python,algorithm,python-3.x,python-2.7,sorting,Python,Algorithm,Python 3.x,Python 2.7,Sorting,我试图以升序将每个零替换为特定的奇数。例如:我有一个原始的列表[[0],[0,0],[0,0,0]等等,我需要得到这个:[[1],[3,5],[7,9,11],[13,15,17,19]等等] def x(n): obj = iter(list(range(1, 100, 2))) odds = list(range(1, 100, 2)) a =[y*[0] for y,x in enumerate(odds,1)] for i in range(0,50):

我试图以升序将每个零替换为特定的奇数。例如:我有一个原始的列表[[0],[0,0],[0,0,0]等等,我需要得到这个:[[1],[3,5],[7,9,11],[13,15,17,19]等等]

def x(n):
    obj = iter(list(range(1, 100, 2)))
    odds = list(range(1, 100, 2))
    a =[y*[0] for y,x in enumerate(odds,1)]
    for i in range(0,50):
        a[i] = next(obj)
    return odds
print(x(10))

我需要一个小提示。如何继续/修复此代码。

您只需执行以下简单操作:

zeroes = [[0], [0, 0], [0,0,0], [0,0,0,0]]

count = 1
for lst in zeroes:
    for i in range(len(lst)):
        lst[i] = count
        count += 2

print(zeroes)
# [[1], [3, 5], [7, 9, 11], [13, 15, 17, 19]]
它只是根据递增为2的计数器,用奇数更新零的嵌套列表

您还可以执行更复杂的操作,如使用迭代器和itertools函数:

import itertools

def x(n):
    # get the zeroes sublists
    zeroes = [i * [0] for i in range(1, n+1)]

    # get the length of the flattened list above
    odd_len = len(list(itertools.chain.from_iterable(zeroes)))

    # get the odd numbers
    odd_numbers = [i for i in range(1, odd_len * 2, 2)]

    # check they are the same length
    assert(len(odd_numbers) == odd_len)

    # convert to iterator
    odd_numbers = iter(odd_numbers)

    # loop over the zeroes
    for lst in zeroes:

        for i in range(len(lst)):

            # update the zeroes with the odd numbers
            lst[i] = next(odd_numbers)

    return zeroes

>>> print(x(4))
[[1], [3, 5], [7, 9, 11], [13, 15, 17, 19]]

>>> print(x(10))
[[1], [3, 5], [7, 9, 11], [13, 15, 17, 19], [21, 23, 25, 27, 29], [31, 33, 35, 37, 39, 41], [43, 45, 47, 49, 51, 53, 55], [57, 59, 61, 63, 65, 67, 69, 71], [73, 75, 77, 79, 81, 83, 85, 87, 89], [91, 93, 95, 97, 99, 101, 103, 105, 107, 109]]

因为你试图使用迭代器

#this gives you your zero lists...
def zero(n):
    return [[0] * i for i in range(1, n)]

z = zero(5) # this gives the same input as what you show in your example

x = iter(range(1, 1000, 2)) # you should calc and be sure 1000 is big enough
replacement = [[next(x) for i in chunk if i is 0] for chunk in z]
这使用了一种叫做列表理解的方法。函数使用列表理解创建初始列表,最后一行使用它创建最终答案。稍后,我将添加第二个更简单的示例

以下是更简单的解决方案:

def xx(ll):
    obj = iter(range(1, 1000, 2))
    for chunk in ll:
        for i in range(len(chunk)):
            if chunk[i] is 0:
                chunk[i] = next(obj)
    return ll

def zero(n):
    return [[0] * i for i in range(1, n)]

zeros = zero(5)
print(zeros)
result = xx(zeros)
print(result)
以及输出:

[[0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[[1], [3, 5], [7, 9, 11], [13, 15, 17, 19]]
同样,xx函数使用迭代器提取奇数。我这么做是因为我猜这是你想看到的。从那里,我一步一步地浏览主列表的每一块,这是另一个较小的列表。对于每个较小的列表,我检查该值是否为零,如果为零,则用迭代器对象中的下一个值替换它。最后,将返回更新后的主列表


顺便说一句,这个实现的一个好处是,如果输入的主列表中不只包含零,那么这个代码将只替换零,这是我一开始以为你会用到的地方,直到我看到您的输入数据集是多么简单。

这里有一个解决方案,可以处理任意深度的列表列表…s。它使用itertools.count、列表理解和递归

import random
import itertools

def create(minchld = 2, mxchld = 4, p0 = 0.5, mxdpth = 4):
    return [0 if mxdpth==0 or random.random() < p0 else create(minchld, mxchld, p0, mxdpth-1)
            for i in range(random.randint(minchld, mxchld))]

def fill(zeros):
    odd = itertools.count(start=1, step=2)
    def replace(zeros):
        return [next(odd) if i==0 else replace(i) for i in zeros]
    return replace(zeros)

zeros = create()
print(zeros)
print(fill(zeros))

我在这里没有看到任何[[0]、[0,0]、[0,0,0]等]输入。。。那么问题是关于什么的?非常感谢!这个简单的方法非常有趣:谢谢。
# [[0, 0, [0, 0, 0]], 0, [0, 0, [[0, 0, [0, 0]], [[0, 0, 0], 0, 0], 0, 0], 0], 0]
# [[1, 3, [5, 7, 9]], 11, [13, 15, [[17, 19, [21, 23]], [[25, 27, 29], 31, 33], 35, 37], 39], 41]

# [0, [0, [0, [0, 0]]]]
# [1, [3, [5, [7, 9]]]]

# [[[0, [0, [0, 0]], [[0, 0, 0, 0], 0], [[0, 0], 0]], 0, 0, [[[0, 0, 0], [0, 0, 0, 0], [0, 0, 0], [0, 0, 0]], 0, 0]], 0]
# [[[1, [3, [5, 7]], [[9, 11, 13, 15], 17], [[19, 21], 23]], 25, 27, [[[29, 31, 33], [35, 37, 39, 41], [43, 45, 47], [49, 51, 53]], 55, 57]], 59]