invert edge values in python boolean list

2024/10/6 20:27:47

I have a list of booleans like

l = [False, False, False, True, True, True, False, False, True, False, False]

and want to invert every edge value that is False like

[True, True, True, True, True, True, False, False, True, True, True]

What is the most pythonic way to achieve that?

Answer

Possibly there's some clever one-line solution for this, but until someone comes up with it, here's a way to do it with old fashioned iteration:

seq = [False, False, False, True, True, True, False, False, True, False, False]for i in range(len(seq)):if seq[i]:breakseq[i] = Truefor i in range(len(seq)-1, -1, -1):if seq[i]:breakseq[i] = Trueprint(seq)

Result:

[True, True, True, True, True, True, False, False, True, True, True]
https://en.xdnf.cn/q/118904.html

Related Q&A

Unable to write text on mouseclick area on Image

I am trying to draw text on Image where the user clicks. Getting this error:Exception in Tkinter callback Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Python\Pytho…

Google Cloud Storage: __init__() got an unexpected keyword argument total_size

I am developping a tool to transcribe interviews for a contract I have. For that I develop a code with the following flow:After input validation, the audio file (in m4a) is converted to wav and stored …

Selenium, Intercept HTTP Request?

Using selenium 4.12 in Python, how can I intercept an HTTP request to see what its body or headers look like? Please Note, that Im not asking for code but rather for resources/ideas of different or su…

Flask server returns 404 on localhost:5000 w/ Twilio

Im following this guide (Python Quickstart: Replying to SMS and MMS Messages) to try and set up a flask server, but when I try to connect to http://localhost:5000 I get a 404 error. I can ping 127.0.0.…

printing values and keys from a dictionary in a specific format (python)

I have this dictionary (name and grade):d1 = {a: 1, b: 2, c: 3}and I have to print it like this:|a | 1 | C | |b | 2 | B | |c | 3 | …

stdscr.getstr() ignore keys, just string

I just need convert entered text(bytes) to string. But if i on cyrillic press Backspace and some character, python throw me this error:UnicodeDecodeError: utf-8 codec cant decode byte 0xd0 in position …

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers?

What is wrong with the following program code, attempting to initialize a 4 x 4 matrix of integers? How should the initialization be done?line = [0] * 4 matrix = [line, line, line, line]

Creating a Data Pipeline to BigQuery Using Cloud Functions and Cloud Scheduler

I am trying to build a Data Pipeline that will download the data from this website and push it to a BigQuery Table. def OH_Data_Pipeline(trigger=Yes):if trigger==Yes:import pandas as pdimport pandas_gb…

Matching several string matches from lists and making a new row for each match

I have a data frame with text in one of the columns and I am using regex formatted strings to see if I can find any matches from three lists. However, when there are multiple matches from list 1, I wan…

Join and format array of objects in Python

I want to join and format values and array of objects to a string in python. Is there any way for me to do that?url = "https://google.com", search = "thai food", search_res = [{&q…