python: How to remove values from 2 lists based on whats in 1 list

2024/10/4 17:29:35

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements.

Normally, I would just plot

ax.scatter(xVar,yVar,s=2,color='tomato');

I want to remove data from yVar that are over a certain value, for example all data that have a yVar value over 100, but I also want to remove the associated xVar value. Can somebody suggest a way to create 2 new variables that remove all values in yVar over 100 and the xVar values associated with them? Then I can just substitute xVar & yVar in my plotting line to the new variables.

Thanks again All,

Answer

Whenever you want to do something to the corresponding values of two (or more) lists, that's what zip is for. It gives you one list, of the corresponding values for each index.

So, in this case, zip the two lists together, then filtered the zipped list, then unzip them (with zip again, as the documentation explains):

xVar, yVar = zip(*((x, y) for x, y in zip(xVar, yVar) if y <= 100))

If this is confusing, let me show it step by step:

>>> xVar = [1, 200, 300, 10]
>>> yVar = [150, 100, 50, 200]
>>> xyVar = zip(xVar, yVar)
>>> xyVar
[(1, 150), (200, 100), (300, 50), (10, 500)]
>>> xyFiltered = [(x, y) for x, y in xyVar if y <= 100]
>>> xyFiltered
[(200, 100), (300, 50)]
>>> xVar, yVar = zip(*xyFiltered)
[(200, 300), (100, 50)]
https://en.xdnf.cn/q/70588.html

Related Q&A

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single columnIndex A Index B 0 A 0 NAN 1 NAN 1 D 2 B 2 …

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…

Copy fields from one instance to another in Django

I have the following code which takes an existing instance and copies, or archives it, in another model and then deletes it replacing it with the draft copy. Current Codedef archive_calc(self, rev_num,…