如何基于Python中的另一个列表列表迭代替换一个列表列表中的值

编程入门 行业动态 更新时间:2024-10-09 00:40:52
本文介绍了如何基于Python中的另一个列表列表迭代替换一个列表列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一个蟒蛇新bie。 我有两个列表如下: A = List [List [float,float]] B = List [List [float,float]] 两种不同尺码 Ex: A有[时间,价值] B有[start_time,end_time] A = [[0.0,10.0],[1.0,10.0],[2.0,10.0],[3.0,10.0],[4.0,10.0],[5.0,10.0],[6.0,10.0] ] B = [[0.0,2.0],[5.0,6.0]] 我想做的是:如果A有一个不在B的时间,我应该把A中相应的'值'设为零。 因此产出将是: [[0.0,10.0],[1.0,10.0],[2.0,10.0],[3.0,0.0],[4.0,0.0 ],[5.0,10.0],[6.0,10.0]] 即如果B中的时间段在A中没有相应的时间值,则对应于该时间的值应为零。在这种情况下,B中存在2到5之间的值,因此A中的'3'和'4'的伙伴值被设为零。 请告诉我该怎么做。

我试过的: 我很无能为力。 AA = numpy.hstack(B)#获取次数 for i in 1:len(AA): if(AA [i] == A [

解决方案

Python教程 - Python 3.4.7文档 [ ^ ]

I am a python new bie. I have two list of lists like this: A = List[List[float,float]] B = List[List[float,float]] both different sizes Ex : A has [time, value] B has [start_time, end_time] A = [[0.0,10.0] , [1.0,10.0], [2.0,10.0], [3.0,10.0], [4.0,10.0], [5.0,10.0], [6.0,10.0]] B = [[0.0,2.0], [5.0,6.0]] What I am trying to do is : if A has a time which is not in B, I should make the corresponding 'value' in A to zero. So output would be : [[0.0,10.0] , [1.0,10.0], [2.0,10.0], [3.0,0.0], [4.0,0.0], [5.0,10.0], [6.0,10.0]] i.e if a time segment in B has no corresponding time value present in A, the value corresponding to that time should be made zero. In this case, the values between 2 and 5 are there in B , so partner values of'3' and '4' in A are made to zeroes. Please tell me how to do it.

What I have tried: I'm an clueless. AA = numpy.hstack(B) # for getting array of times for i in 1: len(AA): if (AA[i]==A[

解决方案

The Python Tutorial — Python 3.4.7 documentation[^]

更多推荐

如何基于Python中的另一个列表列表迭代替换一个列表列表中的值

本文发布于:2023-11-30 07:43:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   迭代   列表中   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!