[Solved] Single line for loop over iterator with an “if” filter?

No, there is no shorter way. Usually, you will even break it into two lines : important_airports = (airport for airport in airports if airport.is_important) for airport in important_airports: # do stuff This is more flexible, easier to read and still don’t consume much memory. 3 solved Single line for loop over iterator with an … Read more

[Solved] How to find data in string? [closed]

Use re.findall. The regex: r’^(.*\S)\s+id:\s*(\d+)\s+type:\s*(.+)’ means: ^ : start of the string..* :any character, repeated 0 or more times.\S : non-whitespace character.\s+ : whitespace character, repeated 1 or more times.\d+ : any digit, repeated 1 or more times.(PATTERN) : capture the patterns and return it. Here we capture 3 patterns. import re string = “Wacom … Read more

[Solved] How to check the distinct number of characters in a string [closed]

You can do something like this: text=”Hello this is a test” unique_letters = set(text.lower()) unique_letters.remove(‘ ‘) print(unique_letters) print (‘odd’) if len(unique_letters) % 2 else print (‘even’) Result: {‘o’, ‘h’, ‘e’, ‘l’, ‘i’, ‘s’, ‘a’, ‘t’} even 0 solved How to check the distinct number of characters in a string [closed]

[Solved] Print message if a certain condition meet on a dataframe

import pandas as pd, numpy as np # filter results by showing records that meet condition # sample df d = {‘SBP’: [111, 100], ‘DBP’: [81, 40], ‘HEARTRATE’:[1,50]} df = pd.DataFrame(data=d) df # if an alert is found, prints alert, else normal if len(df[(df[ ‘SBP’ ] > 110) & (df[ ‘DBP’ ] > 80) & … Read more

[Solved] Reverse of a list of list in Python [closed]

A simple solution: def reversemat(mat): return [row[::-1] for row in mat] print(reversemat([[1,0],[1,0]])) # [[0, 1], [0, 1]] besides, you have returned nothing from your function, so you will get None from your second print. 3 solved Reverse of a list of list in Python [closed]

[Solved] Given a 6 blocks, of different height h1, h2 . Make 2 towers using 3 Blocks for each tower in desired height h1, h2

elemnts=[2,2,0,0,5,6] h1=9 h2=6 def func(elemts,a,b): list1=[] for i in range(0,len(elemts)): for j in range(i+1,len(elemts)): for k in range(0,len(elemts)): if(k not in [i,j]): temp=elemts[i]+elemts[j]+elemts[k] if(temp in [h1,h2]): list1.extend([elemts[i],elemts[j],elemts[k]]) return list1 list2=func(elemnts,h1,h2) @arthur_currry…just chnged one line in last if case.Its working fine solved Given a 6 blocks, of different height h1, h2 . Make 2 towers using … Read more

[Solved] NameError: variable is not defined [closed]

Is this what you want? def existingPlayers(name, players): ”’ Check if player name already exists ”’ for player in players: if player[0].lower() == name.lower(): return True return False def getPlayerNames(players): ”’ Get the names of the players. Check if the number of players and player names are valid ”’ while True: name = input(“Enter player … Read more

[Solved] What would be the python function that returns a list of elements that only appear once in a list [duplicate]

Here is the function that you were looking for. The Iterable and the corresponding import is just to provide type hints and can be removed if you like. from collections import Counter from typing import Iterable d = [1,1,2,3,4,5,5,5,6] def retainSingles(it: Iterable): counts = Counter(it) return [c for c in counts if counts[c] == 1] … Read more

[Solved] Accept arbitrary multi-line input of String type and store it in a variable

From what I gather, you’re trying to get input from a user until that user types -1. If thats the case, please see my function below. public static void main (String[] args) { // Scanner is used for I/O Scanner input = new Scanner(System.in); // Prompt user to enter text System.out.println(“Enter something “); // Get … Read more