Select 50 items from list at random
I have a function which reads a list of items from a file. How can I select only 50 items from the list randomly to write to another file?
I have a function which reads a list of items from a file. How can I select only 50 items from the list randomly to write to another file?
How do I serialize a Python dictionary into a string, and then back to a dictionary? The dictionary will have lists and other dictionaries inside it.
Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list — a list of lists. At the moment, I’ve tried this: fO = open(filename, 'rU') fL = fO.readlines() That’s all I’ve got. I don’t quite … Read more
I have a df like so:
How would I use python to check a list and delete all duplicates? I don’t want to have to specify what the duplicate item is – I want the code to figure out if there are any and remove them if so, keeping only one instance of each. It also must work if there are … Read more
Well this interactive python console snippet will tell everything:
I have made a generator to read a file word by word and it works nicely.
Say I have the following Pandas Dataframe: df = pd.DataFrame({"a" : [1,2,3], "b" : [[1,2],[2,3,4],[5]]}) a b 0 1 [1, 2] 1 2 [2, 3, 4] 2 3 [5] How would I “unstack” the lists in the “b” column in order to transform it into the dataframe: a b 0 1 1 1 1 2 … Read more
I have example list like this: example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']] Now, I check if it has empty string like this: has_empty = False; for list1 in example_list: for val1 in list1: if val1 == '': has_empty = True print(has_empty) This works OK as it prints True, but looking for more pythonik … Read more
My code is for a Tic Tac Toe game and checking for a draw state but I think this question could be more useful in a general sense.