idx
int64 0
2.38k
| question
stringlengths 5
184
| target
stringlengths 5
213
|
---|---|---|
2,300 | Get a list of pairs of key-value sorted by values in dictionary `data`
| sorted ( list ( data . items ( ) ) , key = lambda x : x [ 1 ] )
|
2,301 | sort dict by value python
| sorted ( list ( data . items ( ) ) , key = lambda x : x [ 1 ] )
|
2,302 | display current time
| now = datetime . datetime . now ( ) . strftime ( '%H:%M:%S' )
|
2,303 | find the index of the second occurrence of the substring `bar` in string `foo bar bar bar`
| """foo bar bar bar""" . replace ( 'bar' , 'XXX' , 1 ) . find ( 'bar' )
|
2,304 | check if key 'stackoverflow' and key 'google' are presented in dictionary `sites`
| set ( [ 'stackoverflow' , 'google' ] ) . issubset ( sites )
|
2,305 | replace string ' and ' in string `stuff` with character '/'
| stuff . replace ( ' and ' , '/' )
|
2,306 | Save array at index 0, index 1 and index 8 of array `np` to tmp file `tmp`
| np . savez ( tmp , * [ getarray [ 0 ] , getarray [ 1 ] , getarray [ 8 ] ] )
|
2,307 | substract 1 hour and 10 minutes from current time
| t = datetime . datetime . now ( ) ( t - datetime . timedelta ( hours = 1 , minutes = 10 ) )
|
2,308 | subtract 1 hour and 10 minutes from time object `t`
| ( t - datetime . timedelta ( hours = 1 , minutes = 10 ) )
|
2,309 | add 1 hour and 2 minutes to time object `t`
| dt = datetime . datetime . combine ( datetime . date . today ( ) , t )
|
2,310 | subtract 5 hours from the time object `dt`
| dt -= datetime . timedelta ( hours = 5 )
|
2,311 | encode string `data` using hex 'hex' encoding
| print ( data . encode ( 'hex' ) )
|
2,312 | Return the decimal value for each hex character in data `data`
| print ( ' ' . join ( [ str ( ord ( a ) ) for a in data ] ) )
|
2,313 | Get all the items from a list of tuple 'l' where second item in tuple is '1'.
| [ x for x in l if x [ 1 ] == 1 ]
|
2,314 | Create array `a` containing integers from stdin
| a . fromlist ( [ int ( val ) for val in stdin . read ( ) . split ( ) ] )
|
2,315 | place '\' infront of each non-letter char in string `line`
| print ( re . sub ( '[_%^$]' , '\\\\\\g<0>' , line ) )
|
2,316 | Get all `a` tags where the text starts with value `some text` using regex
| doc . xpath ( "//a[starts-with(text(),'some text')]" )
|
2,317 | convert a list of lists `a` into list of tuples of appropriate elements form nested lists
| zip ( * a )
|
2,318 | convert a list of strings `lst` to list of integers
| [ map ( int , sublist ) for sublist in lst ]
|
2,319 | convert strings in list-of-lists `lst` to ints
| [ [ int ( x ) for x in sublist ] for sublist in lst ]
|
2,320 | get index of elements in array `A` that occur in another array `B`
| np . where ( np . in1d ( A , B ) ) [ 0 ]
|
2,321 | create a list where each element is a dictionary with keys 'key1' and 'key2' and values corresponding to each value in the lists referenced by keys 'key1' and 'key2' in dictionary `d`
| [ { 'key1' : a , 'key2' : b } for a , b in zip ( d [ 'key1' ] , d [ 'key2' ] ) ]
|
2,322 | Split dictionary of lists into list of dictionaries
| map ( dict , zip ( * [ [ ( k , v ) for v in value ] for k , value in list ( d . items ( ) ) ] ) )
|
2,323 | Get Last Day of the first month in 2002
| calendar . monthrange ( 2002 , 1 )
|
2,324 | Get Last Day of the second month in 2002
| calendar . monthrange ( 2008 , 2 )
|
2,325 | Get Last Day of the second month in 2100
| calendar . monthrange ( 2100 , 2 )
|
2,326 | Get Last Day of the month `month` in year `year`
| calendar . monthrange ( year , month ) [ 1 ]
|
2,327 | Get Last Day of the second month in year 2012
| monthrange ( 2012 , 2 )
|
2,328 | Get Last Day of the first month in year 2000
| ( datetime . date ( 2000 , 2 , 1 ) - datetime . timedelta ( days = 1 ) )
|
2,329 | Calling an external command "ls -l"
| from subprocess import call
|
2,330 | Calling an external command "some_command with args"
| os . system ( 'some_command with args' )
|
2,331 | Calling an external command "some_command < input_file | another_command > output_file"
| os . system ( 'some_command < input_file | another_command > output_file' )
|
2,332 | Calling an external command "some_command with args"
| stream = os . popen ( 'some_command with args' )
|
2,333 | Calling an external command "echo Hello World"
| print ( subprocess . Popen ( 'echo Hello World' , shell = True , stdout = subprocess . PIPE ) . stdout . read ( ) )
|
2,334 | Calling an external command "echo Hello World"
| print ( os . popen ( 'echo Hello World' ) . read ( ) )
|
2,335 | Calling an external command "echo Hello World"
| return_code = subprocess . call ( 'echo Hello World' , shell = True )
|
2,336 | Calling an external command "ls"
| p = subprocess . Popen ( 'ls' , shell = True , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) for line in p . stdout . readlines ( ) : print ( line , end = ' ' ) retval = p . wait ( )
|
2,337 | Calling an external command "ls -l"
| call ( [ 'ls' , '-l' ] )
|
2,338 | decode url `url` with utf8 and print it
| print ( urllib . parse . unquote ( url ) . decode ( 'utf8' ) )
|
2,339 | decode a urllib escaped url string `url` with `utf8`
| url = urllib . parse . unquote ( url ) . decode ( 'utf8' )
|
2,340 | delete letters from string '12454v'
| """""" . join ( filter ( str . isdigit , '12454v' ) )
|
2,341 | Update row values for a column `Season` using vectorized string operation in pandas
| df [ 'Season' ] . str . split ( '-' ) . str [ 0 ] . astype ( int )
|
2,342 | sort a list of tuples `my_list` by second parameter in the tuple
| my_list . sort ( key = lambda x : x [ 1 ] )
|
2,343 | find indexes of all occurrences of a substring `tt` in a string `ttt`
| [ m . start ( ) for m in re . finditer ( '(?=tt)' , 'ttt' ) ]
|
2,344 | find all occurrences of a substring in a string
| [ m . start ( ) for m in re . finditer ( 'test' , 'test test test test' ) ]
|
2,345 | split string `s` based on white spaces
| re . findall ( '\\s+|\\S+' , s )
|
2,346 | set columns `['race_date', 'track_code', 'race_number']` as indexes in dataframe `rdata`
| rdata . set_index ( [ 'race_date' , 'track_code' , 'race_number' ] )
|
2,347 | recursively go through all subdirectories and files in `rootdir`
| for ( root , subFolders , files ) in os . walk ( rootdir ) : pass
|
2,348 | sort a list of dictionary values by 'date' in reverse order
| list . sort ( key = lambda item : item [ 'date' ] , reverse = True )
|
2,349 | display first 5 characters of string 'aaabbbccc'
| """{:.5}""" . format ( 'aaabbbccc' )
|
2,350 | unpack hexadecimal string `s` to a list of integer values
| struct . unpack ( '11B' , s )
|
2,351 | finding the index of an item 'foo' given a list `['foo', 'bar', 'baz']` containing it
| [ i for i , j in enumerate ( [ 'foo' , 'bar' , 'baz' ] ) if j == 'foo' ]
|
2,352 | generate all permutations of list `[1, 2, 3]` and list `[4, 5, 6]`
| print ( list ( itertools . product ( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ) ) )
|
2,353 | generate all permutations of a list `[1, 2, 3]`
| itertools . permutations ( [ 1 , 2 , 3 ] )
|
2,354 | substitute occurrences of unicode regex pattern u'\\p{P}+' with empty string '' in string `text`
| return re . sub ( '\\p{P}+' , '' , text )
|
2,355 | manually throw/raise a `ValueError` exception with the message 'A very specific bad thing happened'
| raise ValueError ( 'A very specific bad thing happened' )
|
2,356 | throw an exception "I know Python!"
| raise Exception ( 'I know Python!' )
|
2,357 | Manually throw an exception "I know python!"
| raise Exception ( 'I know python!' )
|
2,358 | throw a ValueError with message 'represents a hidden bug, do not catch this'
| raise ValueError ( 'represents a hidden bug, do not catch this' )
|
2,359 | throw an Exception with message 'This is the exception you expect to handle'
| raise Exception ( 'This is the exception you expect to handle' )
|
2,360 | throw a value error with message 'A very specific bad thing happened', 'foo', 'bar', 'baz'
| raise ValueError ( 'A very specific bad thing happened' )
|
2,361 | throw a runtime error with message 'specific message'
| raise RuntimeError ( 'specific message' )
|
2,362 | throw an assertion error with message "Unexpected value of 'distance'!", distance
| raise AssertionError ( "Unexpected value of 'distance'!" , distance )
|
2,363 | if Selenium textarea element `foo` is not empty, clear the field
| driver . find_element_by_id ( 'foo' ) . clear ( )
|
2,364 | clear text from textarea 'foo' with selenium
| driver . find_element_by_id ( 'foo' ) . clear ( )
|
2,365 | convert a number 2130706433 to ip string
| socket . inet_ntoa ( struct . pack ( '!L' , 2130706433 ) )
|
2,366 | Rearrange the columns 'a','b','x','y' of pandas DataFrame `df` in mentioned sequence 'x' ,'y','a' ,'b'
| df = df [ [ 'x' , 'y' , 'a' , 'b' ] ]
|
2,367 | call base class's __init__ method from the child class `ChildClass`
| super ( ChildClass , self ) . __init__ ( * args , ** kwargs )
|
2,368 | sum of all values in a python dict `d`
| sum ( d . values ( ) )
|
2,369 | Sum of all values in a Python dict
| sum ( d . values ( ) )
|
2,370 | convert python dictionary `your_data` to json array
| json . dumps ( your_data , ensure_ascii = False )
|
2,371 | assign an array of floats in range from 0 to 100 to a variable `values`
| values = np . array ( [ i for i in range ( 100 ) ] , dtype = np . float64 )
|
2,372 | sort a list of dictionaries `list_of_dct` by values in an order `order`
| sorted ( list_of_dct , key = lambda x : order . index ( list ( x . values ( ) ) [ 0 ] ) )
|
2,373 | change the case of the first letter in string `s`
| return s [ 0 ] . upper ( ) + s [ 1 : ]
|
2,374 | join list of numbers `[1,2,3,4] ` to string of numbers.
| """""" . join ( [ 1 , 2 , 3 , 4 ] )
|
2,375 | delete every non `utf-8` characters from a string `line`
| line = line . decode ( 'utf-8' , 'ignore' ) . encode ( 'utf-8' )
|
2,376 | execute a command `command ` in the terminal from a python script
| os . system ( command )
|
2,377 | MySQL execute query 'SELECT * FROM foo WHERE bar = %s AND baz = %s' with parameters `param1` and `param2`
| c . execute ( 'SELECT * FROM foo WHERE bar = %s AND baz = %s' , ( param1 , param2 ) )
|
2,378 | Parse string `datestr` into a datetime object using format pattern '%Y-%m-%d'
| dateobj = datetime . datetime . strptime ( datestr , '%Y-%m-%d' ) . date ( )
|