python-3.x

How to use python to merge two lists using common elements?

How to use python to merge two lists using common elements? Question: I have two lists in python, the first with each element composed by a string and an integer: delta[0:10] [(‘conhecimento’, 17), (‘ciência’, 14), (‘interdisciplinaridade’, 13), (‘saber’, 10), (‘objeto’, 10), (‘pode’, 10), (‘processo’, 9), (‘conceito’, 9), (‘assim’, 8), (‘mundo’, 8)] And a second list …

Total answers: 2

100 Prisoners Dilemma – Python code isn't working

100 Prisoners Dilemma – Python code isn't working Question: I stumbled across an interesting probability problem about half an hour ago. https://en.wikipedia.org/wiki/100_prisoners_problem Essentially, there are 100 boxes, drawers, etc, each with a unique number between 1-100 inside of them. There are also 100 prisoners. Each prisoner has 50 chances to find the box with their …

Total answers: 2

Fail to grab the posting dates from encrypted content or so

Fail to grab the posting dates from encrypted content or so Question: I’m trying to scrape the titles and posting dates of different jobs from this webpage. The content of that page seems to be dynamic and loaded using an endpoint. I can parse titles from json response but fail to grab the posting dates. …

Total answers: 1

Splitting a list on non-sequential numbers

Splitting a list on non-sequential numbers Question: I have an ordered list of entities, numbered in a broken sequence: [1, 2, 3, 6, 7, 11, 17, 18, 19] I’d like to break the list where there’s a gap, and collect the results in a new list: [[1, 2, 3], [6, 7], [11], [17, 18, 19]] …

Total answers: 5

Load a from a text file containing multiple JSONs into Python

Load a from a text file containing multiple JSONs into Python Question: I have a text file temp.txt of the sort — { "names" : [ {"index" : 0, "cards": "nnbingo" …} ] "more stuff": … } { "names" : [ {"index" : 0, "cards": "nfalse" …} ] "more stuff": … } . . Here’s …

Total answers: 1

Historical minimum of timeseries data instead of .min in Panda

Historical minimum of timeseries data instead of .min in Panda Question: I have a dataframe of different stocks and their stock price at different time points that looks like: Stock_ID Price Date 1 45 2022-11-22 1 43 2022-11-21 1 46 2022-11-20 1 49 2022-11-19 2 81 2021-10-19 2 79 2021-10-13 2 63 2021-10-09 2 74 …

Total answers: 1

Appending list of key:value pairs without adding more curly braces in Python

Appending list of key:value pairs without adding more curly braces in Python Question: I need to append a list of key:value pairs multiple times. However, this produces additional curly braces after each .append(). Basically, I have something like this: textToDump = {"food": "cereal_bowl", "ingredients":[]} textToDump["ingredients"].append({"milk": 100, "cereal": 100, }) textToDump["ingredients"].append({"honey": 10}) print(textToDump) which results in: …

Total answers: 3

Order of dictionary keys when performing conditional list comprehension in Python

Order of dictionary keys when performing conditional list comprehension in Python Question: In Python 3.10, I am aware that a dictionary preserves insertion order. However when performing conditional list comprehensions, can this order still be guaranteed? For example, given: my_dict = {} my_dict[‘a’] = 1 my_dict[‘b’] = 2 my_dict[‘c’] = 3 my_dict[‘d’] = 4 Can …

Total answers: 2

globals(), locals(), vars() do not work as expected in a function of python

globals(), locals(), vars() do not work as expected in a function of python Question: if I run one of following code variants var1, var2 =”, ” globals()[‘var1′]=’value1′ print(f'{var1=}, {var2=}’) var1, var2 =”, ” locals()[‘var1′]=’value1′ print(f'{var1=}, {var2=}’) var1, var2 =”, ” vars()[‘var1′]=’value1′ print(f'{var1=}, {var2=}’) all of them will return var1=’value1′, var2=”, which is what I expect, …

Total answers: 1

GMAIL API: I cant change account in python

GMAIL API: I cant change account in python Question: I want to change the account in python, but when I change flow=InstalledAppFlow.from_client_secrets_file(‘onerandomclient.json’, SCOPES) For the other JSON, still connecting with the last account, not the one I put. Asked By: GundamUser || Source Answers: I am going to assume that you are following python quickstart …

Total answers: 1