简体   繁体   English

更改列表的元素以匹配另一个列表

[英]Changing elements of a list to match another list

I am trying to iterate through a list of list and increase the first element in each sublist until it matches an integer on another list...我正在尝试遍历列表列表并增加每个子列表中的第一个元素,直到它与另一个列表中的整数匹配...

list1=[[2, 13, 22, 40], [8, 13, 22, 40], [24, 13, 22, 40]]
reference_list = [5, 10, 30]
for i in list1: 
    while (i[0] != i) in reference_list:    
        i[0] = i[0] + 1
print(list1)

Right now my output is:现在我的输出是:

[[2, 13, 22, 40], [8, 13, 22, 40], [24, 13, 22, 40]]

(Nothing is getting modified) My desired output is: (没有被修改)我想要的输出是:

[[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]

(so that the first integer is increased until it matches an integer on the reference_list) (这样第一个整数就会增加,直到它与 reference_list 上的整数匹配)

I have dabbled using the set function but I can't seem to get that to work.我已经尝试过使用 set 函数,但似乎无法让它发挥作用。

Any ideas are appreciated!任何想法表示赞赏!

This should work, you "while" condition was using the same iterator for two different lists这应该有效,您的“while”条件对两个不同的列表使用相同的迭代器

list1=[[2,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list=[5, 10, 30]
for i in list1:
    while i[0] not in reference_list:
        i[0]=i[0]+1
print(list1)

But as @GrzegorzBokota said, if the first element of list1 is bigger than all of the elements in the reference list, you will have an infinite loop, be careful.但正如@GrzegorzBokota 所说,如果 list1 的第一个元素大于引用列表中的所有元素,则会出现无限循环,要小心。

if you replace the reference list by a numpy.array , you can directly index the element which is the next bigger one:如果用numpy.array替换引用list ,则可以直接索引下一个较大的元素:

import numpy as np

list1 = [[2,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list = np.array([5, 10, 30])
for i in list1:
    i[0] = reference_list[reference_list>=i[0]][0]
print(list1)

# [[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]

Note that this also does not address the problem which occurs if your reference does not provide the biggest value.请注意,这也不能解决如果您的参考没有提供最大价值时出现的问题。
But this can be solved by checking if the list of all bigger values is empty:但这可以通过检查所有较大值的列表是否为空来解决:

list1 = [[200,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list = np.array([5, 10, 30])
for i in list1:
    next_bigger = list(reference_list[reference_list>=i[0]])
    if next_bigger:
        i[0] = next_bigger[0]
print(list1)
# [[200, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]

If you want to quantize the first element of each sublist to one of the entries in the reference_list, you could do it with a list comprehension:如果要将每个子列表的第一个元素量化为 reference_list 中的一个条目,可以使用列表理解来实现:

list1=[[2,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list=[5, 10, 30]
list1 = [ [next((r for r in reference_list if r>=v[0]),v[0])]+v[1:] for v in list1 ]
print(list1)

Your question and example could also be interpreted to mean that each entry in the reference list corresponds to a list at the same position in the list of lists.您的问题和示例也可以解释为参考列表中的每个条目对应于列表列表中相同位置的列表。 If that is the case, you can use zip to traverse both lists in parallel:如果是这种情况,您可以使用 zip 并行遍历两个列表:

list1 = [ [max(r,v[0])]+v[1:] for r,v in zip(reference_list,list1) ]  

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

相关问题 搜索与另一个字符串元素列表匹配的字符串元素列表 - Search list of string elements that match another list of string elements 将元素列表转换为元组列表以匹配另一个元组列表的结构 - Convert list of elements into list of tuples to match the structure of another list of tuples Python - 查找与另一个列表的元素匹配的列表元素的索引 - Python - Find the indices of elements of a list that match elements of another list 将pandas列中的关键字与另一个元素列表进行匹配 - Match keywords in pandas column with another list of elements 根据python中的另一个列表匹配列表的元素,其中一个列表的元素是另一个列表的子字符串 - match element of a list based on another list in python where elements of one list is the substring of elements of another list Python-与条件匹配的另一个列表的元素的索引列表 - Python - List of indices of elements of another list that match a condition 更改列表中元素的 position 以便它们交叉匹配 Python 中的另一个列表 - Change position of elements in a list so they cross match another list in Python 将列表的元素更改为字符串 - Changing elements of list to string Python:更改列表中的元素 - Python: Changing Elements in List 更改列表的元素与更改列表本身 - changing elements of a list vs changing the list itself
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM