idx
int64 0
2.38k
| question
stringlengths 5
184
| target
stringlengths 5
213
|
---|---|---|
300 | empty a list `lst`
| lst [ : ] = [ ]
|
301 | empty a list `alist`
| alist [ : ] = [ ]
|
302 | reset index of series `s`
| s . reset_index ( 0 ) . reset_index ( drop = True )
|
303 | convert unicode text from list `elems` with index 0 to normal text 'utf-8'
| elems [ 0 ] . getText ( ) . encode ( 'utf-8' )
|
304 | create a list containing the subtraction of each item in list `L` from the item prior to it
| [ ( y - x ) for x , y in zip ( L , L [ 1 : ] ) ]
|
305 | get value in string `line` matched by regex pattern '\\bLOG_ADDR\\s+(\\S+)'
| print ( re . search ( '\\bLOG_ADDR\\s+(\\S+)' , line ) . group ( 1 ) )
|
306 | import all classes from module `some.package`
| globals ( ) . update ( importlib . import_module ( 'some.package' ) . __dict__ )
|
307 | convert a list of characters `['a', 'b', 'c', 'd']` into a string
| """""" . join ( [ 'a' , 'b' , 'c' , 'd' ] )
|
308 | Slice `url` with '&' as delimiter to get "http://www.domainname.com/page?CONTENT_ITEM_ID=1234" from url "http://www.domainname.com/page?CONTENT_ITEM_ID=1234¶m2¶m3
"
| url . split ( '&' )
|
309 | sort dictionary `d` by key
| od = collections . OrderedDict ( sorted ( d . items ( ) ) )
|
310 | sort a dictionary `d` by key
| OrderedDict ( sorted ( list ( d . items ( ) ) , key = ( lambda t : t [ 0 ] ) ) )
|
311 | Execute a put request to the url `url`
| response = requests . put ( url , data = json . dumps ( data ) , headers = headers )
|
312 | replace everything that is not an alphabet or a digit with '' in 's'.
| re . sub ( '[\\W_]+' , '' , s )
|
313 | create a list of aggregation of each element from list `l2` to all elements of list `l1`
| [ ( x + y ) for x in l2 for y in l1 ]
|
314 | convert string `x' to dictionary splitted by `=` using list comprehension
| dict ( [ x . split ( '=' ) for x in s . split ( ) ] )
|
315 | remove index 2 element from a list `my_list`
| my_list . pop ( 2 )
|
316 | Delete character "M" from a string `s` using python
| s = s . replace ( 'M' , '' )
|
317 | How to delete a character from a string using python?
| newstr = oldstr . replace ( 'M' , '' )
|
318 | get the sum of the products of each pair of corresponding elements in lists `a` and `b`
| sum ( x * y for x , y in zip ( a , b ) )
|
319 | sum the products of each two elements at the same index of list `a` and list `b`
| list ( x * y for x , y in list ( zip ( a , b ) ) )
|
320 | sum the product of each two items at the same index of list `a` and list `b`
| sum ( i * j for i , j in zip ( a , b ) )
|
321 | sum the product of elements of two lists named `a` and `b`
| sum ( x * y for x , y in list ( zip ( a , b ) ) )
|
322 | write the content of file `xxx.mp4` to file `f`
| f . write ( open ( 'xxx.mp4' , 'rb' ) . read ( ) )
|
323 | Add 1 to each integer value in list `my_list`
| new_list = [ ( x + 1 ) for x in my_list ]
|
324 | get a list of all items in list `j` with values greater than `5`
| [ x for x in j if x >= 5 ]
|
325 | set color marker styles `--bo` in matplotlib
| plt . plot ( list ( range ( 10 ) ) , '--bo' )
|
326 | set circle markers on plot for individual points defined in list `[1,2,3,4,5,6,7,8,9,10]` created by range(10)
| plt . plot ( list ( range ( 10 ) ) , linestyle = '--' , marker = 'o' , color = 'b' )
|
327 | split strings in list `l` on the first occurring tab `\t` and enter only the first resulting substring in a new list
| [ i . split ( '\t' , 1 ) [ 0 ] for i in l ]
|
328 | Split each string in list `myList` on the tab character
| myList = [ i . split ( '\t' ) [ 0 ] for i in myList ]
|
329 | Sum numbers in a list 'your_list'
| sum ( your_list )
|
330 | attach debugger pdb to class `ForkedPdb`
| ForkedPdb ( ) . set_trace ( )
|
331 | Compose keys from dictionary `d1` with respective values in dictionary `d2`
| result = { k : d2 . get ( v ) for k , v in list ( d1 . items ( ) ) }
|
332 | add one day and three hours to the present time from datetime.now()
| datetime . datetime . now ( ) + datetime . timedelta ( days = 1 , hours = 3 )
|
333 | Convert binary string to list of integers using Python
| [ int ( s [ i : i + 3 ] , 2 ) for i in range ( 0 , len ( s ) , 3 ) ]
|
334 | switch keys and values in a dictionary `my_dict`
| dict ( ( v , k ) for k , v in my_dict . items ( ) )
|
335 | sort a list `L` by number after second '.'
| print ( sorted ( L , key = lambda x : int ( x . split ( '.' ) [ 2 ] ) ) )
|
336 | Check if the value of the key "name" is "Test" in a list of dictionaries `label`
| any ( d [ 'name' ] == 'Test' for d in label )
|
337 | remove all instances of [1, 1] from list `a`
| a [ : ] = [ x for x in a if x != [ 1 , 1 ] ]
|
338 | remove all instances of `[1, 1]` from a list `a`
| [ x for x in a if x != [ 1 , 1 ] ]
|
339 | convert a list 'a' to a dictionary where each even element represents the key to the dictionary, and the following odd element is the value
| b = { a [ i ] : a [ i + 1 ] for i in range ( 0 , len ( a ) , 2 ) }
|
340 | check whether elements in list `a` appear only once
| len ( set ( a ) ) == len ( a )
|
341 | Generate MD5 checksum of file in the path `full_path` in hashlib
| print ( hashlib . md5 ( open ( full_path , 'rb' ) . read ( ) ) . hexdigest ( ) )
|
342 | How to sort a dictionary in python by value when the value is a list and I want to sort it by the first index of that list
| sorted ( list ( data . items ( ) ) , key = lambda x : x [ 1 ] [ 0 ] )
|
343 | randomly switch letters' cases in string `s`
| """""" . join ( x . upper ( ) if random . randint ( 0 , 1 ) else x for x in s )
|
344 | force bash interpreter '/bin/bash' to be used instead of shell
| os . system ( 'GREPDB="echo 123"; /bin/bash -c "$GREPDB"' )
|
345 | Run a command `echo hello world` in bash instead of shell
| os . system ( '/bin/bash -c "echo hello world"' )
|
346 | access the class variable `a_string` from a class object `test`
| getattr ( test , a_string )
|
347 | Display a image file `pathToFile`
| Image . open ( 'pathToFile' ) . show ( )
|
348 | replace single quote character in string "didn't" with empty string ''
| """didn't""" . replace ( "'" , '' )
|
349 | sort list `files` based on variable `file_number`
| files . sort ( key = file_number )
|
350 | remove all whitespace in a string `sentence`
| sentence . replace ( ' ' , '' )
|
351 | remove all whitespace in a string `sentence`
| pattern = re . compile ( '\\s+' ) sentence = re . sub ( pattern , '' , sentence )
|
352 | remove whitespace in string `sentence` from beginning and end
| sentence . strip ( )
|
353 | remove all whitespaces in string `sentence`
| sentence = re . sub ( '\\s+' , '' , sentence , flags = re . UNICODE )
|
354 | remove all whitespaces in a string `sentence`
| sentence = '' . join ( sentence . split ( ) )
|
355 | sum all the values in a counter variable `my_counter`
| sum ( my_counter . values ( ) )
|
356 | find the euclidean distance between two 3-d arrays `A` and `B`
| np . sqrt ( ( ( A - B ) ** 2 ) . sum ( - 1 ) )
|
357 | create list `levels` containing 3 empty dictionaries
| levels = [ { } , { } , { } ]
|
358 | find the sums of length 7 subsets of a list `daily`
| weekly = [ sum ( visitors [ x : x + 7 ] ) for x in range ( 0 , len ( daily ) , 7 ) ]
|
359 | Delete an element `key` from a dictionary `d`
| del d [ key ]
|
360 | Delete an element 0 from a dictionary `a`
| { i : a [ i ] for i in a if ( i != 0 ) }
|
361 | Delete an element "hello" from a dictionary `lol`
| lol . pop ( 'hello' )
|
362 | Delete an element with key `key` dictionary `r`
| del r [ key ]
|
363 | solve for the least squares' solution of matrices `a` and `b`
| np . linalg . solve ( np . dot ( a . T , a ) , np . dot ( a . T , b ) )
|
364 | split dictionary/list inside a pandas column 'b' into separate columns in dataframe `df`
| pd . concat ( [ df . drop ( 'b' , axis = 1 ) , pd . DataFrame ( df [ 'b' ] . tolist ( ) ) ] , axis = 1 )
|
365 | loop through 0 to 10 with step 2
| for i in range ( 0 , 10 , 2 ) : pass
|
366 | loop through `mylist` with step 2
| for i in mylist [ : : 2 ] : pass
|
367 | lowercase string values with key 'content' in a list of dictionaries `messages`
| [ { 'content' : x [ 'content' ] . lower ( ) } for x in messages ]
|
368 | convert a list `my_list` into string with values separated by spaces
| """ """ . join ( my_list )
|
369 | replace each occurrence of the pattern '(http://\\S+|\\S*[^\\w\\s]\\S*)' within `a` with ''
| re . sub ( '(http://\\S+|\\S*[^\\w\\s]\\S*)' , '' , a )
|
370 | check if string `str` is palindrome
| str ( n ) == str ( n ) [ : : - 1 ]
|
371 | upload binary file `myfile.txt` with ftplib
| ftp . storbinary ( 'STOR myfile.txt' , open ( 'myfile.txt' , 'rb' ) )
|
372 | remove all characters from string `stri` upto character 'I'
| re . sub ( '.*I' , 'I' , stri )
|
373 | parse a comma-separated string number '1,000,000' into int
| int ( '1,000,000' . replace ( ',' , '' ) )
|
374 | combine dataframe `df1` and dataframe `df2` by index number
| pd . merge ( df1 , df2 , left_index = True , right_index = True , how = 'outer' )
|
375 | Combine two Pandas dataframes with the same index
| pandas . concat ( [ df1 , df2 ] , axis = 1 )
|
376 | check if all boolean values in a python dictionary `dict` are true
| all ( dict . values ( ) )
|
377 | use regex pattern '^12(?=.{4}$)' to remove digit 12 if followed by 4 other digits in column `c_contofficeID` of dataframe `df`
| df . c_contofficeID . str . replace ( '^12(?=.{4}$)' , '' )
|
378 | reverse a list `L`
| L [ : : ( - 1 ) ]
|
379 | reverse a list `array`
| reversed ( array )
|
380 | reverse a list `L`
| L . reverse ( )
|
381 | reverse a list `array`
| list ( reversed ( array ) )
|
382 | get first element of each tuple in list `A`
| [ tup [ 0 ] for tup in A ]
|
383 | replace character 'a' with character 'e' and character 's' with character '3' in file `contents`
| newcontents = contents . replace ( 'a' , 'e' ) . replace ( 's' , '3' )
|
384 | serialise SqlAlchemy RowProxy object `row` to a json object
| json . dumps ( [ dict ( list ( row . items ( ) ) ) for row in rs ] )
|
385 | get file '~/foo.ini'
| config_file = os . path . expanduser ( '~/foo.ini' )
|
386 | get multiple parameters with same name from a url in pylons
| request . params . getall ( 'c' )
|
387 | Convert array `x` into a correlation matrix
| np . corrcoef ( x )
|
388 | Find the greatest number in set `(1, 2, 3)`
| print ( max ( 1 , 2 , 3 ) )
|
389 | Retrieve parameter 'var_name' from a GET request.
| self . request . get ( 'var_name' )
|
390 | Add 100 to each element of column "x" in dataframe `a`
| a [ 'x' ] . apply ( lambda x , y : x + y , args = ( 100 , ) )
|
391 | Django get first 10 records of model `User` ordered by criteria 'age' of model 'pet'
| User . objects . order_by ( '-pet__age' ) [ : 10 ]
|
392 | delay for "5" seconds
| time . sleep ( 5 )
|
393 | make a 60 seconds time delay
| time . sleep ( 60 )
|
394 | make a 0.1 seconds time delay
| sleep ( 0.1 )
|
395 | make a 60 seconds time delay
| time . sleep ( 60 )
|
396 | make a 0.1 seconds time delay
| time . sleep ( 0.1 )
|
397 | From a list of strings `my_list`, remove the values that contains numbers.
| [ x for x in my_list if not any ( c . isdigit ( ) for c in x ) ]
|
398 | get the middle two characters of a string 'state' in a pandas dataframe `df`
| df [ 'state' ] . apply ( lambda x : x [ len ( x ) / 2 - 1 : len ( x ) / 2 + 1 ] )
|
399 | draw a grid line on every tick of plot `plt`
| plt . grid ( True )
|
Subsets and Splits