简体   繁体   English

如何设置新值以列出相同的数字多次出现?

[英]How to set new values to list, with same numbers occurring multiple times?

I'm trying to set new values based on an existing list with values rather randomly set.我正在尝试根据现有列表设置新值,而值是随机设置的。 Some values appear multiple times and the new index value should reflect that.有些值出现多次,新的索引值应该反映出来。 However, I seem unable to count a value that appears multiple times as 1, to then add to a counter ( startval ).但是,我似乎无法将多次出现的值计为 1,然后添加到计数器( startval )。 What would be a good way of doing this?这样做的好方法是什么?

startval = 901
old_index = [100, 145, 145, 740, 740, 740, 276, 277, 278]
# new_index = [901, 902, 902, 903, 903, 903, 904, 905, 906]
new_index = []

for x in old_index:
    new_index.append(startval)
    if old_index.count(x) != 1:
        for y in range(old_index.count(x)):
            startval *=1 #but add 1 once 
    else:
        startval +=1

You can use the old defaultdict size trick to get contiguous integer keys corresponding to a set of (repeating) values:您可以使用旧的defaultdict大小技巧来获取与一组(重复)值对应的连续 integer 键:

from collections import defaultdict

old_index = [100, 145, 145, 740, 740, 740, 276, 277, 278]

offset = 901
index = defaultdict(lambda: len(index) + offset)

new_index = [index[v] for v in old_index]
# [901, 902, 902, 903, 903, 903, 904, 905, 906]

If you only want the same index for neighbouring duplicates, you can go with itertools.groupby and enumerate :如果您只想为相邻的重复项使用相同的索引,则可以使用itertools.groupbyenumerate go :

from itertools import groupby

new_index = [i for i, (_, g) in enumerate(groupby(old_index), offset) for _ in g]
# [901, 902, 902, 903, 903, 903, 904, 905, 906]

Some documentation:一些文档:

暂无
暂无

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

相关问题 如何在列表上映射函数并将新值多次附加到同一个列表中? - How to map a function on a list and append the new values onto the same list multiple times? 如何循环一个系列并将值多次添加到新列表中? - How can loop a series and add to the values multiple times to a new list? 提取文件中多次出现的特定部分下的值 - Extract values under a particular section occurring multiple times in a file 使用列中设置的不同值多次运行相同的 Python 脚本 - Run same Python script multiple times with different values set in a column 获取加起来为数字“n”的数字列表(使用数字重复)——如何考虑多次使用相同数字的情况? - Get a list of numbers that adds up to a number "n" (using repetition of number) - how would one consider the case of using same number multiple times? 用相应的值替换Numpy数组中多个出现的元素的集合 - Replace set of multiple occurring elements in Numpy array with corresponding values 有没有办法消除在 Python 表达式中多次出现的相同 lambda 表达式的多次计算? - Is there a way to eliminate multiple computations of same lambda expression occurring multiple times in a Python expression? 如何多次运行相同的 function 并另存为新变量 - How to run same function multiple times and save as new variable Python:具有一个键和多个值的字典:如何获取具有相同SET值的键列表? - Python : Dictionary with one key and multiple values : How to get list of keys having same SET of values? 如果列表包含浮点数,如何在列表中选择相同的值 - How to pick same values in a list if the list contain floating numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM