python - replace the boolean value of a list with the values from two different lists [duplicate]
2024/11/15 22:00:31
I have one list with boolean values like
lyst = [True,True,False,True,False]
and two different lists for example like:
car = ['BMW','VW','Volvo']
a = ['b','c']
I just want to replace True with the values from car and False with the values from a.
or make a new list with the sequence from lyst and values from car and a.
The result should be
[BMW,VW,b,Volvo,c].
my code so far:
for elem1,elem2,elem3 in zip(lyst,car,a):subelem2=elem2subelem3=elem3if elem1 != True:result_list.append(subelem2)else: result_list.append(subelem3)
but this creates a list match longer than 5.
How can i do this?
Answer
car = iter(car)
a = iter(a)
[next(car) if item else next(a) for item in lyst]
Ok, I couldn't help myself:
car = iter(car)
a = iter(a)
[(flag and next(car)) or next(a) for flag in lyst]
This makes use of some boolean expression features:
a boolean and expression will NOT evaluate the second operand if the first operand is False
a boolean and expression will return the second operand (the actual object) if both operands evaluate to True
a boolean or expression will return the first object that evaluates to True
A potential problem would be if any items in car or a evaluate to False. Also this isn't as readable as the ternary in the first solution.
But it was fun.
I guess I'll add that after looking at this again, I probably shouldn't have reassigned the original list names - I should have chosen something different for the iterator names. Once exhausted, the iterators cannot be reset and since the list names were reassigned, the original list information is lost.
vagrant@ubuntu-xenial:~/lb/f5/v12$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "licens…
I am trying to draw a rectangle on top of a data plot in matplotlib. To do this, I have this codeimport matplotlib.patches as patches
import matplotlib.pyplot as pl...fig = pl.figure()
ax=fig.add_axes(…
Ive a pandas DataFrame plotted as a table using matplotlib (from this answer).Now I want to set the bottom edge color of a given row and Ive this code:import pandas as pd
import numpy as np
import matp…
This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 5 years ago.I am trying to retrieve the id value : ad284hdnn.I am getti…
Only part of this question has been asked before ([1][2]) , which explained how to split numpy arrays. I am quite new in Python. I have an array containing 262144 items and want to split it in small…
I have been stuck trying to get the Stanford POS Tagger to work for a while. From an old SO post I found the following (slightly modified) code:stanford_dir = C:/Users/.../stanford-postagger-2017-06-09…
I have a list in Python which I simply want to write (append) in the first column row-by-row in a Google Sheet. Im done with all the initial authentication part, and heres the code:credentials = Google…
I would like to compute the beta or standardized coefficient of a linear regression model using standard tools in Python (numpy, pandas, scipy.stats, etc.).A friend of mine told me that this is done in…
I was trying to create annotations for grouped bar charts - where each bar has a specific data label that shows the value of that bar and is located above the centre of the bar.I tried a simple modific…
Aside from the obvious, I thought Id try this, just in case:def somegen(input=None):...yield...gentype = type(somegen())
class subgen(gentype):def best_function_ever():...Alas, Pythons response was qui…