Python use string variable as index/key for nested list/dict

Advertisements data = {‘virtualInterfaces’: [{"interfaceName": "abc", "interfaceState": "online"}, {"interfaceName": "axc", "interfaceState": "online"}, {"interfaceName": "xpto", "interfaceState": "offline"}]} foo = "[‘virtualInterfaces’][2][‘interfaceState’]" I need to use the var "foo" to get a specific value from data e.g data[foo] #should return "offline" I tried to use eval value = eval(data + foo) But didn´t work. Foo can have any… Read More Python use string variable as index/key for nested list/dict

Python – Iterate over a file of items and Split('_) -> now group like names and get a total count

Advertisements a file has 50 files in a naming convention to where I can split off ‘_’ and grab the [3] element from the split to get the names of the produce from the file. I have the file path imported and the files in a variable for & split each element[3] off into a… Read More Python – Iterate over a file of items and Split('_) -> now group like names and get a total count

Python3 – IndexError: string index out of range

Advertisements import sys from Bio import SeqIO f = sys.argv[1] seq_records = SeqIO.parse(f, ‘fasta’) refseq_record = next(seq_records) for seq_record in seq_records: for i in range(0, len(refseq_record)): nt1 = refseq_record[i] nt2 = seq_record[i] if nt1 != nt2: print (seq_record.id, i+1, nt2, nt1) Hello, I get IndexError: string index out of range error at line 12: nt2… Read More Python3 – IndexError: string index out of range

Most efficient way to continuously find the median of a stream of numbers (in Python)?

Advertisements I’m trying to solve a problem which reads as follows: A queue of eager single-digits (your input) are waiting to enter an empty room. I allow one digit (from the left) to enter the room each minute. Each time a new digit enters the room I chalk up the median of all the digits… Read More Most efficient way to continuously find the median of a stream of numbers (in Python)?

how extract the alphanumeric value using regex

Advertisements import re text = """THE PO NUMBER 134314 MUST BE INCLUDED ALONG WITH SERVICE DATE ON ALL INVOICES SUBMITTED THROUGH THE AVID BILL PAY PROCESS. THANK YOU.""" p_no = re.search("PO# (\w+)|PO NUMBER (\w+)", text) print(p_no) if p_no == None: print("No value found") else: print(p_no.group(1)) REQUIRED SOLUTION : 134314 Is there any solution to get… Read More how extract the alphanumeric value using regex

Can One List Comprehension Be Used for both a Method and Class?

Advertisements I have a simple program that determines the lateral surface area and volume of a pyramid from preset values: class RightSquarePyramid(): def __init__(self, b=0, h=0): self.b = float(b) self.h = float(h) pass def slantHeight(self): self.l = sqrt((self.h ** 2 + (self.b / 2) ** 2)) return self.l def lateralSurfaceArea(self): return 2 * self.b *… Read More Can One List Comprehension Be Used for both a Method and Class?

Better way of replacing subtring from a string

Advertisements I’m new to python and looking for a better or faster solution. I replaced substring from string using this solution. In this example, that is to replace https://www.site1.com// from a url string site2 = ‘https://www.site2.com/’ url = ‘https://www.site1.com//vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=New’ output = site2 + url[url.index(‘vehicles/’):] print(output) # https://www.site2.com/vehicles/2023/Ford/F-150/Calgary/AB/57506598/?sale_class=N Is this a faster solution? Is there a… Read More Better way of replacing subtring from a string