python-2.x

Python convert different depth list of tuples to flatten list of tuples

Python convert different depth list of tuples to flatten list of tuples Question: I have a tuple of characters like such: p= [((4.0, 4.0), ‘->’, ((4, 2), (4, 8)), ((2, 2), (5, 5))), ((4.0, 7.0), ‘->’, ((4, 2), (4, 8)), ((5, 6), (3, 8)))]` How do I convert it to a tupple so that it …

Total answers: 5

Calculate sum of columns of same name pandas

Calculate sum of columns of same name pandas Question: How can i search for duplicate columns in a dataframe and then create a new column with same name. the new column is result of ‘OR’ operator of these columns. Then drop old duplicated columns. Example: For that, I tried to create a unique column ‘job’ …

Total answers: 1

Python 3 2-D list initialization breaks in Python 2

Python 3 2-D list initialization breaks in Python 2 Question: I’m trying to initialize a 2-D list using list comprehensions, but I’m seeing different results in Python 2 vs. Python 3 and I have no idea why. Sample code (the import is just so I can use v3 print statements in v2): from __future__ import …

Total answers: 1

Digging deeper into python if statements

Digging deeper into python if statements Question: It might be a really silly question, but I was not able to find the answer to it anywhere else, I’ve looked at SO, but there is nothing I could find related to my question. Question: In python, don’t know about other languages, whenever we call if statements …

Total answers: 3

I'm trying to create a discord.py giveaway command

I'm trying to create a discord.py giveaway command Question: I’ve tried this and i didn’t really get the logic through it, I need some help making it have: Emoji Message Setting the time My Code: @bot.command() async def giveaway(ctx, msg, duration): embed=discord.Embed() embed.title=msg embed.description="React To Giveaway With To Join." embed.set_footer(text=" MTND Bot Development") embed.color=0x00ffff msg …

Total answers: 3

How to reinstall python@2 from Homebrew?

How to reinstall python@2 from Homebrew? Question: I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python. The problem is, when you try to install Python 2 with brew, …

Total answers: 13

Create a file within a subdirectory in python

Create a file within a subdirectory in python Question: I need to create folders and files as below: Suppose my current working directory is called ‘working_dir’ I need to create -> working_dir/my_folder/sub_folder/new_file.json I can create a file in a sub directory as below: import json import os my_details = { ‘name’: ‘John Doe’, ‘age’: 29 …

Total answers: 2

bytes.decode() in Python2 and Python3

bytes.decode() in Python2 and Python3 Question: In the source code of sqlalchemy I see following val = cursor.fetchone()[0] if util.py3k and isinstance(val, bytes): val = val.decode() Why we do decode only for Python3 and doesn’t do it for Python2? Asked By: Rudziankoŭ || Source Answers: You can check out a detail documentation of string encoding …

Total answers: 2

Dynamically defining/updating ctypes structure in Python

Dynamically defining/updating ctypes structure in Python Question: I have created subtructure and structure in ctypes as below where I am defining a array of substructure inside structure with some predefined size. (As per requirement SIZE may be set to 0 initially and can varies based on user input). from ctypes import * class MySubStructure(Structure): _fields_ …

Total answers: 1

What is an Object in Python?

What is an Object in Python? Question: I am surprised that my question was not asked (worded like the above) before. I am hoping that someone could break down this basic term "object" in the context of a OOP language like Python. Explained in a way in which a beginner like myself would be able …

Total answers: 8