comparison

Python: comparing numpy array with sub-numpy array without loop

Python: comparing numpy array with sub-numpy array without loop Question: My problem is quite simple but I cannot figure how to solve it without a loop. I have a first numpy array: FullArray = np.array([0,1,2,3,4,5,6,7,8,9]) and a sub array (not necessarily ordered in the same way): Sub array = np.array([8, 3, 5]) I would like …

Total answers: 1

Comparing nested dictionaries into general dictionary

Comparing nested dictionaries into general dictionary Question: I would like to compare multiple nested dictionaries inside a larger dictionary. Suppose we have the following dictionary: pricesDict = { "supermarket_1": {"apples": 1, "bananas": 2, "melons": 3}, "supermarket_2": {"apples": 1.5, "bananas": 2, "melons": 3}, "supermarket_3": {"apples": 1, "bananas": 2, "melons": 3}, "supermarket_4": {"apples": 1, "bananas": 2.7, "melons": …

Total answers: 2

Add an additional column to a panda dataframe comparing two columns

Add an additional column to a panda dataframe comparing two columns Question: I have a dataframe (df) containing two columns: Column 1 Column 2 Apple Banana Chicken Chicken Dragonfruit Egg Fish Fish What I want to do is create a third column that says whether the results in each column are the same. For instance: …

Total answers: 4

What is the difference between two codes that count words in a text?

What is the difference between two codes that count words in a text? Question: Hello I was trying to count words in a text but there’s a problem. If I write code like def popular_words(text:str, list:list)-> dict: text=text.lower() splited_text=text.split() answer={} for word in list: answer[word]=splited_text.count(word) return answer print(popular_words(”’ When I was One I had just …

Total answers: 1

A short python program

A short python program Question: I am new to learning python and need to write two lines of code to get the result using comparison operators and not if blocks. Help would be greatly appreciated, thanks. Using one of the comparison operators in Python, write a simple two-line program that takes the parameter n as …

Total answers: 2

Is there a way to compare each list item to another list of exeptions?

Is there a way to compare each list item to another list of exeptions? Question: I have two lists with strings. I need to compare each item in the first list to list of exeptions in the other list and replace items in the first list with ‘ ‘ if there is the same item …

Total answers: 1

Why is it faster to compare strings that match than strings that do not?

Why is it faster to compare strings that match than strings that do not? Question: Here are two measurements: timeit.timeit(‘"toto"=="1234"’, number=100000000) 1.8320042459999968 timeit.timeit(‘"toto"=="toto"’, number=100000000) 1.4517491540000265 As you can see, comparing two strings that match is faster than comparing two strings with the same size that do not match. This is quite disturbing: During a string …

Total answers: 2

Check if each element of a tensor is contained in a list

Check if each element of a tensor is contained in a list Question: Say I have a tensor A and a container of values vals. Is there a clean way of returning a Boolean tensor of the same shape as A with each element being whether that element of A is contained within vals? e.g: …

Total answers: 4

Python string comparison failing

Python string comparison failing Question: small Python prog’ to sort downloads folder by matching the extension, I am getting a False (it seems to me) string match in the following. I searched but seemed to only find issues regarding REGEX matching, or more complex stuff! seperate the extension for filename in os.listdir(src_base_folder): orig_title, orig_ext = …

Total answers: 2

Python: Comparing dictionary key with string

Python: Comparing dictionary key with string Question: I’m trying to compare the key in a dictionary with a string in Python but I can’t find any way of doing this. Let’s say I have: dict = {“a” : 1, “b” : 2} And I want to compare the key of the first index in the …

Total answers: 2