Linked Questions

90 questions linked to/from "Ask forgiveness not permission" - explain
-1 votes
1 answer
66 views

To separate only the lists that converted become all numbers, I do it like this: a = ['start','nan','1.004569','0.8554','nan'] b = ['0.464','5','4.8784','2.411474','0.44'] c = ['start','start','start',...
340 votes
25 answers
387k views

Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like getch()). I know there's a function in Windows for ...
Evan Fosmark's user avatar
453 votes
10 answers
289k views

Let's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with ...
67 votes
7 answers
103k views

1 = 0b1 -> 1 5 = 0b101 -> 3 10 = 0b1010 -> 4 100 = 0b1100100 -> 7 1000 = 0b1111101000 -> 10 ... How can I get the bit length of an integer, i.e. the number of bits that are necessary to ...
user288832's user avatar
63 votes
11 answers
120k views

I am calling os.mkdir to create a folder with a certain set of generated data. However, even though the path I specified has not been created, the os.mkdir(path) raises an OSError that the path ...
30 votes
5 answers
42k views

I know there is no .Net function that exists for checking, but is there an algorithm or easy and effective way of checking if a byte is a valid image before I use the byte array. I need this because I'...
20 votes
7 answers
17k views

Let's say I have a python function whose single argument is a non-trivial type: from typing import List, Dict ArgType = List[Dict[str, int]] # this could be any non-trivial type def myfun(a: ArgType) ...
25 votes
2 answers
29k views

Is there a better way to load a variable with pickle if it already exists or create it and dump it if it doesn't? if os.path.isfile("var.pickle"): foo = pickle.load( open( "var.pickle", "rb" ) ) ...
Matteo's user avatar
  • 8,172
8 votes
6 answers
12k views

I have a list of strings ['foo1', 'foo2', ...] that represent variables that I want to delete from self if they are part of self. What is a Pythonic and compact way to do this? My first attempt is ...
RoachLord's user avatar
  • 1,071
13 votes
4 answers
20k views

I am looking for a pythonic way to create and write to file if opening it was successful or else return an error if not (e.g. permission denied). I was reading here What's the pythonic way of ...
dylan7's user avatar
  • 823
18 votes
4 answers
11k views

I'm working on code bases with extensive type hints, checked by mypy. There's several instances where we have a mapping from an enum.Enum or other small finite set of statically known values (typing....
11 votes
2 answers
32k views

I have a file that may or may not be empty. The goal is to read from the file an json object, append to it, and then write it back to the file. However, on the case that the file is empty, then json....
13 votes
2 answers
8k views

I am trying to access a certain member in a JavaScript object. In order to do this, I need to try out a couple of key values. For example, Object['text/html'] which will give me an export link for a ...
5 votes
6 answers
13k views

I have several nested dictionaries within lists, and I need to verify if a specific path exist e.g. dict1['layer1']['layer2'][0]['layer3'] How can I check with an IF statement if the path is valid? ...
Luigi T.'s user avatar
  • 555
4 votes
3 answers
8k views

I keep getting an error in Python when I try to split a single word. From what I read, this is because the default split() command looks for whitespace. The problem is, I want the second assigned ...

15 30 50 per page
1
2 3 4 5 6