连续数字列表...根据另一个列表中的值

时间:2018-04-02 14:41:51

标签: python list

我的目标是获取连续数字列表,并相应地重复初始列表值。让我们说:

var text1 = 'MPG_0023';
var text2 = 'MPG_23';

var regex = /(.*_[0]*)(\d*)/;

var match1 = regex.exec(text1);
var match2 = regex.exec(text2);

var newText1 = match1[1] + (Number(match1[2]) + 1);
var newText2 = match2[1] + (Number(match2[2]) + 1);

console.log(newText1);
console.log(newText2);

我想得到:

initialList=[1,2,3,5]

......我是Python的全新人,对不起这个 - 可能是第一步的问题。不幸的是,尝试了很多搜索,但结果并不符合我的需求。非常感谢你提前。

10 个答案:

答案 0 :(得分:7)

新手友好的解决方案是使用两个循环:

result = []
number = 0
for repeat in initialList:
    for _ in range(repeat):
        result.append(number)
    number += 1

print(result)  # [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

如果您因任何原因喜欢单行,您可以合并enumeraterange来获取

result = [num for num, repeat in enumerate(initialList) for _ in range(repeat)]

答案 1 :(得分:1)

您可以使用enumerate

initialList=[1,2,3,5]
final_result = [i for b in [[c]*d for c, d in enumerate(initialList)] for i in b]

输出:

[0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

答案 2 :(得分:1)

IMO,这是一个更易于维护的功能解决方案:

initialList = [1, 2, 3, 5]

def listify(x):
    return [x]

# create sub-lists [[0], [1], [2], [3], ...]
sublists = map(listify, range(len(initialList)))

# attach to each sub-list the repititions required [([0], 1), ([2], 2), ...]
sublists_with_rep_spec = zip(sublists, initialList)

# create repetitions based on initialList (using list multiplication)
sublists_with_repetitions = starmap(operator.mul, sublists_with_rep_spec)

# flatten everything out
result = chain.from_iterable(sublists_with_repetitions)

print(list(result))

请注意,这都是懒惰的(在python3上)所以一切都会发生"发生"只有在您实际拨打list

答案 3 :(得分:1)

以下是使用repeatchain.from_iterable

的另一种方法
from itertools import repeat, chain

list(chain.from_iterable((repeat(idx, num)) for idx, num in enumerate(initialList)))

[0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

答案 4 :(得分:0)

我的解决方案

>>> initialList=[1,2,3,5]
>>> sum(([num]*count for num, count in enumerate(initialList)), [])
[0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

答案 5 :(得分:0)

使用sumenumerate

的非常简单的解决方案
initialList = [1, 2, 3, 5]

targetList  = sum((times*[index] for index, times in enumerate(initialList)), [])

答案 6 :(得分:0)

如果您希望不再需要嵌套逻辑,可以通过itertools进行此操作。 itertools是标准库的一部分。

为了提高您对Python的理解,我建议您看一些嵌套列表推导的@Ajax1234's solution

from itertools import chain

initialList = [1,2,3,5]

targetList = list(chain.from_iterable([i]*j for i, j in enumerate(initialList)))

# [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

注意:如果您使用[i]*j,则可以将itertools.repeat(i, j)替换为numpy.repeat(i, j)numpy。前者可能更好,因为它很懒惰。

答案 7 :(得分:0)

另一种简单方法:

from functools import reduce

initialList = [1,2,3,5]
targetList = [[index]*item for index, item in enumerate(initialList)]
targetList = reduce(lambda x,y: x+y, targetList)
print(targetList)
# [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]

答案 8 :(得分:0)

您可以尝试这种方法:

data=[[i]*initialList[i] for i,j in enumerate(initialList)]

print([k for i in data for k in i])

为了好玩,我尝试使用lambda:

initialList=[1,2,3,5]

print(list(map(lambda x:[x]*initialList[x],range(0,len(initialList)))))

lambda结果在嵌套列表中。

答案 9 :(得分:-1)

我发现大多数当前答案要么性能差,要么很难阅读。实现此目的的另一种功能方法是使用itertoolschain.from_iterablerepeat这样的count函数:

from itertools import chain, count, repeat

initial_list = [1, 2, 3, 5]
result = list(chain.from_iterable(map(repeat, count(), initial_list)))
# [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3]