Category "list-comprehension"

Replace items of a longer list with matches from a shorter list

My goal is to compare two lists as follows: Take every item in long_list and compare it for substrings to the entire short_list. If there is a match, add the it

Filtering list of objects based on attribute

I have 4 objects. class MyObject: def __init__(self, id: int, result_name: str): self.id = id self.result = Result(result_name) class Resu

Fill in uneven sized lists in Python

I have a 2D-List contains unequal size lengths, like this: lst = [[1,2,3],[-1,2,4],[0,2],[2,-3,6]] I use this code to insert a 0 if element size less 3: newlis

Can't get the walrus operator to work (Python double list comprehension)

This list comprehension does not work: buy_prices = [(buylow := round(0.997 + ii/10000.0, 5), max(jj, buylow)) for jj in [buylow, 0.9982] for ii in range(21)]

Problems converting a list comprehension

This list comprehension works, but I’d rather have it written like conventional loops: Here is the list comprehension: with open(sys.argv[1], 'r') as f:

Is there a simple way to compare two class objects for all items that are not none?

I am looking to compare two instances of the same class, but only the items for which both are not None. for instance, I will have a Bolt class, and one instanc

Convert list of strings of delimited values to list of lists of floats [closed]

I have this array of strings of comma-separated numbers: data = ['1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065', '

How to extract multiple texts from span elements using python selenium?

I am trying to extract all the texts in span into list, using the following HTML code from Selenium webdriver method: ['1a', '1b', '1c', '2a', ' ', ' ', '3a', '

how exactly do expression makes the list comprehension redundant haskell

We know that list comprehensions and do expressions are equivalent for lists. Also list comprehensions are redundant. But can someone explain how exactly do not

How can I use scipy interp1d with N-D array for x without for loop

How can I use scipy.interpolate.interp1d when my x array is an N-D array, instead of a 1-D array, without using a loop? The function f from interp1d then needs

Improving (shortening) my list comprehension with lambda x function?

!!! I know I could use Counter() here, but that's not the point !!! Giving the code below: test =[['a',1],['a',2],['b',3], ['c',5],['a',7],['c',9]] {i:[test[x]

Decouple dictionary that contains a list as a value for a particular key

Let's say I have a dict in Python that follows this structure: dict_a = {"a": [1,2,3]} I want to produce an output to 'decouple' dict_a in a list of 3 separate

Filling a 2D array with values from a 1D array, but using list comprehension

I'm trying to "paste" the values contained in the 1D array dataslice (1x8) into the 2D array values_matrix (9x9), following the position indices contained in th

How to add an extra middle step into a list comprehension?

Let's say I have a list[str] object containing timestamps in "HH:mm" format, e.g. timestamps = ["22:58", "03:11", "12:21"] I want to convert it to a list[int]

Pythonic way to print list items

I would like to know if there is a better way to print all objects in a Python list than this : myList = [Person("Foo"), Person("Bar")] print("\n".join(map(str

Python: Generating all n-length arrays combinations of values within a range

Ok. I'm looking for the smartest and more compact way to do this function def f(): [[a,b,c] for a in range(6) for b in range(6) for c in range(6)] which

Assign the result of a loop to a variable in Python

Consider a list I want to parse using a for : friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"] for i in friends: print i will retur

Can list comprehensions make groups?

I have a list that looks like [(1,2,5),(2,10,13),(5,24,56),(1,8,10),(2,3,11)] How can I produce a dictionary by grouping by first element of tuples and find

How to find list comprehension in python code

I want to find a list comprehension in python source code, for that I tried to use Pygments, but it didn't find the way to do that. To be more specific, I wan

I want to speed up a nested loop when creating a df keyword counts (keywords appearing with other keywords)

Using Pandas I have a df that is 14000 rows by 56 columns (keywords) I have a keyword list (full_keys) that is 1406 items and an empty (0) dataframe (called key