NL: send a signal `signal.sigusr1` to the current process ref: os.kill(os.getpid(), signal.SIGUSR1) Output: print ( proc. get _ append ( ) ) bleu_metric: 14.253628894439752 ---------- NL: decode a hex string '4a4b4c' to utf-8. ref: bytes.fromhex('4a4b4c').decode('utf-8') Output: print ( str. decode ('utf8') ) bleu_metric: 23.083362053422253 ---------- NL: check if all elements in list `mylist` are identical ref: all(x == myList[0] for x in myList) Output: len ( set ( mylist ) ) = = 1 bleu_metric: 9.360791398475978 ---------- NL: format number of spaces between strings `python`, `:` and `very good` to be `20` ref: print('%*s : %*s' % (20, 'Python', 20, 'Very Good')) Output: " " " { 0 :, } " " ". format ( 123456789, 16 ) ( lambda x : x. bleu_metric: 7.030766106716325 ---------- NL: how to convert a string from cp-1251 to utf-8? ref: d.decode('cp1251').encode('utf8') Output: d. decode ('cp1252'). encode ('utf8') bleu_metric: 82.82477531331041 ---------- NL: get rid of none values in dictionary `kwargs` ref: res = {k: v for k, v in list(kwargs.items()) if v is not None} Output: k for k, v in list ( dict. items ( ) ) if v is not none ] bleu_metric: 54.70605317141546 ---------- NL: get rid of none values in dictionary `kwargs` ref: res = dict((k, v) for k, v in kwargs.items() if v is not None) Output: k for k, v in list ( dict. items ( ) ) if v is not none ] bleu_metric: 40.55760587628575 ---------- NL: capture final output of a chain of system commands `ps -ef | grep something | wc -l` ref: subprocess.check_output('ps -ef | grep something | wc -l', shell=True) Output: subprocess. call ( ['mystring. py ','mystring. py'] ) bleu_metric: 11.327304007996714 ---------- NL: concatenate a list of strings `['a', 'b', 'c']` ref: """""".join(['a', 'b', 'c']) Output: ['a ','b ','c ','d'] bleu_metric: 50.018452828984486 ---------- NL: find intersection data between series `s1` and series `s2` ref: pd.Series(list(set(s1).intersection(set(s2)))) Output: s1. intersection ( s1, how ='left'). reset _ index ( ) bleu_metric: 16.132580731955684 ---------- NL: sending http headers to `client` ref: client.send('HTTP/1.0 200 OK\r\n') Output: server. serve _ forever ( ) bleu_metric: 6.063168540538235 ---------- NL: format a datetime string `when` to extract date only ref: then = datetime.datetime.strptime(when, '%Y-%m-%d').date() Output: datetime. datetime. strptime ('2012 - 05 - 29t19 : 30 : 03. 283z ','% y bleu_metric: 28.12321366714592 ---------- NL: split a multi-line string `inputstring` into separate strings ref: inputString.split('\n') Output: from functools import reduce reduce ( lambda x, y : x + y, l. split ('' ), x. split ( bleu_metric: 14.32450175903488 ---------- NL: split a multi-line string ` a \n b \r\n c ` by new line character `\n` ref: ' a \n b \r\n c '.split('\n') Output: re. split ('\ n \ r \ n ','a \ n b \ n') bleu_metric: 65.72891674091409 ---------- NL: concatenate elements of list `b` by a colon ":" ref: """:""".join(str(x) for x in b) Output: ['a ','b'] bleu_metric: 6.490157276489835 ---------- NL: get the first object from a queryset in django model `entry` ref: Entry.objects.filter()[:1].get() Output: model. objects. all ( ). order _ by ('- id') [ 0 ] bleu_metric: 16.1692143534558 ---------- NL: calculate sum over all rows of 2d numpy array ref: a.sum(axis=1) Output: a. sum ( axis = 1 ) bleu_metric: 100.0 ---------- NL: enable warnings using action 'always' ref: warnings.simplefilter('always') Output: warnings. simplefilter ('always') bleu_metric: 100.0 ---------- NL: concatenate items of list `l` with a space ' ' ref: print(' '.join(map(str, l))) Output: print (''. join ( [ str ( x ) for x in l ] ) ) bleu_metric: 42.5874468039002 ---------- NL: run script 'hello.py' with argument 'htmlfilename.htm' on terminal using python executable ref: subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm']) Output: subprocess. call ( ['python ','hello world ','world'] ) bleu_metric: 48.69018522875219 ---------- NL: how can i parse a time string containing milliseconds in it with python? ref: time.strptime('30/03/09 16:31:32.123', '%d/%m/%y %H:%M:%S.%f') Output: time. strptime ('2015 / 01 / 01 / 03. 0. 0. 0 ','% d / % m / % bleu_metric: 31.63951380366603 ---------- NL: convert a string `my_string` with dot and comma into a float number `my_float` ref: my_float = float(my_string.replace(',', '')) Output: my _ string. replace ( ', ','' ) bleu_metric: 59.23488237152091 ---------- NL: convert a string `123,456.908` with dot and comma into a floating number ref: float('123,456.908'.replace(',', '')) Output: " " " { 0 :,. 2f } " " ". format ( 12345678. 456789, 16 bleu_metric: 11.63901425102663 ---------- NL: set pythonpath in python script. ref: sys.path.append('/path/to/whatever') Output: os. path. join ( sys. path. append ('/ path / to / my / root') ) bleu_metric: 51.191407111567585 ---------- NL: split string 'words, words, words.' using a regex '(\\w+)' ref: re.split('(\\W+)', 'Words, words, words.') Output: re. split ('\ \ w + ','\ \ 1 ', words, words ) bleu_metric: 39.78163327099985 ---------- NL: open a file `output.txt` in append mode ref: file = open('Output.txt', 'a') Output: file. write ('whatever') bleu_metric: 17.234857810563913 ---------- NL: download a file "http://www.example.com/songs/mp3.mp3" over http and save to "mp3.mp3" ref: urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3') Output: urllib. request. urlretrieve ('http : / / www. google. com / file. gz ', ' bleu_metric: 49.30073739073773 ---------- NL: download a file `url` over http and save to `file_name` ref: u = urllib.request.urlopen(url) Output: response. close ( ) bleu_metric: 14.35549295013305 ---------- NL: download a file 'http://www.example.com/' over http ref: response = urllib.request.urlopen('http://www.example.com/') html = response.read() Output: urllib. request. urlretrieve ('http : / / www. google. com / ','http : / / bleu_metric: 42.4884196166917 ---------- NL: download a file `url` over http ref: r = requests.get(url) Output: response = requests. get ( url ) bleu_metric: 88.01117367933934 ---------- NL: download a file `url` over http and save to "10mb" ref: response = requests.get(url, stream=True) Output: urllib. request. urlretrieve ('http : / / example. com / file. ext ','wb ' bleu_metric: 6.370405230161802 ---------- NL: argparse add argument with flag '--version' and version action of '%(prog)s 2.0' to parser `parser` ref: parser.add_argument('--version', action='version', version='%(prog)s 2.0') Output: parser. add _ argument ('- - type ', nargs ='*') bleu_metric: 17.102327939516414 ---------- NL: remove key 'c' from dictionary `d` ref: {i: d[i] for i in d if i != 'c'} Output: d = { k : v for k, v in list ( d. items ( ) ) if v } bleu_metric: 9.637411586929977 ---------- NL: create new dataframe object by merging columns "key" of dataframes `split_df` and `csv_df` and rename the columns from dataframes `split_df` and `csv_df` with suffix `_left` and `_right` respectively ref: pd.merge(split_df, csv_df, on=['key'], suffixes=('_left', '_right')) Output: df. drop _ duplicates ( df. columns ['column _ name']. apply ( lambda x :'column _ name ' bleu_metric: 8.416754937260349 ---------- NL: split a string `s` by space with `4` splits ref: s.split(' ', 4) Output: ['x ','y ','x ','z ','x ','d ','x ', ' bleu_metric: 5.648892151960283 ---------- NL: read keyboard-input ref: input('Enter your input:') Output: input ('press enter to continue :') bleu_metric: 42.40125351805037 ---------- NL: enable debug mode on flask application `app` ref: app.run(debug=True) Output: app. run ( debug = true ) bleu_metric: 70.71067811865474 ---------- NL: python save list `mylist` to file object 'save.txt' ref: pickle.dump(mylist, open('save.txt', 'wb')) Output: savefig. save ('filename. txt') bleu_metric: 13.988120102483691 ---------- NL: multiply a matrix `p` with a 3d tensor `t` in scipy ref: scipy.tensordot(P, T, axes=[1, 1]).swapaxes(0, 1) Output: scipy. matrix ( [ 1, 2, 3 ], [ 1, 2, 3 ] ). t bleu_metric: 16.944114083600446 ---------- NL: create 3d array of zeroes of size `(3,3,3)` ref: numpy.zeros((3, 3, 3)) Output: np. zeros ( ( 3, 3 ) ). transpose ( 2, 3 ) bleu_metric: 54.166685749344154 ---------- NL: cut off the last word of a sentence `content` ref: """ """.join(content.split(' ')[:-1]) Output: " " " " " ". join ( content. split ('' ) [ : - 1 ] ) bleu_metric: 100.0 ---------- NL: convert scalar `x` to array ref: x = np.asarray(x).reshape(1, -1)[(0), :] Output: x = np. zeros ( x ). reshape ( - 1, 2 ) bleu_metric: 35.6520799192373 ---------- NL: sum all elements of nested list `l` ref: sum(sum(i) if isinstance(i, list) else i for i in L) Output: sum ( [ i for i in l ) for i in l ] bleu_metric: 19.29709182756385 ---------- NL: convert hex string '470fc614' to a float number ref: struct.unpack('!f', '470FC614'.decode('hex'))[0] Output: struct. unpack ('d ', binascii. unhexlify ('4081637ef7d bleu_metric: 20.061882169394003 ---------- NL: multiple each value by `2` for all keys in a dictionary `my_dict` ref: my_dict.update((x, y * 2) for x, y in list(my_dict.items())) Output: dict ( ( k, v ) for k, v in list ( my _ dict. items ( ) ) ) bleu_metric: 35.25419189217226 ---------- NL: running bash script 'sleep.sh' ref: subprocess.call('sleep.sh', shell=True) Output: os. system ('echo $ command - flags arguments') bleu_metric: 14.814869013357976 ---------- NL: join elements of list `l` with a comma `,` ref: """,""".join(l) Output: " " ", " " ". join ( l ) bleu_metric: 100.0 ---------- NL: make a comma-separated string from a list `mylist` ref: myList = ','.join(map(str, myList)) Output: mylist = [ i. split ( ',') for i in mylist ] bleu_metric: 15.526320210133507 ---------- NL: reverse the list that contains 1 to 10 ref: list(reversed(list(range(10)))) Output: [ ( 1, 2 ), ( 3, 4 ), ( 2, - 1 ), ( 1, 2 ), ( 3, 4 bleu_metric: 5.834347180338517 ---------- NL: remove substring 'bag,' from a string 'lamp, bag, mirror' ref: print('lamp, bag, mirror'.replace('bag,', '')) Output: re. sub ( ', ', ', '. join ( chr ) for ch in asking ) bleu_metric: 22.93476949918813 ---------- NL: reverse the order of words, delimited by `.`, in string `s` ref: """.""".join(s.split('.')[::-1]) Output: re. sub ('\ \ b (. + ) (? = \ \ s + \ \ s + ) ','\ \ 1 bleu_metric: 7.508325052934217 ---------- NL: convert epoch time represented as milliseconds `s` to string using format '%y-%m-%d %h:%m:%s.%f' ref: datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f') Output: datetime. datetime. now ( ). strftime ('% y - % m - % d % h : % m : % bleu_metric: 42.22477323756999 ---------- NL: parse milliseconds epoch time '1236472051807' to format '%y-%m-%d %h:%m:%s' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1236472051807 / 1000.0)) Output: datetime. datetime. now ( ). strftime ('% y - % m - % d % h : % m : % bleu_metric: 32.53778694644052 ---------- NL: get the date 7 days before the current date ref: (datetime.datetime.now() - datetime.timedelta(days=7)).date() Output: ( datetime. datetime. now ( ) - datetime. timedelta ( days = 1 ) ) bleu_metric: 69.7353047868514 ---------- NL: sum elements at index `column` of each list in list `data` ref: print(sum(row[column] for row in data)) Output: [ sum ( item ) for item in zip ( * items ) ] bleu_metric: 14.009047908905242 ---------- NL: sum columns of a list `array` ref: [sum(row[i] for row in array) for i in range(len(array[0]))] Output: sum ( i ) bleu_metric: 0.4295018804111146 ---------- NL: encode binary string 'your string' to base64 code ref: base64.b64encode(bytes('your string', 'utf-8')) Output: base64. b64encode ('base64') bleu_metric: 17.22724743406155 ---------- NL: combine list of dictionaries `dicts` with the same keys in each list to a single dictionary ref: dict((k, [d[k] for d in dicts]) for k in dicts[0]) Output: dict ( ( k, v ) for d in dicts for k, v in list ( d. items ( ) ) ) bleu_metric: 32.12854967972961 ---------- NL: merge a nested dictionary `dicts` into a flat dictionary by concatenating nested values with the same key `k` ref: {k: [d[k] for d in dicts] for k in dicts[0]} Output: dict ( ( k, v ) for d in list ( d. items ( ) ) for k, v in list ( d. items ( bleu_metric: 10.14524997386625 ---------- NL: how do i get the url parameter in a flask view ref: request.args['myParam'] Output: request. args ['myapp'] bleu_metric: 58.8837385313777 ---------- NL: identify duplicate values in list `mylist` ref: [k for k, v in list(Counter(mylist).items()) if v > 1] Output: [ k for k, v in mylist if v. startswith ('mycount') else v for k, v in mylist bleu_metric: 34.06227682100307 ---------- NL: insert directory 'apps' into directory `__file__` ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) Output: urllib. request. urlopen ('http : / / www. example. com ','/ path') bleu_metric: 9.10451572772839 ---------- NL: modify sys.path for python module `subdir` ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) Output: subprocess. call ( ['start / subdir / bin / 2014 ','inr'] ) bleu_metric: 8.368553758184737 ---------- NL: insert a 'none' value into a sqlite3 table. ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) Output: cursor. execute ('select * from table where column in l') bleu_metric: 13.62767317410906 ---------- NL: flatten list `list_of_menuitems` ref: [image for menuitem in list_of_menuitems for image in menuitem] Output: [ item for sublist in list _ of _ lists ] bleu_metric: 17.194656088289214 ---------- NL: append elements of a set `b` to a list `a` ref: a.extend(b) Output: a. extend ( b ) bleu_metric: 100.0 ---------- NL: append elements of a set to a list in python ref: a.extend(list(b)) Output: a. extend ( b ) bleu_metric: 46.81294641285289 ---------- NL: write the data of dataframe `df` into text file `np.txt` ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') Output: df. to _ csv ('filename. txt ', header = false ) bleu_metric: 14.209004647554735 ---------- NL: write content of dataframe `df` into text file 'c:\\data\\pandas.txt' ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') Output: df. to _ csv ('c : \ \ data \ \ t. txt ', sep ='\ t ', index bleu_metric: 37.50344293811855 ---------- NL: split a string `x` by last occurrence of character `-` ref: print(x.rpartition('-')[0]) Output: x. split ('-') [ : - 1 ] bleu_metric: 43.353400970949984 ---------- NL: get the last part of a string before the character '-' ref: print(x.rsplit('-', 1)[0]) Output: re. sub ('(? < = - -. ) ','- ', line ) bleu_metric: 20.776569671187676 ---------- NL: upload file using ftp ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) Output: ftp. quit ( ) bleu_metric: 3.710176016036221 ---------- NL: add one to the hidden web element with id 'xyz' with selenium python script ref: browser.execute_script("document.getElementById('XYZ').value+='1'") Output: driver. execute _ script ('window. scrollto ( 0, document. body. com') ) bleu_metric: 10.807499589580493 ---------- NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` ref: np.maximum([2, 3, 4], [1, 5, 2]) Output: np. array ( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ) bleu_metric: 28.489318277723964 ---------- NL: print a list `l` and move first 3 elements to the end of the list ref: print(l[3:] + l[:3]) Output: print ( [ l [ 3 : ] for l in l ] ) bleu_metric: 43.472087194499146 ---------- NL: loop over files in directory '.' ref: for fn in os.listdir('.'): Output: for ( root, dirs, dirs ) in os. walk ( '.') : if os. path. isdir ( ' bleu_metric: 26.167475767017738 ---------- NL: loop over files in directory `source` ref: for (root, dirs, filenames) in os.walk(source): Output: os. listdir ('os') bleu_metric: 9.081302407530908 ---------- NL: create a random list of integers ref: [int(1000 * random.random()) for i in range(10000)] Output: random. choice ( [ int ( i ) for i in range ( 10 ) ] ) bleu_metric: 38.95592996402149 ---------- NL: using %f with strftime() in python to get microseconds ref: datetime.datetime.now().strftime('%H:%M:%S.%f') Output: datetime. datetime. now ( ). strftime ('% h : % m : % s. % f') bleu_metric: 69.80972118841675 ---------- NL: google app engine execute gql query 'select * from schedule where station = $1' with parameter `foo.key()` ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) Output: c. objects. filter ( id = 1 ). order _ by ('foobar') bleu_metric: 10.30143863225941 ---------- NL: filter rows in pandas starting with alphabet 'f' using regular expression. ref: df.b.str.contains('^f') Output: df ['a']. str. contains ('c') bleu_metric: 43.85068972747104 ---------- NL: print a 2 dimensional list `tab` as a table with delimiters ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) Output: print ('\ n '. join ( map ( str, row ) ) ) bleu_metric: 28.575895973659073 ---------- NL: pandas: delete rows in dataframe `df` based on multiple columns values ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() Output: df. drop ( df. index. map ( lambda x : x. index, axis = 1 ) ) bleu_metric: 13.21619418680914 ---------- NL: format the variables `self.goals` and `self.penalties` using string formatting ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) Output: " " " { } \ \ w { }. { } " " ". format ( date,'user') bleu_metric: 28.34142738530324 ---------- NL: format string "({} goals, ${})" with variables `goals` and `penalties` ref: """({} goals, ${})""".format(self.goals, self.penalties) Output: " " " { } { } " " ". format ( {'foo':'bar'} ) bleu_metric: 34.620457601133026 ---------- NL: format string "({0.goals} goals, ${0.penalties})" ref: """({0.goals} goals, ${0.penalties})""".format(self) Output: df. set _ index ('name') bleu_metric: 3.9504404329436813 ---------- NL: convert list of lists `l` to list of integers ref: [int(''.join(str(d) for d in x)) for x in L] Output: [ int ( x ) for x in l ] bleu_metric: 18.761335250609946 ---------- NL: combine elements of each list in list `l` into digits of a single integer ref: [''.join(str(d) for d in x) for x in L] Output: map ( int, l ) bleu_metric: 2.5915324112223583 ---------- NL: convert a list of lists `l` to list of integers ref: L = [int(''.join([str(y) for y in x])) for x in L] Output: [ int ( x ) for x in l ] bleu_metric: 11.473780780974305 ---------- NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` ref: myfile.write('\n'.join(lines)) Output: myfile. write ('\ n '. join ( map ( str, mylist ) ) ) bleu_metric: 60.5478327168454 ---------- NL: removing an element from a list based on a predicate 'x' or 'n' ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] Output: [ x for x in [ x if x! ='b'] if x! ='c'] bleu_metric: 20.792998340455547 ---------- NL: remove duplicate words from a string `text` using regex ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) Output: re. sub ('(. * ) \ \ 1 + ','\ \ 1 ', text ) bleu_metric: 42.691904454956294 ---------- NL: count non zero values in each column in pandas data frame ref: df.astype(bool).sum(axis=1) Output: df. groupby ( level = 0 ). sum ( ) bleu_metric: 33.17838167503153 ---------- NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` ref: print(re.search('>.*<', line).group(0)) Output: print ( doc. group ( 1 ). group ( 1 ) ) bleu_metric: 22.08517793512327 ---------- NL: erase all the contents of a file `filename` ref: open(filename, 'w').close() Output: open ('filename ','w'). close ( ) bleu_metric: 70.98108717509044 ---------- NL: convert a string into datetime using the format '%y-%m-%d %h:%m:%s.%f' ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') Output: datetime. datetime. strptime ('2010 - 11 - 13 10 : 33 : 54. 227806 ','% y bleu_metric: 23.69248624414322 ---------- NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` ref: [index for index, item in enumerate(thelist) if item[0] == '332'] Output: [ x [ 0 ] for x in l if x [ 0 ] = ='score'] bleu_metric: 32.08521724608534 ---------- NL: lower a string `text` and remove non-alphanumeric characters aside from space ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() Output: re. sub ('[ ^ \ \ w. ] + ','', text ). strip ( ) bleu_metric: 54.08690470924506 ---------- NL: remove all non-alphanumeric characters except space from a string `text` and lower it ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() Output: re. sub ('[ ^ 0 - 9a - za - z ] + ','', s ). strip ( ) bleu_metric: 38.80387619814469 ---------- NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='H\u2082O') Output: plt. plot ( x, y, label ='h \ u2082o') bleu_metric: 72.76817202342092 ---------- NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='$H_2O$') Output: plt. plot ( x, y, label ='h \ u2082o') bleu_metric: 71.60350546947922 ---------- NL: loop over a list `mylist` if sublists length equals 3 ref: [x for x in mylist if len(x) == 3] Output: mylist [ : : 2 ] = = mylist [ 0 ] bleu_metric: 12.640504935009755 ---------- NL: initialize a list `lst` of 100 objects object() ref: lst = [Object() for _ in range(100)] Output: lst = [ [ ] for i in range ( 3 ) ] bleu_metric: 28.992166587078206 ---------- NL: create list `lst` containing 100 instances of object `object` ref: lst = [Object() for i in range(100)] Output: list ( chain. from _ iterable ( l ) ) bleu_metric: 10.649638629044373 ---------- NL: get the content of child tag with`href` attribute whose parent has css `someclass` ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') Output: soup. find _ elements _ by _ xpath ( " / / * [ @ type ='submit'] " ). get _ attribute ( bleu_metric: 10.389889377420038 ---------- NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'date_time' in both dataframes ref: df1.merge(df2, on='Date_Time') Output: pd. merge ( df1, df2, on = ['date _ time ','date _ time'] ) bleu_metric: 21.22363344155404 ---------- NL: use `%s` operator to print variable values `str1` inside a string ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') Output: print ( str ( a ) +'' + str ( b ) ) bleu_metric: 6.053698751871955 ---------- NL: split a string by a delimiter in python ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] Output: s. split (';') bleu_metric: 2.904501569386027 ---------- NL: check if directory `directory ` exists and create it if necessary ref: if (not os.path.exists(directory)): os.makedirs(directory) Output: os. makedirs ( path ) bleu_metric: 9.597952472216766 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os. makedirs ( path ) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os. makedirs ( path ) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os. makedirs ( path ) bleu_metric: 100.0 ---------- NL: replace a separate word 'h3' by 'h1' in a string 'text' ref: re.sub('\\bH3\\b', 'H1', text) Output: re. sub ('\ \ w + ','\ \ 1 ', text ) bleu_metric: 56.31071891586576 ---------- NL: substitute ascii letters in string 'aas30dsa20' with empty string '' ref: re.sub('\\D', '', 'aas30dsa20') Output: re. sub ('[ a - z ] ','','abcd') bleu_metric: 51.003234295212685 ---------- NL: get digits only from a string `aas30dsa20` using lambda function ref: """""".join([x for x in 'aas30dsa20' if x.isdigit()]) Output: " " " " " ". join ( map ( lambda x : x * 7 ) ) bleu_metric: 39.54603942707659 ---------- NL: access a tag called "name" in beautifulsoup `soup` ref: print(soup.find('name').string) Output: soup. findall ('div ', {'name':'description'} ) bleu_metric: 17.457404828304572 ---------- NL: get a dictionary `records` of key-value pairs in pymongo cursor `cursor` ref: records = dict((record['_id'], record) for record in cursor) Output: dict ( ( x [ 0 ], x [ 1 ] ) for x in cursor. items ( ) ) bleu_metric: 16.34453553468014 ---------- NL: create new matrix object by concatenating data from matrix a and matrix b ref: np.concatenate((A, B)) Output: numpy. concatenate ( [ a, b ], axis = 0 ) bleu_metric: 18.92240568795935 ---------- NL: concat two matrices `a` and `b` in numpy ref: np.vstack((A, B)) Output: np. sqrt ( ( a. shape [ 0 ], b ) ) bleu_metric: 19.923405658137924 ---------- NL: get the characters count in a file `filepath` ref: os.stat(filepath).st_size Output: print ( os. path. join ( get _ list ) ) bleu_metric: 13.834368456410944 ---------- NL: count the occurrences of item "a" in list `l` ref: l.count('a') Output: [ x. count ('a') for x in l ] bleu_metric: 43.78826865860793 ---------- NL: count the occurrences of items in list `l` ref: Counter(l) Output: [ sum ( x ) for x in l ] bleu_metric: 13.065113298388567 ---------- NL: count the occurrences of items in list `l` ref: [[x, l.count(x)] for x in set(l)] Output: [ sum ( x ) for x in l ] bleu_metric: 16.443715448412245 ---------- NL: count the occurrences of items in list `l` ref: dict(((x, l.count(x)) for x in set(l))) Output: [ sum ( x ) for x in l ] bleu_metric: 15.927159921457768 ---------- NL: count the occurrences of item "b" in list `l` ref: l.count('b') Output: [ ( x, l. count ( x ) ) for x in l ] bleu_metric: 20.61477352156375 ---------- NL: copy file `srcfile` to directory `dstdir` ref: shutil.copy(srcfile, dstdir) Output: os. chdir ('copyfile. txt ', dst ) bleu_metric: 14.949751774990682 ---------- NL: find the key associated with the largest value in dictionary `x` whilst key is non-zero value ref: max(k for k, v in x.items() if v != 0) Output: max ( x, key = lambda x : ( x ['x'], x ['x'] ) ) bleu_metric: 9.186672262412122 ---------- NL: get the largest key whose not associated with value of 0 in dictionary `x` ref: (k for k, v in x.items() if v != 0) Output: ( k for k, v in x. items ( ) if v! = 0 ) bleu_metric: 100.0 ---------- NL: get the largest key in a dictionary `x` with non-zero value ref: max(k for k, v in x.items() if v != 0) Output: max ( x, key = lambda x : ( x [ 1 ], x [ 0 ] ) ) bleu_metric: 11.162938527547608 ---------- NL: put the curser at beginning of the file ref: file.seek(0) Output: os. system ('with open ( filename ). read ( ) ','rb') bleu_metric: 8.687475782716618 ---------- NL: combine values from column 'b' and column 'a' of dataframe `df` into column 'c' of datafram `df` ref: df['c'] = np.where(df['a'].isnull, df['b'], df['a']) Output: df ['c'] = df ['a']. apply ( lambda x : x ['b']. fillna bleu_metric: 43.50703089837015 ---------- NL: remove key 'ele' from dictionary `d` ref: del d['ele'] Output: d = { k : v for k, v in list ( d. items ( ) ) if v! ='none'} bleu_metric: 5.442133807846856 ---------- NL: update datetime field in `mymodel` to be the existing `timestamp` plus 100 years ref: MyModel.objects.update(timestamp=F('timestamp') + timedelta(days=36524.25)) Output: mymodel. objects. filter ( date _ _ length ='% y - % m - % d') bleu_metric: 12.351156662075915 ---------- NL: merge list `['it']` and list `['was']` and list `['annoying']` into one list ref: ['it'] + ['was'] + ['annoying'] Output: [ dict ( zip ( * [ ['one ','two'], ['one ','two'] ] ) ) bleu_metric: 10.105385745342533 ---------- NL: increment a value with leading zeroes in a number `x` ref: str(int(x) + 1).zfill(len(x)) Output: print ('\ xe, { 0 : x. 0 } '. format ( x, - 1 ) ) bleu_metric: 13.471766853689124 ---------- NL: check if a pandas dataframe `df`'s index is sorted ref: all(df.index[:-1] <= df.index[1:]) Output: df. index. values. tolist ( ) bleu_metric: 9.980212030371455 ---------- NL: convert tuple `t` to list ref: list(t) Output: list ( t ) bleu_metric: 100.0 ---------- NL: convert list `t` to tuple ref: tuple(l) Output: tuple ( l ) bleu_metric: 100.0 ---------- NL: convert tuple `level1` to list ref: level1 = map(list, level1) Output: [ ( x + 1 ) for x in a ] bleu_metric: 11.35935489027116 ---------- NL: send the output of pprint object `dataobject` to file `logfile` ref: pprint.pprint(dataobject, logFile) Output: sys. stdout. flush ( ) bleu_metric: 18.67587389639562 ---------- NL: get index of rows in column 'boolcol' ref: df.loc[df['BoolCol']] Output: df [ ['bool'] ]. index ('matrix2'). index bleu_metric: 18.710158230410627 ---------- NL: create a list containing the indexes of rows where the value of column 'boolcol' in dataframe `df` are equal to true ref: df.iloc[np.flatnonzero(df['BoolCol'])] Output: df [ df. index. map ( lambda x : x [ 1 ]. isin ( x ) ) ] bleu_metric: 14.72821272412462 ---------- NL: get list of indexes of rows where column 'boolcol' values match true ref: df[df['BoolCol'] == True].index.tolist() Output: [ df ['ratio'] for d in l ] bleu_metric: 19.62812256638541 ---------- NL: get index of rows in dataframe `df` which column 'boolcol' matches value true ref: df[df['BoolCol']].index.tolist() Output: df ['column _ name'] = = df. index. str. get ('columnid') bleu_metric: 18.088201962129936 ---------- NL: change working directory to the directory `owd` ref: os.chdir(owd) Output: os. chdir ('c : \ \ users \ \ users \ \ desktop \ \ desktop \ \ python') bleu_metric: 15.553986407412198 ---------- NL: insert data from a string `testfield` to sqlite db `c` ref: c.execute("INSERT INTO test VALUES (?, 'bar')", (testfield,)) Output: cursor. execute ('insert into table values ( % s, % s ) ', ( ast, ) ) bleu_metric: 34.29547961820059 ---------- NL: decode string "\\x89\\n" into a normal string ref: """\\x89\\n""".decode('string_escape') Output: " " " \ \ xc3 \ \ x96 \ xc3 \ x96 \ x93 \ x93 \ bleu_metric: 29.888108815158 ---------- NL: convert a raw string `raw_string` into a normal string ref: raw_string.decode('string_escape') Output: raw _ string. decode ('unicode _ escape') bleu_metric: 35.556702356686955 ---------- NL: convert a raw string `raw_byte_string` into a normal string ref: raw_byte_string.decode('unicode_escape') Output: raw _ byte _ string. decode ('unicode _ escape') bleu_metric: 30.620490882364876 ---------- NL: split a string `s` with into all strings of repeated characters ref: [m.group(0) for m in re.finditer('(\\d)\\1*', s)] Output: re. findall ('\ \ d + (? : \ \ s + ) ','a \ n b') bleu_metric: 12.792208459604188 ---------- NL: scatter a plot with x, y position of `np.random.randn(100)` and face color equal to none ref: plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none') Output: plt. show ( ) bleu_metric: 0.8278521686370256 ---------- NL: do a scatter plot with empty circles ref: plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none') Output: plt. show ( ) bleu_metric: 0.42503347540650505 ---------- NL: remove a div with a id `main-content` using beautifulsoup ref: soup.find('div', id='main-content').decompose() Output: soup. find _ all ('div ', id ='all') bleu_metric: 46.28692516962493 ---------- NL: filter rows containing key word `ball` in column `ids` ref: df[df['ids'].str.contains('ball')] Output: id. idxmax ( word ). drop ( false ) bleu_metric: 7.630799524006417 ---------- NL: convert index at level 0 into a column in dataframe `df` ref: df.reset_index(level=0, inplace=True) Output: df. set _ index ('level1'). unstack ( level = 0 ) bleu_metric: 24.29335519279861 ---------- NL: add indexes in a data frame `df` to a column `index1` ref: df['index1'] = df.index Output: df. loc [ index ] = df. index. get _ level _ values ( 0 ) bleu_metric: 24.04315522172745 ---------- NL: convert pandas index in a dataframe to columns ref: df.reset_index(level=['tick', 'obs']) Output: df. reset _ index ( drop = true ) bleu_metric: 11.884004683899853 ---------- NL: get reverse of list items from list 'b' using extended slicing ref: [x[::-1] for x in b] Output: [ [ i [ 0 ] for i in range ( len ( x ) ) ] for i in range ( 0, len ( list ( a ) bleu_metric: 7.434360163166209 ---------- NL: join each element in array `a` with element at the same index in array `b` as a tuple ref: np.array([zip(x, y) for x, y in zip(a, b)]) Output: b = np. where ( a = = 0, axis = 0 ) bleu_metric: 9.027924728401977 ---------- NL: zip two 2-d arrays `a` and `b` ref: np.array(zip(a.ravel(), b.ravel()), dtype='i4,i4').reshape(a.shape) Output: np. array ( zip ( a. ravel ( ), b. ravel ( ) ), dtype ='i4, i4 bleu_metric: 67.03200460356393 ---------- NL: convert list `list_of_ints` into a comma separated string ref: """,""".join([str(i) for i in list_of_ints]) Output: list ( map ( int, list _ of _ lists. split ( ',') ) ) bleu_metric: 13.283261562346366 ---------- NL: send a post request with raw data `data` and basic authentication with `username` and `password` ref: requests.post(url, data=DATA, headers=HEADERS_DICT, auth=(username, password)) Output: r = requests. post ( url, data = {'username':'username'} ) bleu_metric: 37.18698903847989 ---------- NL: find last occurrence of character '}' in string "abcd}def}" ref: 'abcd}def}'.rfind('}') Output: re. findall ('(? : ^ [ a - z ] * ) ','abcd0') bleu_metric: 12.962472880491877 ---------- NL: iterate ove list `[1, 2, 3]` using list comprehension ref: print([item for item in [1, 2, 3]]) Output: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] bleu_metric: 27.073362211548464 ---------- NL: extract all the values with keys 'x' and 'y' from a list of dictionaries `d` to list of tuples ref: [(x['x'], x['y']) for x in d] Output: [ d ['x'] for d in x if'y'in d ] bleu_metric: 37.60857585579953 ---------- NL: get the filename without the extension from file 'hemanth.txt' ref: print(os.path.splitext(os.path.basename('hemanth.txt'))[0]) Output: print ( glob. glob ('liname. txt') [ 0 ] ) bleu_metric: 31.18776276497533 ---------- NL: create a dictionary by adding each two adjacent elements in tuple `x` as key/value pair to it ref: dict(x[i:i + 2] for i in range(0, len(x), 2)) Output: dict ( ( k, v ) for k, v in zip ( a, b ) ) bleu_metric: 12.152094584183624 ---------- NL: create a list containing flattened list `[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]` ref: values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], []) Output: [ ['a ','b ','c ','d'], ['a ','b ','c bleu_metric: 17.7080331882364 ---------- NL: select rows in a dataframe `df` column 'closing_price' between two values 99 and 101 ref: df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)] Output: df. loc [ ~ df ['column _ name']. isin ( some _ values ('one ','two bleu_metric: 10.948000684047626 ---------- NL: replace all occurences of newlines `\n` with `
` in dataframe `df` ref: df.replace({'\n': '
'}, regex=True) Output: df. replace ('\ n ','\ n') bleu_metric: 27.770641036119947 ---------- NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` ref: df.replace({'\n': '
'}, regex=True) Output: df. replace ( {'\ n':'< br >'}, regex = true ) bleu_metric: 89.21616972156079 ---------- NL: create a list containing each two adjacent letters in string `word` as its elements ref: [(x + y) for x, y in zip(word, word[1:])] Output: [ ('a ','b'), ('b ','c'), ('c ','d') bleu_metric: 8.840994001530046 ---------- NL: get a list of pairs from a string `word` using lambda function ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) Output: [ str ( x ) for x in words ] bleu_metric: 3.8733243551839442 ---------- NL: extract a url from a string `mystring` ref: print(re.findall('(https?://[^\\s]+)', myString)) Output: print ( re. search ('( https? : / / [ ^ \ \ s ] + ) ', mystring ) ) bleu_metric: 77.28607577370117 ---------- NL: extract a url from a string `mystring` ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) Output: print ( re. search ('( https? : / / [ ^ \ \ s ] + ) ', mystring ) ) bleu_metric: 51.916231800616785 ---------- NL: remove all special characters, punctuation and spaces from a string `mystring` using regex ref: re.sub('[^A-Za-z0-9]+', '', mystring) Output: re. sub ('\ \ w + (? = [ ^. ] * ) ','', mystring ) bleu_metric: 45.91497693322866 ---------- NL: create a datetimeindex containing 13 periods of the second friday of each month starting from date '2016-01-01' ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) Output: df. groupby ('date'). apply ( lambda x : x. year ) bleu_metric: 8.453429635655233 ---------- NL: create multidimensional array `matrix` with 3 rows and 2 columns in python ref: matrix = [[a, b], [c, d], [e, f]] Output: matrix = np. concatenate ( ( [ a, b ], [ [ 0, 3, 6 ], [ 7, 8, 9 bleu_metric: 32.24199399071415 ---------- NL: replace spaces with underscore ref: mystring.replace(' ', '_') Output: re. sub ('\ \ b (? = \ \ w + ) ','','this is a string ','replace bleu_metric: 14.117915208981241 ---------- NL: get an absolute file path of file 'mydir/myfile.txt' ref: os.path.abspath('mydir/myfile.txt') Output: os. path. join ( os. path. join ( root, myfile ) ) bleu_metric: 23.83041256525615 ---------- NL: split string `my_string` on white spaces ref: """ """.join(my_string.split()) Output: " " " 42, " " ". split ( ) bleu_metric: 47.72263780018156 ---------- NL: get filename without extension from file `filename` ref: os.path.splitext(filename)[0] Output: os. path. splitext ( filename ) [ 0 ] bleu_metric: 100.0 ---------- NL: get a list containing the sum of each element `i` in list `l` plus the previous elements ref: [sum(l[:i]) for i, _ in enumerate(l)] Output: sum ( x for x in l ) bleu_metric: 6.924459302580939 ---------- NL: split a string `docs/src/scripts/temp` by `/` keeping `/` in the result ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') Output: re. split ('\ \ s + ', string ) bleu_metric: 7.926805970774756 ---------- NL: shuffle columns of an numpy array 'r' ref: np.random.shuffle(np.transpose(r)) Output: np. random. shuffle ( np. transpose ( r ) ) bleu_metric: 100.0 ---------- NL: copy all values in a column 'b' to a new column 'd' in a pandas data frame 'df' ref: df['D'] = df['B'] Output: df ['b'] = df ['c']. apply ( lambda x : x. tolist ( ), axis = bleu_metric: 26.492685902784498 ---------- NL: find a value within nested json 'data' where the key inside another key 'b' is unknown. ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] Output: print ( json. dumps ( ['b ','c'] ) ) bleu_metric: 5.979778160388024 ---------- NL: check characters of string `string` are true predication of function `predicate` ref: all(predicate(x) for x in string) Output: print ( any ( test _ string ) for substring in string _ string ) bleu_metric: 19.923405658137924 ---------- NL: determine number of files on a drive with python ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree Output: os. stat ('/ myfile. txt'). os. listdir ('/') bleu_metric: 37.211067993181466 ---------- NL: how to get a single result from a sqlite query in python? ref: cursor.fetchone()[0] Output: cursor. fetchone ( ) [ 0 ] bleu_metric: 100.0 ---------- NL: convert string `user_input` into a list of integers `user_list` ref: user_list = [int(number) for number in user_input.split(',')] Output: [ int ( x ) for x in input ( ). split ( ',') ] bleu_metric: 51.23350305765596 ---------- NL: get a list of integers by splitting a string `user` with comma ref: [int(s) for s in user.split(',')] Output: map ( int,'re. split ('( \ \ d + ) ','hello world') ) bleu_metric: 20.80315522738391 ---------- NL: sorting a python list by two criteria ref: sorted(list, key=lambda x: (x[0], -x[1])) Output: sorted ( list ( range ( 0, len ( s ) ), key = lambda x : ( x [ 0 ], x [ 0 ] ) bleu_metric: 48.440807796000044 ---------- NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order ref: ut.sort(key=cmpfun, reverse=True) Output: sorted ( l, key = lambda x : x. modified, reverse = true ) bleu_metric: 18.107197870881738 ---------- NL: reverse list `ut` based on the `count` attribute of each object ref: ut.sort(key=lambda x: x.count, reverse=True) Output: ['city ','same2 ','type'] bleu_metric: 7.453337029678027 ---------- NL: sort a list of objects `ut` in reverse order by their `count` property ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sorted ( l, key = lambda x : x. count ('c') ) bleu_metric: 45.34010611888324 ---------- NL: click a href button 'send' with selenium ref: driver.find_element_by_partial_link_text('Send').click() Output: driver. find _ element _ by _ css _ selector ('send inmail'). click ( ) bleu_metric: 35.13874939965221 ---------- NL: click a href button having text `send inmail` with selenium ref: driver.findElement(By.linkText('Send InMail')).click() Output: driver. find _ element _ by _ link _ text ('send inmail') bleu_metric: 15.175278640994005 ---------- NL: click a href button with text 'send inmail' with selenium ref: driver.find_element_by_link_text('Send InMail').click() Output: driver. find _ element _ by _ link _ text ('send') bleu_metric: 15.27678022374703 ---------- NL: cast an int `i` to a string and concat to string 'me' ref: 'ME' + str(i) Output: str ( i ) bleu_metric: 36.787944117144235 ---------- NL: sorting data in dataframe pandas ref: df.sort_values(['System_num', 'Dis']) Output: df. sort _ values ( ['system _ id ','dis'] ) bleu_metric: 31.671615012203976 ---------- NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) Output: open ('test. txt ','w'). write ('\ n') bleu_metric: 36.176855814312944 ---------- NL: sort a list `l` by length of value in tuple ref: l.sort(key=lambda t: len(t[1]), reverse=True) Output: l. sort ( key = lambda x : ( len ( x ), x ) ) bleu_metric: 37.875123684341865 ---------- NL: split string `s` by words that ends with 'd' ref: re.findall('\\b(\\w+)d\\b', s) Output: re. split ('( \ \ d + ) ', s ) bleu_metric: 29.23155541924467 ---------- NL: return `true` if string `foobarrrr` contains regex `ba[rzd]` ref: bool(re.search('ba[rzd]', 'foobarrrr')) Output: pattern = re. compile ('\ \ b [ a - za - z0 - 9 - ]') bleu_metric: 12.384901282810546 ---------- NL: removing duplicates in list `t` ref: list(set(t)) Output: list ( set ( t ) ) bleu_metric: 100.0 ---------- NL: removing duplicates in list `source_list` ref: list(set(source_list)) Output: list ( set ( t ) ) bleu_metric: 59.69491792019645 ---------- NL: removing duplicates in list `abracadabra` ref: list(OrderedDict.fromkeys('abracadabra')) Output: [ ('a ','b'), ('b ','c'), ('a ','d') bleu_metric: 7.555283720961675 ---------- NL: convert array `a` into a list ref: numpy.array(a).reshape(-1).tolist() Output: list ( a ) bleu_metric: 5.237603475197421 ---------- NL: convert the first row of numpy matrix `a` to a list ref: numpy.array(a)[0].tolist() Output: numpy. apply _ along _ axis ( numpy. array ( [ 0, 1, 2 ] ), a ) bleu_metric: 20.97823227594146 ---------- NL: in `soup`, get the content of the sibling of the `td` tag with text content `address:` ref: print(soup.find(text='Address:').findNext('td').contents[0]) Output: soup. find _ element _ by _ id ('< id >'). text bleu_metric: 12.033574792347215 ---------- NL: convert elements of each tuple in list `l` into a string separated by character `@` ref: """ """.join([('%d@%d' % t) for t in l]) Output: [''. join ( x ) for x in l ] bleu_metric: 17.41466588478686 ---------- NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) Output: " " " " " ". join ( [ x for x in l if x [ 0 ] in l ] ) bleu_metric: 36.45674601639763 ---------- NL: get the html from the current web page of a selenium driver ref: driver.execute_script('return document.documentElement.outerHTML;') Output: driver. get ('http : / / www. google. com') bleu_metric: 19.209534151258666 ---------- NL: get all matches with regex pattern `\\d+[xx]` in list of string `teststr` ref: [i for i in teststr if re.search('\\d+[xX]', i)] Output: [ i for i in teststr if re. search ('\ \ d + [ ^ \ \ ] * \ \ ] ', i ) bleu_metric: 69.89029944247109 ---------- NL: select values from column 'a' for which corresponding values in column 'b' will be greater than 50, and in column 'c' - equal 900 in dataframe `df` ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] Output: df [ ( df ['a'] > 1 ) & ( df ['b'] < = 5 ) ] bleu_metric: 39.042104382208294 ---------- NL: sort dictionary `o` in ascending order based on its keys and items ref: sorted(o.items()) Output: sorted ( list ( dictionary. items ( ) ), key = lambda x : x [ 1 ] ) bleu_metric: 24.42195895839763 ---------- NL: get sorted list of keys of dict `d` ref: sorted(d) Output: sorted ( d, key = d. get ) bleu_metric: 25.21193618434983 ---------- NL: how to sort dictionaries by keys in python ref: sorted(d.items()) Output: sorted ( d. items ( ) ) bleu_metric: 100.0 ---------- NL: convert string "1" into integer ref: int('1') Output: int ('1') bleu_metric: 100.0 ---------- NL: function to convert strings into integers ref: int() Output: map ( int,'1') bleu_metric: 21.105340631872636 ---------- NL: convert items in `t1` to integers ref: T2 = [map(int, x) for x in T1] Output: int ('\ n '. join ( str ( i ) for i in str ( n ) ) ) bleu_metric: 8.607692533178168 ---------- NL: call a shell script `./test.sh` using subprocess ref: subprocess.call(['./test.sh']) Output: subprocess. call ( ['test. sh ','/ test. sh otherfunc'] ) bleu_metric: 53.66411241731205 ---------- NL: call a shell script `notepad` using subprocess ref: subprocess.call(['notepad']) Output: subprocess. call ( ['notepad'] ) bleu_metric: 100.0 ---------- NL: combine lists `l1` and `l2` by alternating their elements ref: [val for pair in zip(l1, l2) for val in pair] Output: [ ( x + y ) for x in l2 for y in zip ( l1, l2 ) ] bleu_metric: 38.146366463982886 ---------- NL: encode string 'data to be encoded' ref: encoded = base64.b64encode('data to be encoded') Output: data. encode ('utf8') bleu_metric: 19.260282250359822 ---------- NL: encode a string `data to be encoded` to `ascii` encoding ref: encoded = 'data to be encoded'.encode('ascii') Output: data. encode ('ascii') bleu_metric: 41.51380951089673 ---------- NL: parse tab-delimited csv file 'text.txt' into a list ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) Output: txt = open ('myfile. txt ','r'). read ( ) bleu_metric: 20.692050735338164 ---------- NL: get attribute `my_str` of object `my_object` ref: getattr(my_object, my_str) Output: getattr ( my _ obj,'id') bleu_metric: 19.72940627795883 ---------- NL: group a list of dicts `ld` into one dict by key ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) Output: values = [ dict ( k, v ) for k, v in list ( d. items ( ) ) ] bleu_metric: 17.057966110550765 ---------- NL: how do i sum the first value in each tuple in a list of tuples in python? ref: sum([pair[0] for pair in list_of_pairs]) Output: sum ( [ item [ 0 ] for item in tuple ] ) bleu_metric: 41.37441202051881 ---------- NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary ref: d = ast.literal_eval("{'code1':1,'code2':1}") Output: print ('\ u0420 \ u043e \ u0441 \ u0438 \ u0438 \ u bleu_metric: 6.351019285070924 ---------- NL: find all words in a string `mystring` that start with the `$` sign ref: [word for word in mystring.split() if word.startswith('$')] Output: re. findall ('\ \ b [ ^ \ \ w \ \ ] ', mystring ) bleu_metric: 9.850775379403402 ---------- NL: remove any url within string `text` ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) Output: re. sub ('\ \. \ \ blog _ addr \ \ r \ \ n ','', text ) bleu_metric: 32.17551945847391 ---------- NL: replace all elements in array `a` that are not present in array `[1, 3, 4]` with zeros ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) Output: np. where ( np. in1d ( a, b ) ) [ 0 ] bleu_metric: 22.828499613963487 ---------- NL: calculate mean across dimension in a 2d array `a` ref: np.mean(a, axis=1) Output: np. mean ( a, axis = 1 ) bleu_metric: 100.0 ---------- NL: running r script '/pathto/myrscript.r' from python ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) Output: sys. path. append ('/ path / to / my / my / mypath') bleu_metric: 7.04483155666154 ---------- NL: run r script '/usr/bin/rscript --vanilla /pathto/myrscript.r' ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) Output: os. path. join ( root, '. / main _ folder') bleu_metric: 6.726052538373431 ---------- NL: add a header to a csv file ref: writer.writeheader() Output: writer. writeheader ( ) bleu_metric: 100.0 ---------- NL: replacing nan in the dataframe `df` with row average ref: df.fillna(df.mean(axis=1), axis=1) Output: df. fillna ( df. mean ( axis = 1 ), axis = 1 ) bleu_metric: 100.0 ---------- NL: convert unix timestamp '1347517370' to formatted string '%y-%m-%d %h:%m:%s' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) Output: datetime. strptime ('21 / 11 / 06 16 : 30 ','% d / % m / % y') bleu_metric: 10.247261229308657 ---------- NL: call a base class's class method `do` from derived class `derived` ref: super(Derived, cls).do(a) Output: super ( childclass, self ). _ _ init _ _ ( * args, * * kwargs ) bleu_metric: 11.96655750514248 ---------- NL: selecting rows in numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] Output: a [ np. arange ( 3 ), ( 0, 1, 0 ) ] bleu_metric: 13.581342454223687 ---------- NL: separate words delimited by one or more spaces into a list ref: re.split(' +', 'hello world sample text') Output: ['one ','two ','one ','two'] bleu_metric: 13.78592993183041 ---------- NL: length of longest element in list `words` ref: len(max(words, key=len)) Output: len ( max ( words, key = len ) ) bleu_metric: 100.0 ---------- NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` ref: result[0]['from_user'] Output: ['key1 ','key2 ','value'] bleu_metric: 18.20705281109213 ---------- NL: retrieve each line from a file 'file.txt' as a list ref: [line.split() for line in open('File.txt')] Output: print ( line. rstrip ('\ n') ) bleu_metric: 11.789701057610694 ---------- NL: swap keys with values in a dictionary `a` ref: res = dict((v, k) for k, v in a.items()) Output: a [ key ] = dict ( ( v ) for k, v in a. items ( ) ) bleu_metric: 73.78351342269067 ---------- NL: open a file `path/to/file_name.ext` in write mode ref: new_file = open('path/to/FILE_NAME.ext', 'w') Output: file _ name = os. path. join ( os. path. join ( dir, '.') ) bleu_metric: 10.713701843513144 ---------- NL: how to count distinct values in a column of a pandas group by object? ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() Output: df. groupby ( ['col1 ','col2'] ) ['col3']. apply ( lambda x : bleu_metric: 72.38800804180103 ---------- NL: check if any key in the dictionary `dict1` starts with the string `emp$$` ref: any(key.startswith('EMP$$') for key in dict1) Output: any ( isinstance ( x, int ( dict. values ( ) ) ) for x in dict _ string in input. lower ( bleu_metric: 8.839374326825924 ---------- NL: create list of values from dictionary `dict1` that have a key that starts with 'emp$$' ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] Output: [ dict ( zip ( k, v ), v ) for k, v in d. items ( ) if v. startswith ( ' bleu_metric: 24.14138031982595 ---------- NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) Output: pd. concat ( [ df1, df2 ], axis = 1 ) bleu_metric: 8.12657367724027 ---------- NL: print elements of list `list` seperated by tabs `\t` ref: print('\t'.join(map(str, list))) Output: print ( [ i. partition ('\ t') for i in list _ of _ lists ] ) bleu_metric: 25.58058053378779 ---------- NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) Output: print ('\ xc5 \ xa4 \ xd0 \ x80 \ xd0 \ xb0 \ xd0 bleu_metric: 29.472590166651962 ---------- NL: encode a latin character in string `sopet\xc3\xb3n` properly ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') Output: " " " \ xc3 \ xa3 \ x9f " " ". encode ('utf - 8') bleu_metric: 46.255815148030386 ---------- NL: resized image `image` to width, height of `(x, y)` with filter of `antialias` ref: image = image.resize((x, y), Image.ANTIALIAS) Output: img = np. array ( [ 0, 1, 0, 1, 2, - 1, 0, 1, 2 ] ) bleu_metric: 7.278748444764005 ---------- NL: regex, find "n"s only in the middle of string `s` ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) Output: re. findall ('(? < = [ ^ \ \ s ] ) (? = [ ^ \ \ s ] + ) ', bleu_metric: 44.74679573461579 ---------- NL: display the float `1/3*100` as a percentage ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) Output: print ( ('0. 1 ', 1 ) ) bleu_metric: 11.406542828786534 ---------- NL: sort a list of dictionary `mylist` by the key `title` ref: mylist.sort(key=lambda x: x['title']) Output: mylist. sort ( key = lambda x : x ['title'] ) bleu_metric: 100.0 ---------- NL: sort a list `l` of dicts by dict value 'title' ref: l.sort(key=lambda x: x['title']) Output: l. sort ( key = lambda x : x ['title'] ) bleu_metric: 100.0 ---------- NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) Output: sorted ( l, key = lambda x : x ['name'] ) bleu_metric: 17.362697834284052 ---------- NL: find 10 largest differences between each respective elements of list `l1` and list `l2` ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) Output: [ ( x, y ) for x, y in zip ( l1, l2 ) ] bleu_metric: 5.935298261592072 ---------- NL: beautifulsoup find all 'span' elements in html string `soup` with class of 'stargryb sp' ref: soup.find_all('span', {'class': 'starGryB sp'}) Output: soup. findall ( text ='div ', style ='width = 300px ;') bleu_metric: 11.970571135993998 ---------- NL: write records in dataframe `df` to table 'test' in schema 'a_schema' ref: df.to_sql('test', engine, schema='a_schema') Output: df. plot ( row ='test ','test _ txt') bleu_metric: 32.62478546610937 ---------- NL: extract brackets from string `s` ref: brackets = re.sub('[^(){}[\\]]', '', s) Output: re. sub ('\ \ [ ( \ \ w + ) \ \ s + \ \ ] ','', s ) bleu_metric: 47.1113978575501 ---------- NL: remove duplicate elements from list 'l' ref: list(dict((x[0], x) for x in L).values()) Output: [ k for k, g in l if'l'in k ] bleu_metric: 6.942420857748657 ---------- NL: read a file `file` without newlines ref: [line.rstrip('\n') for line in file] Output: file. write ('\ n') bleu_metric: 30.98188793972842 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 9.243620302376062 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 9.243620302376062 ---------- NL: get the position of item 1 in `testlist` ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: pass Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 7.565780264883953 ---------- NL: get the position of item 1 in `testlist` ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): pass Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 6.0168435849456285 ---------- NL: get the position of item 1 in `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) for i in gen: pass Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 5.043459282125431 ---------- NL: get the position of item `element` in list `testlist` ref: print(testlist.index(element)) Output: print ( testlist. index ( element ) ) bleu_metric: 100.0 ---------- NL: get the position of item `element` in list `testlist` ref: try: print(testlist.index(element)) except ValueError: pass Output: print ( testlist. index ( element ) ) bleu_metric: 49.01732980393223 ---------- NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` ref: max(lis, key=lambda item: item[1])[0] Output: max ( x [ 0 ] for x in rows ) bleu_metric: 14.988854080083206 ---------- NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` ref: max(lis, key=itemgetter(1))[0] Output: max ( x, key = lambda x : x [ 1 ] ) [ 0 ] bleu_metric: 35.27295712700594 ---------- NL: make a delay of 1 second ref: time.sleep(1) Output: time. sleep ( 0. 1 ) bleu_metric: 52.53819788848316 ---------- NL: convert list of tuples `l` to a string ref: """, """.join('(' + ', '.join(i) + ')' for i in L) Output: " " " " " ". join ( l ) bleu_metric: 14.164334116374599 ---------- NL: django set default value of field `b` equal to '0000000' ref: b = models.CharField(max_length=7, default='0000000', editable=False) Output: obj. save ('new _ name ','default _ number') bleu_metric: 10.318679011582763 ---------- NL: sort lis `list5` in ascending order based on the degrees value of its elements ref: sorted(list5, lambda x: (degree(x), x)) Output: sorted ( lst, key = lambda x : x [ 1 ] ) bleu_metric: 19.143591202259493 ---------- NL: how do i perform secondary sorting in python? ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) Output: sorted ( list5, key = lambda x : ( sum ( x [ 1 ] ), x ) ) bleu_metric: 41.26152034907945 ---------- NL: convert a list into a generator object ref: (n for n in [1, 2, 3, 5]) Output: list ( itertools. combinations ( * ( l ) ) ) bleu_metric: 9.930751066504135 ---------- NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] Output: [ x for x in set ( split _ list [ i ] for i in range ( len ( x ) ) if i not in index ] bleu_metric: 18.860270984795903 ---------- NL: open a file `yourfile.txt` in write mode ref: f = open('yourfile.txt', 'w') Output: os. system ('your string. txt') bleu_metric: 23.571525044211057 ---------- NL: get attribute 'attr' from object `obj` ref: getattr(obj, 'attr') Output: getattr ( obj,'attr') bleu_metric: 100.0 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple ref: from functools import reduce reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) Output: tuple ( ['a ','b ','c ','d'] ) bleu_metric: 6.101077050680879 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) Output: map ( lambda s : ( d ['a'], d ['b'], d ['c'] ), d [ bleu_metric: 11.200083731986627 ---------- NL: python pandas: how to replace a characters in a column of a dataframe? ref: df['range'].replace(',', '-', inplace=True) Output: df ['brandname'] = df ['brandname']. replace ('- ', ','in') bleu_metric: 45.97006398721532 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip ( * [ ('a ','b ','c ','d'] ) ) bleu_metric: 36.501102157810664 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip ( * [ ('a ','b ','c ','d'] ) ) bleu_metric: 36.501102157810664 ---------- NL: unzip list `original` ref: result = ([a for (a, b) in original], [b for (a, b) in original]) Output: zip ( * list ( range ( 10 ) ) ) bleu_metric: 4.606184881898074 ---------- NL: unzip list `original` and return a generator ref: result = ((a for (a, b) in original), (b for (a, b) in original)) Output: [ i for i in range ( 1, len ( l ) ) if i! ='+ 1 ] bleu_metric: 7.62604991346693 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip ( * [ ('a ','b ','c ','d ','e'] ) ) bleu_metric: 34.87995424158042 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with none ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip ( * [ ('a ','b'), ('b ','c ','d'), ( ' bleu_metric: 35.430131828411845 ---------- NL: encode `decimal('3.9')` to a json string ref: json.dumps(Decimal('3.9')) Output: json. dumps ( ['1 ','2 ','3'] ) bleu_metric: 23.58844810653421 ---------- NL: add key "mynewkey" to dictionary `d` with value "mynewvalue" ref: d['mynewkey'] = 'mynewvalue' Output: df. setdefault ('name ', [ ] ). append ( value ) bleu_metric: 9.50724412002623 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update({'a': 1, }) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 37.863495563972855 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update(dict(a=1)) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 8.24055698798382 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update(a=1) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 8.24055698798382 ---------- NL: find maximal value in matrix `matrix` ref: max([max(i) for i in matrix]) Output: max ( matrix, key = lambda x : max ( x ) ) bleu_metric: 16.943571815930884 ---------- NL: round number `answer` to 2 precision after the decimal point ref: answer = str(round(answer, 2)) Output: round ( 1. 0005, 3 ) bleu_metric: 17.51643270174889 ---------- NL: extract ip address from an html string ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) Output: print ( re. findall ('[ ^ a - za - z0 - 9 - ] + (? : \ \. [ a - bleu_metric: 40.70867848415918 ---------- NL: filter dataframe `df` by values in column `a` that appear more than once ref: df.groupby('A').filter(lambda x: len(x) > 1) Output: df. filter ( df. filter ( lambda x : x. any ( axis = 1 ) ) ) bleu_metric: 34.640201116587235 ---------- NL: append each line in file `myfile` into a list ref: [x for x in myfile.splitlines() if x != ''] Output: myfile. write ('\ n '. join ( [ str ( i ) for i in myfile ] ) ) bleu_metric: 10.435185733665413 ---------- NL: get a list of integers `lst` from a file `filename.txt` ref: lst = map(int, open('filename.txt').readlines()) Output: [ int ( x ) for x in lst ] bleu_metric: 7.020674481218203 ---------- NL: add color bar with image `mappable` to plot `plt` ref: plt.colorbar(mappable=mappable, cax=ax3) Output: plt. show ( ) bleu_metric: 11.914378950211951 ---------- NL: count most frequent 100 words in column 'text' of dataframe `df` ref: Counter(' '.join(df['text']).split()).most_common(100) Output: df. groupby ('text'). apply ( lambda x : x. sum ( ) ). most _ common ( 1 ) bleu_metric: 21.52643286782715 ---------- NL: python split a string using regex ref: re.findall('(.+?):(.+?)\\b ?', text) Output: re. split ('(? < = \ \ d ) \ \ d + ', text ) bleu_metric: 29.769819080461428 ---------- NL: generate all 2-element subsets of tuple `(1, 2, 3)` ref: list(itertools.combinations((1, 2, 3), 2)) Output: list ( itertools. product ( [ 1, 2, 3 ], [ 4, 5, 6 ] ) ) bleu_metric: 34.29547961820059 ---------- NL: get a value of datetime.today() in the utc time zone ref: datetime.now(pytz.utc) Output: pytz. utcnow ( ) bleu_metric: 23.206041459353084 ---------- NL: get a new list `list2`by removing empty list from a list of lists `list1` ref: list2 = [x for x in list1 if x != []] Output: list2 = [ x for x in list1 if x [ : ] = = [ ] ] bleu_metric: 69.3395566222006 ---------- NL: create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` ref: list2 = [x for x in list1 if x] Output: list ( zip ( a, b ) ) bleu_metric: 10.320893749383378 ---------- NL: django response with json `data` ref: return HttpResponse(data, mimetype='application/json') Output: return httpresponse ( json. dumps ( response _ data ), content _ type ='application / json') bleu_metric: 36.17640392425988 ---------- NL: get all text that is not enclosed within square brackets in string `example_str` ref: re.findall('(.*?)\\[.*?\\]', example_str) Output: str1. split ('\ \ ) [ ^ \ \ ] ', str. strip ( ) bleu_metric: 26.91730507369109 ---------- NL: use a regex to get all text in a string `example_str` that is not surrounded by square brackets ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) Output: re. findall ('\ \ [ (? : \ \ w + ) \ \ ] ', str1 ) bleu_metric: 35.15147667690756 ---------- NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') Output: re. sub ('\ \ ( [ ^ \ \ w ] ) ','', filename ) bleu_metric: 27.664786097762494 ---------- NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') Output: re. sub ('(? < = \ \ w ) ( \ \ w + ) ( \ \ w + ) ','\ \ bleu_metric: 30.955387518700363 ---------- NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') Output: re. findall ('(? < = \ \ w ) ( [ ^ \ \ w \ \ w ] | \ \ w + ) bleu_metric: 29.552612305356018 ---------- NL: formate each string cin list `elements` into pattern '%{0}%' ref: elements = ['%{0}%'.format(element) for element in elements] Output: " " " { 0 :. 2f } " " ". format ('foo ','bar') bleu_metric: 17.414675099971177 ---------- NL: open a background process 'background-process' with arguments 'arguments' ref: subprocess.Popen(['background-process', 'arguments']) Output: os. system ('some _ command - some _ file') bleu_metric: 12.107470158860224 ---------- NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' ref: [mydict[x] for x in mykeys] Output: [ key for item in my _ list if item. get ('name ','age') ] bleu_metric: 9.119675426861836 ---------- NL: convert list `[('name', 'joe'), ('age', 22)]` into a dictionary ref: dict([('Name', 'Joe'), ('Age', 22)]) Output: dict ( ( ('class ', 2 ), ('class ', 3 ) ) ) bleu_metric: 24.080231808676874 ---------- NL: average each two columns of array `data` ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) Output: np. mean ( np. arange ( len ( df. columns ) ), axis = 1 ) bleu_metric: 18.410768031391818 ---------- NL: double backslash escape all double quotes in string `s` ref: print(s.encode('unicode-escape').replace('"', '\\"')) Output: print ( s. split ('\ \') ) bleu_metric: 21.765088513075124 ---------- NL: split a string into a list of words and whitespace ref: re.split('(\\W+)', s) Output: ['this is a string : ','this is a string ','this is a string ','this is a string ', bleu_metric: 5.648892151960283 ---------- NL: plotting stacked barplots on a panda data frame ref: df.plot(kind='barh', stacked=True) Output: df. plot ( kind ='barh ', stacked = true ) bleu_metric: 83.7117009877792 ---------- NL: reverse the keys and values in a dictionary `mydictionary` ref: {i[1]: i[0] for i in list(myDictionary.items())} Output: dict ( ( k, v ) for k, v in list ( d. items ( ) ) ) bleu_metric: 23.690464140247087 ---------- NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'mylist'. ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] Output: ['x ','y ','red ','var ','var'] bleu_metric: 6.180827527485682 ---------- NL: check if object `obj` is a string ref: isinstance(obj, str) Output: isinstance ( obj, str ) bleu_metric: 100.0 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance ( s, str ) bleu_metric: 59.46035575013605 ---------- NL: check if object `o` is a string ref: (type(o) is str) Output: isinstance ( s, str ) bleu_metric: 23.813694985189848 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance ( s, str ) bleu_metric: 59.46035575013605 ---------- NL: check if `obj_to_test` is a string ref: isinstance(obj_to_test, str) Output: print ( obj. isfile ('attr name') ) bleu_metric: 13.508625657351411 ---------- NL: append list `list1` to `list2` ref: list2.extend(list1) Output: [ list1. append ( list1 ) for a in range ( 3 ) ] bleu_metric: 14.879641171245488 ---------- NL: append list `mylog` to `list1` ref: list1.extend(mylog) Output: list3. append ( list1 ) bleu_metric: 29.071536848410968 ---------- NL: append list `a` to `c` ref: c.extend(a) Output: a. append ( a ) bleu_metric: 54.10822690539396 ---------- NL: append items in list `mylog` to `list1` ref: for line in mylog: list1.append(line) Output: list3. append ( list1 ) bleu_metric: 20.917479021833486 ---------- NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` ref: b.append((a[0][0], a[0][2])) Output: a = [ [ ] for i in range ( 0, len ( a ), [ 3, 4 ] ] ) bleu_metric: 11.556647985416685 ---------- NL: initialize `secret_key` in flask config with `your_secret_string ` ref: app.config['SECRET_KEY'] = 'Your_secret_string' Output: app. config ['your _ id'] ='your _ config ' bleu_metric: 43.343660127583234 ---------- NL: unpack a series of tuples in pandas into a dataframe with column names 'out-1' and 'out-2' ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) Output: pd. dataframe ( df. values. tolist ( ), columns = ['a ','b'], axis = 1 bleu_metric: 35.31494509780309 ---------- NL: find the index of an element 'msft' in a list `stocks_list` ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] Output: print ( list ( enumerate ( my _ list ) ) ) bleu_metric: 5.9394573438219 ---------- NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable ref: ax.set_xticklabels(labels, rotation=45) Output: ax. set _ xticklabels ( ax. xaxis. get _ majorticklabels ( ), rotation = 45 ) bleu_metric: 29.88309778054086 ---------- NL: remove symbols from a string `s` ref: re.sub('[^\\w]', ' ', s) Output: re. sub ('[ ^ a - z ] ','', s ) bleu_metric: 74.83293841345244 ---------- NL: get the current directory of a script ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) Output: os. path. dirname ( os. path. abspath ( _ _ file _ _ ) ) bleu_metric: 48.431464491050775 ---------- NL: find octal characters matches from a string `str` using regex ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) Output: re. findall ('\ \ [ (? < = \ \ d ) \ \ ) ','- ', str ) bleu_metric: 34.98979283499355 ---------- NL: split string `input` based on occurrences of regex pattern '[ ](?=[a-z]+\\b)' ref: re.split('[ ](?=[A-Z]+\\b)', input) Output: re. findall ('(? < = [ a - z ] ) (? = [ a - z ] ) (? = [ a bleu_metric: 16.927318970546587 ---------- NL: split string `input` at every space followed by an upper-case letter ref: re.split('[ ](?=[A-Z])', input) Output: re. split ('\ \ b ', text ) bleu_metric: 23.369549958807866 ---------- NL: send multipart encoded file `files` to url `url` with headers `headers` and metadata `data` ref: r = requests.post(url, files=files, headers=headers, data=data) Output: r = requests. get ( url ) bleu_metric: 13.273006566054132 ---------- NL: write bytes `bytes_` to a file `filename` in python 3 ref: open('filename', 'wb').write(bytes_) Output: f. write ( struct. pack ('> i ', filename ) ) bleu_metric: 21.042990347620457 ---------- NL: get a list from a list `lst` with values mapped into a dictionary `dct` ref: [dct[k] for k in lst] Output: [ list ( lst [ i ] ) for i in lst ] bleu_metric: 28.787878181011266 ---------- NL: find duplicate names in column 'name' of the dataframe `x` ref: x.set_index('name').index.get_duplicates() Output: print ( list ( df. index. get _ duplicates ( ) ) ) bleu_metric: 19.923405658137924 ---------- NL: truncate float 1.923328437452 to 3 decimal places ref: round(1.923328437452, 3) Output: round ( 1. 005, 2 ) bleu_metric: 46.71379777282001 ---------- NL: sort list `li` in descending order based on the date value in second element of each list in list `li` ref: sorted(li, key=lambda x: datetime.strptime(x[1], '%d/%m/%Y'), reverse=True) Output: sorted ( li, key = operator. itemgetter ( 1 ), reverse = true ) bleu_metric: 19.26332463060877 ---------- NL: place the radial ticks in plot `ax` at 135 degrees ref: ax.set_rlabel_position(135) Output: ax. set _ xticklabels ( [ ] ) bleu_metric: 21.834177214239062 ---------- NL: check if path `my_path` is an absolute path ref: os.path.isabs(my_path) Output: os. path. exists ('my _ path') bleu_metric: 31.61487584488943 ---------- NL: get number of keys in dictionary `yourdict` ref: len(list(yourdict.keys())) Output: len ( mydict ) bleu_metric: 11.141275535087015 ---------- NL: count the number of keys in dictionary `yourdictfile` ref: len(set(open(yourdictfile).read().split())) Output: sum ( len ( v ) for v in yourdict. values ( ) ) bleu_metric: 18.767030063813223 ---------- NL: pandas dataframe get first row of each group by 'id' ref: df.groupby('id').first() Output: df. groupby ('id'). first ( ). first ( ) bleu_metric: 75.62722459724604 ---------- NL: split a list in first column into multiple columns keeping other columns as well in pandas data frame ref: pd.concat([df[0].apply(pd.Series), df[1]], axis=1) Output: df [ ['c ','df'] ]. apply ( lambda x : x [ 1 : - 1 ]. split ( bleu_metric: 19.037861963633805 ---------- NL: extract attributes 'src="js/([^"]*\\bjquery\\b[^"]*)"' from string `data` ref: re.findall('src="js/([^"]*\\bjquery\\b[^"]*)"', data) Output: " " " \ \ b " " " bleu_metric: 3.6253194206622026 ---------- NL: sum integers contained in strings in list `['', '3.4', '', '', '1.0']` ref: sum(int(float(item)) for item in [_f for _f in ['', '3.4', '', '', '1.0'] if _f]) Output: sum ( [ 1, 2, 3, 4, 5 ] ) bleu_metric: 3.255351208100268 ---------- NL: call a subprocess with arguments `c:\\program files\\vmware\\vmware server\\vmware-cmd.bat` that may contain spaces ref: subprocess.Popen(['c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat']) Output: subprocess. call ( ['shutdown ','/ r ','/ tmp / test. py'] ) bleu_metric: 16.665685159230854 ---------- NL: reverse a priority queue `q` in python without using classes ref: q.put((-n, n)) Output: t. start ( ) bleu_metric: 12.151662434083677 ---------- NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) Output: df. groupby ('o') ['value']. first ( ) bleu_metric: 3.5795004593328636 ---------- NL: find all matches of regex pattern '([a-fa-f\\d]{32})' in string `data` ref: re.findall('([a-fA-F\\d]{32})', data) Output: re. findall ('(? < = [ a - z ] ) ( [ a - z ] ) ','abcd _ string bleu_metric: 33.336490646495264 ---------- NL: get the length of list `my_list` ref: len(my_list) Output: len ( my _ list ) bleu_metric: 30.739407647563223 ---------- NL: getting the length of array `l` ref: len(l) Output: len ( l ) bleu_metric: 100.0 ---------- NL: getting the length of array `s` ref: len(s) Output: len ( s ) bleu_metric: 100.0 ---------- NL: getting the length of `my_tuple` ref: len(my_tuple) Output: len ( my _ list ) bleu_metric: 30.739407647563223 ---------- NL: getting the length of `my_string` ref: len(my_string) Output: len ( my _ string ) bleu_metric: 30.739407647563223 ---------- NL: remove escape character from string "\\a" ref: """\\a""".decode('string_escape') Output: print ('\ n '. join ('[ _ name _ ]') ) bleu_metric: 15.071676257541073 ---------- NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') Output: a = re. sub ('[ a - za - z0 - 9 ] ','', line ) bleu_metric: 6.256610657452671 ---------- NL: remove directory tree '/folder_name' ref: shutil.rmtree('/folder_name') Output: os. path. dirname ( os. path. join ( dirpath, os. path. dirname ( _ _ file _ _ ) ) bleu_metric: 6.4718242450883325 ---------- NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) Output: df. apply ( lambda x : x. to _ frame ('new _ column'), axis = 1 ) bleu_metric: 34.619282106807496 ---------- NL: reverse sort counter `x` by values ref: sorted(x, key=x.get, reverse=True) Output: sorted ( x, key = itemgetter ( 1 ) ) bleu_metric: 40.540555470323305 ---------- NL: reverse sort counter `x` by value ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) Output: sorted ( x, key = itemgetter ( 1 ), reverse = true ) bleu_metric: 15.53218858512746 ---------- NL: append a numpy array 'b' to a numpy array 'a' ref: np.vstack((a, b)) Output: np. concatenate ( ( a, b ) ) bleu_metric: 76.91605673134586 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: print(concatenate((a, b), axis=0)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 82.82477531331041 ---------- NL: numpy concatenate two arrays `a` and `b` along the second axis ref: print(concatenate((a, b), axis=1)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 100.0 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: c = np.r_[(a[None, :], b[None, :])] Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 9.520068110441361 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: np.array((a, b)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 41.70507291634131 ---------- NL: fetch address information for host 'google.com' ion port 80 ref: print(socket.getaddrinfo('google.com', 80)) Output: os. environ ['localhost : \ \ python27 \ \ python'] bleu_metric: 9.672649511413093 ---------- NL: add a column 'day' with value 'sat' to dataframe `df` ref: df.xs('sat', level='day', drop_level=False) Output: df ['value'] = df ['value']. apply ( lambda x : x ['value'] ) bleu_metric: 8.179879050970094 ---------- NL: return a 401 unauthorized in django ref: return HttpResponse('Unauthorized', status=401) Output: return httpresponse ( status = 204 ) bleu_metric: 15.451666492113134 ---------- NL: flask set folder 'wherever' as the default template folder ref: Flask(__name__, template_folder='wherever') Output: app. run ( debug = true ) bleu_metric: 16.89983564524028 ---------- NL: how do i insert into t1 (select * from t2) in sqlalchemy? ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') Output: session. execute ('select * from from t1 ;') bleu_metric: 34.9740896964685 ---------- NL: sort a list of lists 'c2' such that third row comes first ref: c2.sort(key=lambda row: row[2]) Output: sorted ( l, key = lambda x : ( - x [ 1 ], x [ 2 ] ) ) bleu_metric: 22.169194554222997 ---------- NL: sorting a list of lists in python ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) Output: [ ( row [ i ], row [ i + 2 ] ) for i in range ( 0, len ( row ), row [ 2 ] bleu_metric: 26.189293164591476 ---------- NL: sorting a list of lists in python ref: c2.sort(key=lambda row: (row[2], row[1])) Output: [ ( row [ i ], row [ i + 2 ] ) for i in range ( 0, len ( row ), row [ 2 ] bleu_metric: 22.303080489498637 ---------- NL: set font `arial` to display non-ascii characters in matplotlib ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) Output: ax. set _ ylabel ('y - r ', title ='\ u2022') bleu_metric: 6.906418747057865 ---------- NL: convert datetime column 'date' of pandas dataframe 'df' to ordinal ref: df['date'].apply(lambda x: x.toordinal()) Output: df ['date'] = df ['date']. map ( lambda x : x. date ( ) ) bleu_metric: 51.29497107827519 ---------- NL: get html source of selenium webelement `element` ref: element.get_attribute('innerHTML') Output: print ( element. get _ attribute ('innerhtml') ) bleu_metric: 18.69300079996002 ---------- NL: get the integer location of a key `bob` in a pandas data frame ref: df.index.get_loc('bob') Output: df. reset _ index ( ) bleu_metric: 19.840332506055578 ---------- NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') Output: os. system ('echo $ filename. exe') bleu_metric: 12.044064401053362 ---------- NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` ref: my_dict.update({'third_key': 1}) Output: my _ dict ['my _ dict'] = var bleu_metric: 10.579369505074823 ---------- NL: declare an array ref: my_list = [] Output: variable = [ ] bleu_metric: 75.98356856515926 ---------- NL: insert item `12` to a list `my_list` ref: my_list.append(12) Output: my _ list. append ('{ }, { } '. format ( c. c ) ) bleu_metric: 12.259149958656423 ---------- NL: add an entry 'wuggah' at the beginning of list `mylist` ref: myList.insert(0, 'wuggah') Output: mylist = [ 'an ','i ','j'] bleu_metric: 11.502783619900047 ---------- NL: convert a hex-string representation to actual bytes ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') Output: " " " \ \ xc3 \ \ x85あ " " ". encode ('hex') bleu_metric: 24.712086993335753 ---------- NL: select the last column of dataframe `df` ref: df[df.columns[-1]] Output: df [ df. columns [ - 1 ] ] bleu_metric: 100.0 ---------- NL: get the first value from dataframe `df` where column 'letters' is equal to 'c' ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] Output: df ['value'] = df. groupby ('value') ['value']. apply ( lambda x : x bleu_metric: 17.966302274629616 ---------- NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix ref: np.column_stack(([1, 2, 3], [4, 5, 6])) Output: matrix ( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ) bleu_metric: 35.61693754184905 ---------- NL: get the type of `i` ref: type(i) Output: print ( len ( a ) ) bleu_metric: 22.679164443904003 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: get the type of variable `variable_name` ref: print(type(variable_name)) Output: globals ( ) ['a'] bleu_metric: 16.784459625186194 ---------- NL: get the 5th item of a generator ref: next(itertools.islice(range(10), 5, 5 + 1)) Output: next ( ( iter ( range ( n ) ) ) for i in range ( n ) ) bleu_metric: 18.81478574691708 ---------- NL: print a string `word` with string format ref: print('"{}"'.format(word)) Output: print ('\ n '. join ('% s = % s'% ( value, ) ) ) bleu_metric: 16.331948281960493 ---------- NL: join a list of strings `list` using a space ' ' ref: """ """.join(list) Output: list (''. join ( map ( str, list ) ) ) bleu_metric: 33.918247054808944 ---------- NL: create list `y` containing two empty lists ref: y = [[] for n in range(2)] Output: y = [ ( x, y ) for x in zip ( l1, l2 ) ] bleu_metric: 18.951629567590743 ---------- NL: read a file 'c:/name/mydocuments/numbers' into a list `data` ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] Output: new _ list = [ line. rstrip ('\ t') for line in open ('filename. txt') ] bleu_metric: 37.34651115614923 ---------- NL: delete all occurrences of character 'i' in string 'it is icy' ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: re. sub ('i ','','it is icy') bleu_metric: 19.720950806796015 ---------- NL: delete all instances of a character 'i' in a string 'it is icy' ref: re.sub('i', '', 'it is icy') Output: re. sub ('i ','','it is icy') bleu_metric: 100.0 ---------- NL: delete all characters "i" in string "it is icy" ref: """it is icy""".replace('i', '') Output: " " " a b : b. c '! ='a') bleu_metric: 23.234178119893244 ---------- NL: how to delete all instances of a character in a string in python? ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: " " " " " ". join ( [ char for char in'it is icy'if char! ='i'] ) bleu_metric: 100.0 ---------- NL: drop rows of pandas dataframe `df` having nan in column at index "1" ref: df.dropna(subset=[1]) Output: df. drop ( df. index. duplicated ( ) bleu_metric: 17.996531271765896 ---------- NL: get elements from list `mylist`, that have a field `n` value 30 ref: [x for x in myList if x.n == 30] Output: mylist = [ i for i in range ( 10 ) if i % 2 = = 0 ] bleu_metric: 11.709061630923735 ---------- NL: converting list of strings `intstringlist` to list of integer `nums` ref: nums = [int(x) for x in intstringlist] Output: nums = [ int ( x ) for x in intstringlist ] bleu_metric: 100.0 ---------- NL: convert list of string numbers into list of integers ref: map(int, eval(input('Enter the unfriendly numbers: '))) Output: [ int ( s ) for s in example _ string. split ( ',') ] bleu_metric: 11.433361115787454 ---------- NL: print "." without newline ref: sys.stdout.write('.') Output: sys. stdout. write ( '.') bleu_metric: 100.0 ---------- NL: round off the float that is the product of `2.52 * 100` and convert it to an int ref: int(round(2.51 * 100)) Output: int ( round ( 1123. 456789, 0 ) ) bleu_metric: 39.832871551569504 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for file in glob.glob('*.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 25.1696695878184 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for file in os.listdir('/mydir'): if file.endswith('.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 95.90965597935381 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for (root, dirs, files) in os.walk('/mydir'): for file in files: if file.endswith('.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 50.25808484829805 ---------- NL: plot dataframe `df` without a legend ref: df.plot(legend=False) Output: df. legend ( ). plot ( kind ='bar') bleu_metric: 21.305413619585085 ---------- NL: loop through the ip address range "192.168.x.x" ref: for i in range(256): for j in range(256): ip = ('192.168.%d.%d' % (i, j)) print(ip) Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 13.252382555750852 ---------- NL: loop through the ip address range "192.168.x.x" ref: for (i, j) in product(list(range(256)), list(range(256))): pass Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 5.334065332387164 ---------- NL: loop through the ip address range "192.168.x.x" ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 61.58601992013119 ---------- NL: sum the corresponding decimal values for binary values of each boolean element in list `x` ref: sum(1 << i for i, b in enumerate(x) if b) Output: float ( [ x for x in list ( d. values ( ) ) ] ) bleu_metric: 11.156857921595945 ---------- NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) Output: result = [ line. split ( ) for line in open ('filename. txt ','r') ] bleu_metric: 7.848329424931549 ---------- NL: convert list of lists `data` into a flat list ref: [y for x in data for y in (x if isinstance(x, list) else [x])] Output: [ a for b in c for a in list _ of _ b ] bleu_metric: 8.372912227572577 ---------- NL: print new line character as `\n` in a string `foo\nbar` ref: print('foo\nbar'.encode('string_escape')) Output: print ('\ n '. join ( chr ( i ) for i in text ) ) bleu_metric: 19.268479640608692 ---------- NL: remove last comma character ',' in string `s` ref: """""".join(s.rsplit(',', 1)) Output: s = s. replace ( ', ','' ) bleu_metric: 24.4545047029932 ---------- NL: calculate the mean of each element in array `x` with the element previous to it ref: (x[1:] + x[:-1]) / 2 Output: np. mean ( x, y ) bleu_metric: 8.67666492910357 ---------- NL: get an array of the mean of each two consecutive values in numpy array `x` ref: x[:-1] + (x[1:] - x[:-1]) / 2 Output: np. array ( [ [ 2, 3, 4 ], [ 2, 5, 6 ] ] ) bleu_metric: 9.410489957951636 ---------- NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype=' 1] Output: [ k for k, v in mylist if v. startswith ('mycount') else v for k, v in mylist bleu_metric: 34.06227682100307 ---------- NL: Insert directory 'apps' into directory `__file__` ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) Output: urllib. request. urlopen ('http : / / www. example. com ','/ path') bleu_metric: 9.10451572772839 ---------- NL: modify sys.path for python module `subdir` ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) Output: subprocess. call ( ['start / subdir / bin / 2014 ','inr'] ) bleu_metric: 8.368553758184737 ---------- NL: Insert a 'None' value into a SQLite3 table. ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) Output: cursor. execute ('select * from table where column in l') bleu_metric: 13.62767317410906 ---------- NL: flatten list `list_of_menuitems` ref: [image for menuitem in list_of_menuitems for image in menuitem] Output: [ item for sublist in list _ of _ lists ] bleu_metric: 17.194656088289214 ---------- NL: append elements of a set `b` to a list `a` ref: a.extend(b) Output: a. extend ( b ) bleu_metric: 100.0 ---------- NL: Append elements of a set to a list in Python ref: a.extend(list(b)) Output: a. extend ( b ) bleu_metric: 46.81294641285289 ---------- NL: write the data of dataframe `df` into text file `np.txt` ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') Output: df. to _ csv ('filename. txt ', header = false ) bleu_metric: 14.209004647554735 ---------- NL: write content of DataFrame `df` into text file 'c:\\data\\pandas.txt' ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') Output: df. to _ csv ('c : \ \ data \ \ t. txt ', sep ='\ t ', index bleu_metric: 37.50344293811855 ---------- NL: Split a string `x` by last occurrence of character `-` ref: print(x.rpartition('-')[0]) Output: x. split ('-') [ : - 1 ] bleu_metric: 43.353400970949984 ---------- NL: get the last part of a string before the character '-' ref: print(x.rsplit('-', 1)[0]) Output: re. sub ('(? < = - -. ) ','- ', line ) bleu_metric: 20.776569671187676 ---------- NL: upload file using FTP ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) Output: ftp. quit ( ) bleu_metric: 3.710176016036221 ---------- NL: add one to the hidden web element with id 'XYZ' with selenium python script ref: browser.execute_script("document.getElementById('XYZ').value+='1'") Output: driver. execute _ script ('window. scrollto ( 0, document. body. com') ) bleu_metric: 10.807499589580493 ---------- NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` ref: np.maximum([2, 3, 4], [1, 5, 2]) Output: np. array ( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ) bleu_metric: 28.489318277723964 ---------- NL: print a list `l` and move first 3 elements to the end of the list ref: print(l[3:] + l[:3]) Output: print ( [ l [ 3 : ] for l in l ] ) bleu_metric: 43.472087194499146 ---------- NL: loop over files in directory '.' ref: for fn in os.listdir('.'): if os.path.isfile(fn): pass Output: for ( root, dirs, dirs ) in os. walk ( '.') : if os. path. isdir ( ' bleu_metric: 46.596572764176955 ---------- NL: loop over files in directory `source` ref: for (root, dirs, filenames) in os.walk(source): for f in filenames: pass Output: os. listdir ('os') bleu_metric: 4.9782661836656885 ---------- NL: create a random list of integers ref: [int(1000 * random.random()) for i in range(10000)] Output: random. choice ( [ int ( i ) for i in range ( 10 ) ] ) bleu_metric: 38.95592996402149 ---------- NL: Using %f with strftime() in Python to get microseconds ref: datetime.datetime.now().strftime('%H:%M:%S.%f') Output: datetime. datetime. now ( ). strftime ('% h : % m : % s. % f') bleu_metric: 69.80972118841675 ---------- NL: Google App Engine execute GQL query 'SELECT * FROM Schedule WHERE station = $1' with parameter `foo.key()` ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) Output: c. objects. filter ( id = 1 ). order _ by ('foobar') bleu_metric: 10.30143863225941 ---------- NL: filter rows in pandas starting with alphabet 'f' using regular expression. ref: df.b.str.contains('^f') Output: df ['a']. str. contains ('c') bleu_metric: 43.85068972747104 ---------- NL: print a 2 dimensional list `tab` as a table with delimiters ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) Output: print ('\ n '. join ( map ( str, row ) ) ) bleu_metric: 28.575895973659073 ---------- NL: pandas: delete rows in dataframe `df` based on multiple columns values ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() Output: df. drop ( df. index. map ( lambda x : x. index, axis = 1 ) ) bleu_metric: 13.21619418680914 ---------- NL: format the variables `self.goals` and `self.penalties` using string formatting ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) Output: " " " { } \ \ w { }. { } " " ". format ( date,'user') bleu_metric: 28.34142738530324 ---------- NL: format string "({} goals, ${})" with variables `goals` and `penalties` ref: """({} goals, ${})""".format(self.goals, self.penalties) Output: " " " { } { } " " ". format ( {'foo':'bar'} ) bleu_metric: 34.620457601133026 ---------- NL: format string "({0.goals} goals, ${0.penalties})" ref: """({0.goals} goals, ${0.penalties})""".format(self) Output: df. set _ index ('name') bleu_metric: 3.9504404329436813 ---------- NL: convert list of lists `L` to list of integers ref: [int(''.join(str(d) for d in x)) for x in L] Output: [ int ( x ) for x in l ] bleu_metric: 18.761335250609946 ---------- NL: combine elements of each list in list `L` into digits of a single integer ref: [''.join(str(d) for d in x) for x in L] Output: map ( int, l ) bleu_metric: 2.5915324112223583 ---------- NL: convert a list of lists `L` to list of integers ref: L = [int(''.join([str(y) for y in x])) for x in L] Output: [ int ( x ) for x in l ] bleu_metric: 11.473780780974305 ---------- NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` ref: myfile.write('\n'.join(lines)) Output: myfile. write ('\ n '. join ( map ( str, mylist ) ) ) bleu_metric: 60.5478327168454 ---------- NL: removing an element from a list based on a predicate 'X' or 'N' ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] Output: [ x for x in [ x if x! ='b'] if x! ='c'] bleu_metric: 20.792998340455547 ---------- NL: Remove duplicate words from a string `text` using regex ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) Output: re. sub ('(. * ) \ \ 1 + ','\ \ 1 ', text ) bleu_metric: 42.691904454956294 ---------- NL: count non zero values in each column in pandas data frame ref: df.astype(bool).sum(axis=1) Output: df. groupby ( level = 0 ). sum ( ) bleu_metric: 33.17838167503153 ---------- NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` ref: print(re.search('>.*<', line).group(0)) Output: print ( doc. group ( 1 ). group ( 1 ) ) bleu_metric: 22.08517793512327 ---------- NL: erase all the contents of a file `filename` ref: open(filename, 'w').close() Output: open ('filename ','w'). close ( ) bleu_metric: 70.98108717509044 ---------- NL: convert a string into datetime using the format '%Y-%m-%d %H:%M:%S.%f' ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') Output: datetime. datetime. strptime ('2010 - 11 - 13 10 : 33 : 54. 227806 ','% y bleu_metric: 23.69248624414322 ---------- NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` ref: [index for index, item in enumerate(thelist) if item[0] == '332'] Output: [ x [ 0 ] for x in l if x [ 0 ] = ='score'] bleu_metric: 32.08521724608534 ---------- NL: lower a string `text` and remove non-alphanumeric characters aside from space ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() Output: re. sub ('[ ^ \ \ w. ] + ','', text ). strip ( ) bleu_metric: 54.08690470924506 ---------- NL: remove all non-alphanumeric characters except space from a string `text` and lower it ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() Output: re. sub ('[ ^ 0 - 9a - za - z ] + ','', s ). strip ( ) bleu_metric: 38.80387619814469 ---------- NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='H\u2082O') Output: plt. plot ( x, y, label ='h \ u2082o') bleu_metric: 72.76817202342092 ---------- NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='$H_2O$') Output: plt. plot ( x, y, label ='h \ u2082o') bleu_metric: 71.60350546947922 ---------- NL: loop over a list `mylist` if sublists length equals 3 ref: [x for x in mylist if len(x) == 3] Output: mylist [ : : 2 ] = = mylist [ 0 ] bleu_metric: 12.640504935009755 ---------- NL: initialize a list `lst` of 100 objects Object() ref: lst = [Object() for _ in range(100)] Output: lst = [ [ ] for i in range ( 3 ) ] bleu_metric: 28.992166587078206 ---------- NL: create list `lst` containing 100 instances of object `Object` ref: lst = [Object() for i in range(100)] Output: list ( chain. from _ iterable ( l ) ) bleu_metric: 10.649638629044373 ---------- NL: get the content of child tag with`href` attribute whose parent has css `someclass` ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') Output: soup. find _ elements _ by _ xpath ( " / / * [ @ type ='submit'] " ). get _ attribute ( bleu_metric: 10.389889377420038 ---------- NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'Date_Time' in both dataframes ref: df1.merge(df2, on='Date_Time') Output: pd. merge ( df1, df2, on = ['date _ time ','date _ time'] ) bleu_metric: 21.22363344155404 ---------- NL: use `%s` operator to print variable values `str1` inside a string ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') Output: print ( str ( a ) +'' + str ( b ) ) bleu_metric: 6.053698751871955 ---------- NL: Split a string by a delimiter in python ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] Output: s. split (';') bleu_metric: 2.904501569386027 ---------- NL: check if directory `directory ` exists and create it if necessary ref: if (not os.path.exists(directory)): os.makedirs(directory) Output: os. makedirs ( path ) bleu_metric: 9.597952472216766 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os. makedirs ( path ) bleu_metric: 100.0 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os. makedirs ( path ) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os. makedirs ( path ) bleu_metric: 100.0 ---------- NL: Replace a separate word 'H3' by 'H1' in a string 'text' ref: re.sub('\\bH3\\b', 'H1', text) Output: re. sub ('\ \ w + ','\ \ 1 ', text ) bleu_metric: 56.31071891586576 ---------- NL: substitute ASCII letters in string 'aas30dsa20' with empty string '' ref: re.sub('\\D', '', 'aas30dsa20') Output: re. sub ('[ a - z ] ','','abcd') bleu_metric: 51.003234295212685 ---------- NL: get digits only from a string `aas30dsa20` using lambda function ref: """""".join([x for x in 'aas30dsa20' if x.isdigit()]) Output: " " " " " ". join ( map ( lambda x : x * 7 ) ) bleu_metric: 39.54603942707659 ---------- NL: access a tag called "name" in beautifulsoup `soup` ref: print(soup.find('name').string) Output: soup. findall ('div ', {'name':'description'} ) bleu_metric: 17.457404828304572 ---------- NL: get a dictionary `records` of key-value pairs in PyMongo cursor `cursor` ref: records = dict((record['_id'], record) for record in cursor) Output: dict ( ( x [ 0 ], x [ 1 ] ) for x in cursor. items ( ) ) bleu_metric: 16.34453553468014 ---------- NL: Create new matrix object by concatenating data from matrix A and matrix B ref: np.concatenate((A, B)) Output: numpy. concatenate ( [ a, b ], axis = 0 ) bleu_metric: 18.92240568795935 ---------- NL: concat two matrices `A` and `B` in numpy ref: np.vstack((A, B)) Output: np. sqrt ( ( a. shape [ 0 ], b ) ) bleu_metric: 19.923405658137924 ---------- NL: Get the characters count in a file `filepath` ref: os.stat(filepath).st_size Output: print ( os. path. join ( get _ list ) ) bleu_metric: 13.834368456410944 ---------- NL: count the occurrences of item "a" in list `l` ref: l.count('a') Output: [ x. count ('a') for x in l ] bleu_metric: 43.78826865860793 ---------- NL: count the occurrences of items in list `l` ref: Counter(l) Output: [ sum ( x ) for x in l ] bleu_metric: 13.065113298388567 ---------- NL: count the occurrences of items in list `l` ref: [[x, l.count(x)] for x in set(l)] Output: [ sum ( x ) for x in l ] bleu_metric: 16.443715448412245 ---------- NL: count the occurrences of items in list `l` ref: dict(((x, l.count(x)) for x in set(l))) Output: [ sum ( x ) for x in l ] bleu_metric: 15.927159921457768 ---------- NL: count the occurrences of item "b" in list `l` ref: l.count('b') Output: [ ( x, l. count ( x ) ) for x in l ] bleu_metric: 20.61477352156375 ---------- NL: copy file `srcfile` to directory `dstdir` ref: shutil.copy(srcfile, dstdir) Output: os. chdir ('copyfile. txt ', dst ) bleu_metric: 14.949751774990682 ---------- NL: find the key associated with the largest value in dictionary `x` whilst key is non-zero value ref: max(k for k, v in x.items() if v != 0) Output: max ( x, key = lambda x : ( x ['x'], x ['x'] ) ) bleu_metric: 9.186672262412122 ---------- NL: get the largest key whose not associated with value of 0 in dictionary `x` ref: (k for k, v in x.items() if v != 0) Output: ( k for k, v in x. items ( ) if v! = 0 ) bleu_metric: 100.0 ---------- NL: get the largest key in a dictionary `x` with non-zero value ref: max(k for k, v in x.items() if v != 0) Output: max ( x, key = lambda x : ( x [ 1 ], x [ 0 ] ) ) bleu_metric: 11.162938527547608 ---------- NL: Put the curser at beginning of the file ref: file.seek(0) Output: os. system ('with open ( filename ). read ( ) ','rb') bleu_metric: 8.687475782716618 ---------- NL: combine values from column 'b' and column 'a' of dataframe `df` into column 'c' of datafram `df` ref: df['c'] = np.where(df['a'].isnull, df['b'], df['a']) Output: df ['c'] = df ['a']. apply ( lambda x : x ['b']. fillna bleu_metric: 43.50703089837015 ---------- NL: remove key 'ele' from dictionary `d` ref: del d['ele'] Output: d = { k : v for k, v in list ( d. items ( ) ) if v! ='none'} bleu_metric: 5.442133807846856 ---------- NL: Update datetime field in `MyModel` to be the existing `timestamp` plus 100 years ref: MyModel.objects.update(timestamp=F('timestamp') + timedelta(days=36524.25)) Output: mymodel. objects. filter ( date _ _ length ='% y - % m - % d') bleu_metric: 12.351156662075915 ---------- NL: merge list `['it']` and list `['was']` and list `['annoying']` into one list ref: ['it'] + ['was'] + ['annoying'] Output: [ dict ( zip ( * [ ['one ','two'], ['one ','two'] ] ) ) bleu_metric: 10.105385745342533 ---------- NL: increment a value with leading zeroes in a number `x` ref: str(int(x) + 1).zfill(len(x)) Output: print ('\ xe, { 0 : x. 0 } '. format ( x, - 1 ) ) bleu_metric: 13.471766853689124 ---------- NL: check if a pandas dataframe `df`'s index is sorted ref: all(df.index[:-1] <= df.index[1:]) Output: df. index. values. tolist ( ) bleu_metric: 9.980212030371455 ---------- NL: Convert tuple `t` to list ref: list(t) Output: list ( t ) bleu_metric: 100.0 ---------- NL: Convert list `t` to tuple ref: tuple(l) Output: tuple ( l ) bleu_metric: 100.0 ---------- NL: Convert tuple `level1` to list ref: level1 = map(list, level1) Output: [ ( x + 1 ) for x in a ] bleu_metric: 11.35935489027116 ---------- NL: send the output of pprint object `dataobject` to file `logFile` ref: pprint.pprint(dataobject, logFile) Output: sys. stdout. flush ( ) bleu_metric: 18.67587389639562 ---------- NL: get index of rows in column 'BoolCol' ref: df.loc[df['BoolCol']] Output: df [ ['bool'] ]. index ('matrix2'). index bleu_metric: 18.710158230410627 ---------- NL: Create a list containing the indexes of rows where the value of column 'BoolCol' in dataframe `df` are equal to True ref: df.iloc[np.flatnonzero(df['BoolCol'])] Output: df [ df. index. map ( lambda x : x [ 1 ]. isin ( x ) ) ] bleu_metric: 14.72821272412462 ---------- NL: get list of indexes of rows where column 'BoolCol' values match True ref: df[df['BoolCol'] == True].index.tolist() Output: [ df ['ratio'] for d in l ] bleu_metric: 19.62812256638541 ---------- NL: get index of rows in dataframe `df` which column 'BoolCol' matches value True ref: df[df['BoolCol']].index.tolist() Output: df ['column _ name'] = = df. index. str. get ('columnid') bleu_metric: 18.088201962129936 ---------- NL: change working directory to the directory `owd` ref: os.chdir(owd) Output: os. chdir ('c : \ \ users \ \ users \ \ desktop \ \ desktop \ \ python') bleu_metric: 15.553986407412198 ---------- NL: insert data from a string `testfield` to sqlite db `c` ref: c.execute("INSERT INTO test VALUES (?, 'bar')", (testfield,)) Output: cursor. execute ('insert into table values ( % s, % s ) ', ( ast, ) ) bleu_metric: 34.29547961820059 ---------- NL: decode string "\\x89\\n" into a normal string ref: """\\x89\\n""".decode('string_escape') Output: " " " \ \ xc3 \ \ x96 \ xc3 \ x96 \ x93 \ x93 \ bleu_metric: 29.888108815158 ---------- NL: convert a raw string `raw_string` into a normal string ref: raw_string.decode('string_escape') Output: raw _ string. decode ('unicode _ escape') bleu_metric: 35.556702356686955 ---------- NL: convert a raw string `raw_byte_string` into a normal string ref: raw_byte_string.decode('unicode_escape') Output: raw _ byte _ string. decode ('unicode _ escape') bleu_metric: 30.620490882364876 ---------- NL: split a string `s` with into all strings of repeated characters ref: [m.group(0) for m in re.finditer('(\\d)\\1*', s)] Output: re. findall ('\ \ d + (? : \ \ s + ) ','a \ n b') bleu_metric: 12.792208459604188 ---------- NL: scatter a plot with x, y position of `np.random.randn(100)` and face color equal to none ref: plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none') Output: plt. show ( ) bleu_metric: 0.8278521686370256 ---------- NL: do a scatter plot with empty circles ref: plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none') Output: plt. show ( ) bleu_metric: 0.42503347540650505 ---------- NL: remove a div with a id `main-content` using beautifulsoup ref: soup.find('div', id='main-content').decompose() Output: soup. find _ all ('div ', id ='all') bleu_metric: 46.28692516962493 ---------- NL: filter rows containing key word `ball` in column `ids` ref: df[df['ids'].str.contains('ball')] Output: id. idxmax ( word ). drop ( false ) bleu_metric: 7.630799524006417 ---------- NL: convert index at level 0 into a column in dataframe `df` ref: df.reset_index(level=0, inplace=True) Output: df. set _ index ('level1'). unstack ( level = 0 ) bleu_metric: 24.29335519279861 ---------- NL: Add indexes in a data frame `df` to a column `index1` ref: df['index1'] = df.index Output: df. loc [ index ] = df. index. get _ level _ values ( 0 ) bleu_metric: 24.04315522172745 ---------- NL: convert pandas index in a dataframe to columns ref: df.reset_index(level=['tick', 'obs']) Output: df. reset _ index ( drop = true ) bleu_metric: 11.884004683899853 ---------- NL: Get reverse of list items from list 'b' using extended slicing ref: [x[::-1] for x in b] Output: [ [ i [ 0 ] for i in range ( len ( x ) ) ] for i in range ( 0, len ( list ( a ) bleu_metric: 7.434360163166209 ---------- NL: join each element in array `a` with element at the same index in array `b` as a tuple ref: np.array([zip(x, y) for x, y in zip(a, b)]) Output: b = np. where ( a = = 0, axis = 0 ) bleu_metric: 9.027924728401977 ---------- NL: zip two 2-d arrays `a` and `b` ref: np.array(zip(a.ravel(), b.ravel()), dtype='i4,i4').reshape(a.shape) Output: np. array ( zip ( a. ravel ( ), b. ravel ( ) ), dtype ='i4, i4 bleu_metric: 67.03200460356393 ---------- NL: convert list `list_of_ints` into a comma separated string ref: """,""".join([str(i) for i in list_of_ints]) Output: list ( map ( int, list _ of _ lists. split ( ',') ) ) bleu_metric: 13.283261562346366 ---------- NL: Send a post request with raw data `DATA` and basic authentication with `username` and `password` ref: requests.post(url, data=DATA, headers=HEADERS_DICT, auth=(username, password)) Output: r = requests. post ( url, data = {'username':'username'} ) bleu_metric: 37.18698903847989 ---------- NL: Find last occurrence of character '}' in string "abcd}def}" ref: 'abcd}def}'.rfind('}') Output: re. findall ('(? : ^ [ a - z ] * ) ','abcd0') bleu_metric: 12.962472880491877 ---------- NL: Iterate ove list `[1, 2, 3]` using list comprehension ref: print([item for item in [1, 2, 3]]) Output: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] bleu_metric: 27.073362211548464 ---------- NL: extract all the values with keys 'x' and 'y' from a list of dictionaries `d` to list of tuples ref: [(x['x'], x['y']) for x in d] Output: [ d ['x'] for d in x if'y'in d ] bleu_metric: 37.60857585579953 ---------- NL: get the filename without the extension from file 'hemanth.txt' ref: print(os.path.splitext(os.path.basename('hemanth.txt'))[0]) Output: print ( glob. glob ('liname. txt') [ 0 ] ) bleu_metric: 31.18776276497533 ---------- NL: create a dictionary by adding each two adjacent elements in tuple `x` as key/value pair to it ref: dict(x[i:i + 2] for i in range(0, len(x), 2)) Output: dict ( ( k, v ) for k, v in zip ( a, b ) ) bleu_metric: 12.152094584183624 ---------- NL: create a list containing flattened list `[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]` ref: values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], []) Output: [ ['a ','b ','c ','d'], ['a ','b ','c bleu_metric: 17.7080331882364 ---------- NL: select rows in a dataframe `df` column 'closing_price' between two values 99 and 101 ref: df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)] Output: df. loc [ ~ df ['column _ name']. isin ( some _ values ('one ','two bleu_metric: 10.948000684047626 ---------- NL: replace all occurences of newlines `\n` with `
` in dataframe `df` ref: df.replace({'\n': '
'}, regex=True) Output: df. replace ('\ n ','\ n') bleu_metric: 27.770641036119947 ---------- NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` ref: df.replace({'\n': '
'}, regex=True) Output: df. replace ( {'\ n':'< br >'}, regex = true ) bleu_metric: 89.21616972156079 ---------- NL: create a list containing each two adjacent letters in string `word` as its elements ref: [(x + y) for x, y in zip(word, word[1:])] Output: [ ('a ','b'), ('b ','c'), ('c ','d') bleu_metric: 8.840994001530046 ---------- NL: Get a list of pairs from a string `word` using lambda function ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) Output: [ str ( x ) for x in words ] bleu_metric: 3.8733243551839442 ---------- NL: extract a url from a string `myString` ref: print(re.findall('(https?://[^\\s]+)', myString)) Output: print ( re. search ('( https? : / / [ ^ \ \ s ] + ) ', mystring ) ) bleu_metric: 77.28607577370117 ---------- NL: extract a url from a string `myString` ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) Output: print ( re. search ('( https? : / / [ ^ \ \ s ] + ) ', mystring ) ) bleu_metric: 51.916231800616785 ---------- NL: remove all special characters, punctuation and spaces from a string `mystring` using regex ref: re.sub('[^A-Za-z0-9]+', '', mystring) Output: re. sub ('\ \ w + (? = [ ^. ] * ) ','', mystring ) bleu_metric: 45.91497693322866 ---------- NL: create a DatetimeIndex containing 13 periods of the second friday of each month starting from date '2016-01-01' ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) Output: df. groupby ('date'). apply ( lambda x : x. year ) bleu_metric: 8.453429635655233 ---------- NL: Create multidimensional array `matrix` with 3 rows and 2 columns in python ref: matrix = [[a, b], [c, d], [e, f]] Output: matrix = np. concatenate ( ( [ a, b ], [ [ 0, 3, 6 ], [ 7, 8, 9 bleu_metric: 32.24199399071415 ---------- NL: replace spaces with underscore ref: mystring.replace(' ', '_') Output: re. sub ('\ \ b (? = \ \ w + ) ','','this is a string ','replace bleu_metric: 14.117915208981241 ---------- NL: get an absolute file path of file 'mydir/myfile.txt' ref: os.path.abspath('mydir/myfile.txt') Output: os. path. join ( os. path. join ( root, myfile ) ) bleu_metric: 23.83041256525615 ---------- NL: split string `my_string` on white spaces ref: """ """.join(my_string.split()) Output: " " " 42, " " ". split ( ) bleu_metric: 47.72263780018156 ---------- NL: get filename without extension from file `filename` ref: os.path.splitext(filename)[0] Output: os. path. splitext ( filename ) [ 0 ] bleu_metric: 100.0 ---------- NL: get a list containing the sum of each element `i` in list `l` plus the previous elements ref: [sum(l[:i]) for i, _ in enumerate(l)] Output: sum ( x for x in l ) bleu_metric: 6.924459302580939 ---------- NL: split a string `Docs/src/Scripts/temp` by `/` keeping `/` in the result ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') Output: re. split ('\ \ s + ', string ) bleu_metric: 7.926805970774756 ---------- NL: shuffle columns of an numpy array 'r' ref: np.random.shuffle(np.transpose(r)) Output: np. random. shuffle ( np. transpose ( r ) ) bleu_metric: 100.0 ---------- NL: copy all values in a column 'B' to a new column 'D' in a pandas data frame 'df' ref: df['D'] = df['B'] Output: df ['b'] = df ['c']. apply ( lambda x : x. tolist ( ), axis = bleu_metric: 26.492685902784498 ---------- NL: find a value within nested json 'data' where the key inside another key 'B' is unknown. ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] Output: print ( json. dumps ( ['b ','c'] ) ) bleu_metric: 5.979778160388024 ---------- NL: check characters of string `string` are true predication of function `predicate` ref: all(predicate(x) for x in string) Output: print ( any ( test _ string ) for substring in string _ string ) bleu_metric: 19.923405658137924 ---------- NL: determine number of files on a drive with python ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree Output: os. stat ('/ myfile. txt'). os. listdir ('/') bleu_metric: 37.211067993181466 ---------- NL: how to get a single result from a SQLite query in python? ref: cursor.fetchone()[0] Output: cursor. fetchone ( ) [ 0 ] bleu_metric: 100.0 ---------- NL: convert string `user_input` into a list of integers `user_list` ref: user_list = [int(number) for number in user_input.split(',')] Output: [ int ( x ) for x in input ( ). split ( ',') ] bleu_metric: 51.23350305765596 ---------- NL: Get a list of integers by splitting a string `user` with comma ref: [int(s) for s in user.split(',')] Output: map ( int,'re. split ('( \ \ d + ) ','hello world') ) bleu_metric: 20.80315522738391 ---------- NL: Sorting a Python list by two criteria ref: sorted(list, key=lambda x: (x[0], -x[1])) Output: sorted ( list ( range ( 0, len ( s ) ), key = lambda x : ( x [ 0 ], x [ 0 ] ) bleu_metric: 48.440807796000044 ---------- NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order ref: ut.sort(key=cmpfun, reverse=True) Output: sorted ( l, key = lambda x : x. modified, reverse = true ) bleu_metric: 18.107197870881738 ---------- NL: reverse list `ut` based on the `count` attribute of each object ref: ut.sort(key=lambda x: x.count, reverse=True) Output: ['city ','same2 ','type'] bleu_metric: 7.453337029678027 ---------- NL: sort a list of objects `ut` in reverse order by their `count` property ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sorted ( l, key = lambda x : x. count ('c') ) bleu_metric: 45.34010611888324 ---------- NL: click a href button 'Send' with selenium ref: driver.find_element_by_partial_link_text('Send').click() Output: driver. find _ element _ by _ css _ selector ('send inmail'). click ( ) bleu_metric: 35.13874939965221 ---------- NL: click a href button having text `Send InMail` with selenium ref: driver.findElement(By.linkText('Send InMail')).click() Output: driver. find _ element _ by _ link _ text ('send inmail') bleu_metric: 15.175278640994005 ---------- NL: click a href button with text 'Send InMail' with selenium ref: driver.find_element_by_link_text('Send InMail').click() Output: driver. find _ element _ by _ link _ text ('send') bleu_metric: 15.27678022374703 ---------- NL: cast an int `i` to a string and concat to string 'ME' ref: 'ME' + str(i) Output: str ( i ) bleu_metric: 36.787944117144235 ---------- NL: Sorting data in DataFrame Pandas ref: df.sort_values(['System_num', 'Dis']) Output: df. sort _ values ( ['system _ id ','dis'] ) bleu_metric: 31.671615012203976 ---------- NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) Output: open ('test. txt ','w'). write ('\ n') bleu_metric: 36.176855814312944 ---------- NL: sort a list `l` by length of value in tuple ref: l.sort(key=lambda t: len(t[1]), reverse=True) Output: l. sort ( key = lambda x : ( len ( x ), x ) ) bleu_metric: 37.875123684341865 ---------- NL: split string `s` by words that ends with 'd' ref: re.findall('\\b(\\w+)d\\b', s) Output: re. split ('( \ \ d + ) ', s ) bleu_metric: 29.23155541924467 ---------- NL: return `True` if string `foobarrrr` contains regex `ba[rzd]` ref: bool(re.search('ba[rzd]', 'foobarrrr')) Output: pattern = re. compile ('\ \ b [ a - za - z0 - 9 - ]') bleu_metric: 12.384901282810546 ---------- NL: Removing duplicates in list `t` ref: list(set(t)) Output: list ( set ( t ) ) bleu_metric: 100.0 ---------- NL: Removing duplicates in list `source_list` ref: list(set(source_list)) Output: list ( set ( t ) ) bleu_metric: 59.69491792019645 ---------- NL: Removing duplicates in list `abracadabra` ref: list(OrderedDict.fromkeys('abracadabra')) Output: [ ('a ','b'), ('b ','c'), ('a ','d') bleu_metric: 7.555283720961675 ---------- NL: Convert array `a` into a list ref: numpy.array(a).reshape(-1).tolist() Output: list ( a ) bleu_metric: 5.237603475197421 ---------- NL: Convert the first row of numpy matrix `a` to a list ref: numpy.array(a)[0].tolist() Output: numpy. apply _ along _ axis ( numpy. array ( [ 0, 1, 2 ] ), a ) bleu_metric: 20.97823227594146 ---------- NL: In `soup`, get the content of the sibling of the `td` tag with text content `Address:` ref: print(soup.find(text='Address:').findNext('td').contents[0]) Output: soup. find _ element _ by _ id ('< id >'). text bleu_metric: 12.033574792347215 ---------- NL: convert elements of each tuple in list `l` into a string separated by character `@` ref: """ """.join([('%d@%d' % t) for t in l]) Output: [''. join ( x ) for x in l ] bleu_metric: 17.41466588478686 ---------- NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) Output: " " " " " ". join ( [ x for x in l if x [ 0 ] in l ] ) bleu_metric: 36.45674601639763 ---------- NL: get the html from the current web page of a Selenium driver ref: driver.execute_script('return document.documentElement.outerHTML;') Output: driver. get ('http : / / www. google. com') bleu_metric: 19.209534151258666 ---------- NL: Get all matches with regex pattern `\\d+[xX]` in list of string `teststr` ref: [i for i in teststr if re.search('\\d+[xX]', i)] Output: [ i for i in teststr if re. search ('\ \ d + [ ^ \ \ ] * \ \ ] ', i ) bleu_metric: 69.89029944247109 ---------- NL: select values from column 'A' for which corresponding values in column 'B' will be greater than 50, and in column 'C' - equal 900 in dataframe `df` ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] Output: df [ ( df ['a'] > 1 ) & ( df ['b'] < = 5 ) ] bleu_metric: 39.042104382208294 ---------- NL: Sort dictionary `o` in ascending order based on its keys and items ref: sorted(o.items()) Output: sorted ( list ( dictionary. items ( ) ), key = lambda x : x [ 1 ] ) bleu_metric: 24.42195895839763 ---------- NL: get sorted list of keys of dict `d` ref: sorted(d) Output: sorted ( d, key = d. get ) bleu_metric: 25.21193618434983 ---------- NL: How to sort dictionaries by keys in Python ref: sorted(d.items()) Output: sorted ( d. items ( ) ) bleu_metric: 100.0 ---------- NL: convert string "1" into integer ref: int('1') Output: int ('1') bleu_metric: 100.0 ---------- NL: function to convert strings into integers ref: int() Output: map ( int,'1') bleu_metric: 21.105340631872636 ---------- NL: convert items in `T1` to integers ref: T2 = [map(int, x) for x in T1] Output: int ('\ n '. join ( str ( i ) for i in str ( n ) ) ) bleu_metric: 8.607692533178168 ---------- NL: call a shell script `./test.sh` using subprocess ref: subprocess.call(['./test.sh']) Output: subprocess. call ( ['test. sh ','/ test. sh otherfunc'] ) bleu_metric: 53.66411241731205 ---------- NL: call a shell script `notepad` using subprocess ref: subprocess.call(['notepad']) Output: subprocess. call ( ['notepad'] ) bleu_metric: 100.0 ---------- NL: combine lists `l1` and `l2` by alternating their elements ref: [val for pair in zip(l1, l2) for val in pair] Output: [ ( x + y ) for x in l2 for y in zip ( l1, l2 ) ] bleu_metric: 38.146366463982886 ---------- NL: encode string 'data to be encoded' ref: encoded = base64.b64encode('data to be encoded') Output: data. encode ('utf8') bleu_metric: 19.260282250359822 ---------- NL: encode a string `data to be encoded` to `ascii` encoding ref: encoded = 'data to be encoded'.encode('ascii') Output: data. encode ('ascii') bleu_metric: 41.51380951089673 ---------- NL: parse tab-delimited CSV file 'text.txt' into a list ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) Output: txt = open ('myfile. txt ','r'). read ( ) bleu_metric: 20.692050735338164 ---------- NL: Get attribute `my_str` of object `my_object` ref: getattr(my_object, my_str) Output: getattr ( my _ obj,'id') bleu_metric: 19.72940627795883 ---------- NL: group a list of dicts `LD` into one dict by key ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) Output: values = [ dict ( k, v ) for k, v in list ( d. items ( ) ) ] bleu_metric: 17.057966110550765 ---------- NL: How do I sum the first value in each tuple in a list of tuples in Python? ref: sum([pair[0] for pair in list_of_pairs]) Output: sum ( [ item [ 0 ] for item in tuple ] ) bleu_metric: 41.37441202051881 ---------- NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary ref: d = ast.literal_eval("{'code1':1,'code2':1}") Output: print ('\ u0420 \ u043e \ u0441 \ u0438 \ u0438 \ u bleu_metric: 6.351019285070924 ---------- NL: find all words in a string `mystring` that start with the `$` sign ref: [word for word in mystring.split() if word.startswith('$')] Output: re. findall ('\ \ b [ ^ \ \ w \ \ ] ', mystring ) bleu_metric: 9.850775379403402 ---------- NL: remove any url within string `text` ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) Output: re. sub ('\ \. \ \ blog _ addr \ \ r \ \ n ','', text ) bleu_metric: 32.17551945847391 ---------- NL: replace all elements in array `A` that are not present in array `[1, 3, 4]` with zeros ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) Output: np. where ( np. in1d ( a, b ) ) [ 0 ] bleu_metric: 22.828499613963487 ---------- NL: calculate mean across dimension in a 2d array `a` ref: np.mean(a, axis=1) Output: np. mean ( a, axis = 1 ) bleu_metric: 100.0 ---------- NL: running r script '/pathto/MyrScript.r' from python ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) Output: sys. path. append ('/ path / to / my / my / mypath') bleu_metric: 7.04483155666154 ---------- NL: run r script '/usr/bin/Rscript --vanilla /pathto/MyrScript.r' ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) Output: os. path. join ( root, '. / main _ folder') bleu_metric: 6.726052538373431 ---------- NL: add a header to a csv file ref: writer.writeheader() Output: writer. writeheader ( ) bleu_metric: 100.0 ---------- NL: replacing nan in the dataframe `df` with row average ref: df.fillna(df.mean(axis=1), axis=1) Output: df. fillna ( df. mean ( axis = 1 ), axis = 1 ) bleu_metric: 100.0 ---------- NL: Convert unix timestamp '1347517370' to formatted string '%Y-%m-%d %H:%M:%S' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) Output: datetime. strptime ('21 / 11 / 06 16 : 30 ','% d / % m / % y') bleu_metric: 10.247261229308657 ---------- NL: Call a base class's class method `do` from derived class `Derived` ref: super(Derived, cls).do(a) Output: super ( childclass, self ). _ _ init _ _ ( * args, * * kwargs ) bleu_metric: 11.96655750514248 ---------- NL: selecting rows in Numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] Output: a [ np. arange ( 3 ), ( 0, 1, 0 ) ] bleu_metric: 13.581342454223687 ---------- NL: separate words delimited by one or more spaces into a list ref: re.split(' +', 'hello world sample text') Output: ['one ','two ','one ','two'] bleu_metric: 13.78592993183041 ---------- NL: length of longest element in list `words` ref: len(max(words, key=len)) Output: len ( max ( words, key = len ) ) bleu_metric: 100.0 ---------- NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` ref: result[0]['from_user'] Output: ['key1 ','key2 ','value'] bleu_metric: 18.20705281109213 ---------- NL: Retrieve each line from a file 'File.txt' as a list ref: [line.split() for line in open('File.txt')] Output: print ( line. rstrip ('\ n') ) bleu_metric: 11.789701057610694 ---------- NL: swap keys with values in a dictionary `a` ref: res = dict((v, k) for k, v in a.items()) Output: a [ key ] = dict ( ( v ) for k, v in a. items ( ) ) bleu_metric: 73.78351342269067 ---------- NL: Open a file `path/to/FILE_NAME.ext` in write mode ref: new_file = open('path/to/FILE_NAME.ext', 'w') Output: file _ name = os. path. join ( os. path. join ( dir, '.') ) bleu_metric: 10.713701843513144 ---------- NL: How to count distinct values in a column of a pandas group by object? ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() Output: df. groupby ( ['col1 ','col2'] ) ['col3']. apply ( lambda x : bleu_metric: 72.38800804180103 ---------- NL: Check if any key in the dictionary `dict1` starts with the string `EMP$$` ref: any(key.startswith('EMP$$') for key in dict1) Output: any ( isinstance ( x, int ( dict. values ( ) ) ) for x in dict _ string in input. lower ( bleu_metric: 8.839374326825924 ---------- NL: create list of values from dictionary `dict1` that have a key that starts with 'EMP$$' ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] Output: [ dict ( zip ( k, v ), v ) for k, v in d. items ( ) if v. startswith ( ' bleu_metric: 24.14138031982595 ---------- NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) Output: pd. concat ( [ df1, df2 ], axis = 1 ) bleu_metric: 8.12657367724027 ---------- NL: print elements of list `list` seperated by tabs `\t` ref: print('\t'.join(map(str, list))) Output: print ( [ i. partition ('\ t') for i in list _ of _ lists ] ) bleu_metric: 25.58058053378779 ---------- NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) Output: print ('\ xc5 \ xa4 \ xd0 \ x80 \ xd0 \ xb0 \ xd0 bleu_metric: 29.472590166651962 ---------- NL: Encode a latin character in string `Sopet\xc3\xb3n` properly ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') Output: " " " \ xc3 \ xa3 \ x9f " " ". encode ('utf - 8') bleu_metric: 46.255815148030386 ---------- NL: resized image `image` to width, height of `(x, y)` with filter of `ANTIALIAS` ref: image = image.resize((x, y), Image.ANTIALIAS) Output: img = np. array ( [ 0, 1, 0, 1, 2, - 1, 0, 1, 2 ] ) bleu_metric: 7.278748444764005 ---------- NL: regex, find "n"s only in the middle of string `s` ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) Output: re. findall ('(? < = [ ^ \ \ s ] ) (? = [ ^ \ \ s ] + ) ', bleu_metric: 44.74679573461579 ---------- NL: display the float `1/3*100` as a percentage ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) Output: print ( ('0. 1 ', 1 ) ) bleu_metric: 11.406542828786534 ---------- NL: sort a list of dictionary `mylist` by the key `title` ref: mylist.sort(key=lambda x: x['title']) Output: mylist. sort ( key = lambda x : x ['title'] ) bleu_metric: 100.0 ---------- NL: sort a list `l` of dicts by dict value 'title' ref: l.sort(key=lambda x: x['title']) Output: l. sort ( key = lambda x : x ['title'] ) bleu_metric: 100.0 ---------- NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) Output: sorted ( l, key = lambda x : x ['name'] ) bleu_metric: 17.362697834284052 ---------- NL: find 10 largest differences between each respective elements of list `l1` and list `l2` ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) Output: [ ( x, y ) for x, y in zip ( l1, l2 ) ] bleu_metric: 5.935298261592072 ---------- NL: BeautifulSoup find all 'span' elements in HTML string `soup` with class of 'starGryB sp' ref: soup.find_all('span', {'class': 'starGryB sp'}) Output: soup. findall ( text ='div ', style ='width = 300px ;') bleu_metric: 11.970571135993998 ---------- NL: write records in dataframe `df` to table 'test' in schema 'a_schema' ref: df.to_sql('test', engine, schema='a_schema') Output: df. plot ( row ='test ','test _ txt') bleu_metric: 32.62478546610937 ---------- NL: Extract brackets from string `s` ref: brackets = re.sub('[^(){}[\\]]', '', s) Output: re. sub ('\ \ [ ( \ \ w + ) \ \ s + \ \ ] ','', s ) bleu_metric: 47.1113978575501 ---------- NL: remove duplicate elements from list 'L' ref: list(dict((x[0], x) for x in L).values()) Output: [ k for k, g in l if'l'in k ] bleu_metric: 6.942420857748657 ---------- NL: read a file `file` without newlines ref: [line.rstrip('\n') for line in file] Output: file. write ('\ n') bleu_metric: 30.98188793972842 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 9.243620302376062 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 9.243620302376062 ---------- NL: get the position of item 1 in `testlist` ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: pass Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 7.565780264883953 ---------- NL: get the position of item 1 in `testlist` ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): pass Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 6.0168435849456285 ---------- NL: get the position of item 1 in `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) Output: print ( [ item for item in mylist if item. isdigit ( ) ] ) bleu_metric: 8.28910840961223 ---------- NL: get the position of item `element` in list `testlist` ref: print(testlist.index(element)) Output: print ( testlist. index ( element ) ) bleu_metric: 100.0 ---------- NL: get the position of item `element` in list `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) Output: print ( testlist. index ( element ) ) bleu_metric: 7.150780327539586 ---------- NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` ref: max(lis, key=lambda item: item[1])[0] Output: max ( x [ 0 ] for x in rows ) bleu_metric: 14.988854080083206 ---------- NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` ref: max(lis, key=itemgetter(1))[0] Output: max ( x, key = lambda x : x [ 1 ] ) [ 0 ] bleu_metric: 35.27295712700594 ---------- NL: Make a delay of 1 second ref: time.sleep(1) Output: time. sleep ( 0. 1 ) bleu_metric: 52.53819788848316 ---------- NL: convert list of tuples `L` to a string ref: """, """.join('(' + ', '.join(i) + ')' for i in L) Output: " " " " " ". join ( l ) bleu_metric: 14.164334116374599 ---------- NL: Django set default value of field `b` equal to '0000000' ref: b = models.CharField(max_length=7, default='0000000', editable=False) Output: obj. save ('new _ name ','default _ number') bleu_metric: 10.318679011582763 ---------- NL: Sort lis `list5` in ascending order based on the degrees value of its elements ref: sorted(list5, lambda x: (degree(x), x)) Output: sorted ( lst, key = lambda x : x [ 1 ] ) bleu_metric: 19.143591202259493 ---------- NL: How do I perform secondary sorting in python? ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) Output: sorted ( list5, key = lambda x : ( sum ( x [ 1 ] ), x ) ) bleu_metric: 41.26152034907945 ---------- NL: convert a list into a generator object ref: (n for n in [1, 2, 3, 5]) Output: list ( itertools. combinations ( * ( l ) ) ) bleu_metric: 9.930751066504135 ---------- NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] Output: [ x for x in set ( split _ list [ i ] for i in range ( len ( x ) ) if i not in index ] bleu_metric: 18.860270984795903 ---------- NL: Open a file `yourfile.txt` in write mode ref: f = open('yourfile.txt', 'w') Output: os. system ('your string. txt') bleu_metric: 23.571525044211057 ---------- NL: get attribute 'attr' from object `obj` ref: getattr(obj, 'attr') Output: getattr ( obj,'attr') bleu_metric: 100.0 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple ref: from functools import reduce reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) Output: tuple ( ['a ','b ','c ','d'] ) bleu_metric: 6.101077050680879 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) Output: map ( lambda s : ( d ['a'], d ['b'], d ['c'] ), d [ bleu_metric: 11.200083731986627 ---------- NL: Python Pandas: How to replace a characters in a column of a dataframe? ref: df['range'].replace(',', '-', inplace=True) Output: df ['brandname'] = df ['brandname']. replace ('- ', ','in') bleu_metric: 45.97006398721532 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip ( * [ ('a ','b ','c ','d'] ) ) bleu_metric: 36.501102157810664 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip ( * [ ('a ','b ','c ','d'] ) ) bleu_metric: 36.501102157810664 ---------- NL: unzip list `original` ref: result = ([a for (a, b) in original], [b for (a, b) in original]) Output: zip ( * list ( range ( 10 ) ) ) bleu_metric: 4.606184881898074 ---------- NL: unzip list `original` and return a generator ref: result = ((a for (a, b) in original), (b for (a, b) in original)) Output: [ i for i in range ( 1, len ( l ) ) if i! ='+ 1 ] bleu_metric: 7.62604991346693 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip ( * [ ('a ','b ','c ','d ','e'] ) ) bleu_metric: 34.87995424158042 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with None ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip ( * [ ('a ','b'), ('b ','c ','d'), ( ' bleu_metric: 35.430131828411845 ---------- NL: encode `Decimal('3.9')` to a JSON string ref: json.dumps(Decimal('3.9')) Output: json. dumps ( ['1 ','2 ','3'] ) bleu_metric: 23.58844810653421 ---------- NL: Add key "mynewkey" to dictionary `d` with value "mynewvalue" ref: d['mynewkey'] = 'mynewvalue' Output: df. setdefault ('name ', [ ] ). append ( value ) bleu_metric: 9.50724412002623 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update({'a': 1, }) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 37.863495563972855 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update(dict(a=1)) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 8.24055698798382 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update(a=1) Output: a ['a']. append ( {'a': 1,'b': 2 } ) bleu_metric: 8.24055698798382 ---------- NL: find maximal value in matrix `matrix` ref: max([max(i) for i in matrix]) Output: max ( matrix, key = lambda x : max ( x ) ) bleu_metric: 16.943571815930884 ---------- NL: Round number `answer` to 2 precision after the decimal point ref: answer = str(round(answer, 2)) Output: round ( 1. 0005, 3 ) bleu_metric: 17.51643270174889 ---------- NL: extract ip address from an html string ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) Output: print ( re. findall ('[ ^ a - za - z0 - 9 - ] + (? : \ \. [ a - bleu_metric: 40.70867848415918 ---------- NL: filter dataframe `df` by values in column `A` that appear more than once ref: df.groupby('A').filter(lambda x: len(x) > 1) Output: df. filter ( df. filter ( lambda x : x. any ( axis = 1 ) ) ) bleu_metric: 34.640201116587235 ---------- NL: append each line in file `myfile` into a list ref: [x for x in myfile.splitlines() if x != ''] Output: myfile. write ('\ n '. join ( [ str ( i ) for i in myfile ] ) ) bleu_metric: 10.435185733665413 ---------- NL: Get a list of integers `lst` from a file `filename.txt` ref: lst = map(int, open('filename.txt').readlines()) Output: [ int ( x ) for x in lst ] bleu_metric: 7.020674481218203 ---------- NL: add color bar with image `mappable` to plot `plt` ref: plt.colorbar(mappable=mappable, cax=ax3) Output: plt. show ( ) bleu_metric: 11.914378950211951 ---------- NL: count most frequent 100 words in column 'text' of dataframe `df` ref: Counter(' '.join(df['text']).split()).most_common(100) Output: df. groupby ('text'). apply ( lambda x : x. sum ( ) ). most _ common ( 1 ) bleu_metric: 21.52643286782715 ---------- NL: Python split a string using regex ref: re.findall('(.+?):(.+?)\\b ?', text) Output: re. split ('(? < = \ \ d ) \ \ d + ', text ) bleu_metric: 29.769819080461428 ---------- NL: generate all 2-element subsets of tuple `(1, 2, 3)` ref: list(itertools.combinations((1, 2, 3), 2)) Output: list ( itertools. product ( [ 1, 2, 3 ], [ 4, 5, 6 ] ) ) bleu_metric: 34.29547961820059 ---------- NL: get a value of datetime.today() in the UTC time zone ref: datetime.now(pytz.utc) Output: pytz. utcnow ( ) bleu_metric: 23.206041459353084 ---------- NL: Get a new list `list2`by removing empty list from a list of lists `list1` ref: list2 = [x for x in list1 if x != []] Output: list2 = [ x for x in list1 if x [ : ] = = [ ] ] bleu_metric: 69.3395566222006 ---------- NL: Create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` ref: list2 = [x for x in list1 if x] Output: list ( zip ( a, b ) ) bleu_metric: 10.320893749383378 ---------- NL: Django response with JSON `data` ref: return HttpResponse(data, mimetype='application/json') Output: return httpresponse ( json. dumps ( response _ data ), content _ type ='application / json') bleu_metric: 36.17640392425988 ---------- NL: get all text that is not enclosed within square brackets in string `example_str` ref: re.findall('(.*?)\\[.*?\\]', example_str) Output: str1. split ('\ \ ) [ ^ \ \ ] ', str. strip ( ) bleu_metric: 26.91730507369109 ---------- NL: Use a regex to get all text in a string `example_str` that is not surrounded by square brackets ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) Output: re. findall ('\ \ [ (? : \ \ w + ) \ \ ] ', str1 ) bleu_metric: 35.15147667690756 ---------- NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') Output: re. sub ('\ \ ( [ ^ \ \ w ] ) ','', filename ) bleu_metric: 27.664786097762494 ---------- NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') Output: re. sub ('(? < = \ \ w ) ( \ \ w + ) ( \ \ w + ) ','\ \ bleu_metric: 30.955387518700363 ---------- NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') Output: re. findall ('(? < = \ \ w ) ( [ ^ \ \ w \ \ w ] | \ \ w + ) bleu_metric: 29.552612305356018 ---------- NL: formate each string cin list `elements` into pattern '%{0}%' ref: elements = ['%{0}%'.format(element) for element in elements] Output: " " " { 0 :. 2f } " " ". format ('foo ','bar') bleu_metric: 17.414675099971177 ---------- NL: Open a background process 'background-process' with arguments 'arguments' ref: subprocess.Popen(['background-process', 'arguments']) Output: os. system ('some _ command - some _ file') bleu_metric: 12.107470158860224 ---------- NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' ref: [mydict[x] for x in mykeys] Output: [ key for item in my _ list if item. get ('name ','age') ] bleu_metric: 9.119675426861836 ---------- NL: convert list `[('Name', 'Joe'), ('Age', 22)]` into a dictionary ref: dict([('Name', 'Joe'), ('Age', 22)]) Output: dict ( ( ('class ', 2 ), ('class ', 3 ) ) ) bleu_metric: 24.080231808676874 ---------- NL: average each two columns of array `data` ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) Output: np. mean ( np. arange ( len ( df. columns ) ), axis = 1 ) bleu_metric: 18.410768031391818 ---------- NL: double backslash escape all double quotes in string `s` ref: print(s.encode('unicode-escape').replace('"', '\\"')) Output: print ( s. split ('\ \') ) bleu_metric: 21.765088513075124 ---------- NL: split a string into a list of words and whitespace ref: re.split('(\\W+)', s) Output: ['this is a string : ','this is a string ','this is a string ','this is a string ', bleu_metric: 5.648892151960283 ---------- NL: plotting stacked barplots on a panda data frame ref: df.plot(kind='barh', stacked=True) Output: df. plot ( kind ='barh ', stacked = true ) bleu_metric: 83.7117009877792 ---------- NL: reverse the keys and values in a dictionary `myDictionary` ref: {i[1]: i[0] for i in list(myDictionary.items())} Output: dict ( ( k, v ) for k, v in list ( d. items ( ) ) ) bleu_metric: 23.690464140247087 ---------- NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'myList'. ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] Output: ['x ','y ','red ','var ','var'] bleu_metric: 6.180827527485682 ---------- NL: check if object `obj` is a string ref: isinstance(obj, str) Output: isinstance ( obj, str ) bleu_metric: 100.0 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance ( s, str ) bleu_metric: 59.46035575013605 ---------- NL: check if object `o` is a string ref: (type(o) is str) Output: isinstance ( s, str ) bleu_metric: 23.813694985189848 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance ( s, str ) bleu_metric: 59.46035575013605 ---------- NL: check if `obj_to_test` is a string ref: isinstance(obj_to_test, str) Output: print ( obj. isfile ('attr name') ) bleu_metric: 13.508625657351411 ---------- NL: append list `list1` to `list2` ref: list2.extend(list1) Output: [ list1. append ( list1 ) for a in range ( 3 ) ] bleu_metric: 14.879641171245488 ---------- NL: append list `mylog` to `list1` ref: list1.extend(mylog) Output: list3. append ( list1 ) bleu_metric: 29.071536848410968 ---------- NL: append list `a` to `c` ref: c.extend(a) Output: a. append ( a ) bleu_metric: 54.10822690539396 ---------- NL: append items in list `mylog` to `list1` ref: for line in mylog: list1.append(line) Output: list3. append ( list1 ) bleu_metric: 20.917479021833486 ---------- NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` ref: b.append((a[0][0], a[0][2])) Output: a = [ [ ] for i in range ( 0, len ( a ), [ 3, 4 ] ] ) bleu_metric: 11.556647985416685 ---------- NL: Initialize `SECRET_KEY` in flask config with `Your_secret_string ` ref: app.config['SECRET_KEY'] = 'Your_secret_string' Output: app. config ['your _ id'] ='your _ config ' bleu_metric: 43.343660127583234 ---------- NL: unpack a series of tuples in pandas into a DataFrame with column names 'out-1' and 'out-2' ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) Output: pd. dataframe ( df. values. tolist ( ), columns = ['a ','b'], axis = 1 bleu_metric: 35.31494509780309 ---------- NL: find the index of an element 'MSFT' in a list `stocks_list` ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] Output: print ( list ( enumerate ( my _ list ) ) ) bleu_metric: 5.9394573438219 ---------- NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable ref: ax.set_xticklabels(labels, rotation=45) Output: ax. set _ xticklabels ( ax. xaxis. get _ majorticklabels ( ), rotation = 45 ) bleu_metric: 29.88309778054086 ---------- NL: remove symbols from a string `s` ref: re.sub('[^\\w]', ' ', s) Output: re. sub ('[ ^ a - z ] ','', s ) bleu_metric: 74.83293841345244 ---------- NL: Get the current directory of a script ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) Output: os. path. dirname ( os. path. abspath ( _ _ file _ _ ) ) bleu_metric: 48.431464491050775 ---------- NL: Find octal characters matches from a string `str` using regex ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) Output: re. findall ('\ \ [ (? < = \ \ d ) \ \ ) ','- ', str ) bleu_metric: 34.98979283499355 ---------- NL: split string `input` based on occurrences of regex pattern '[ ](?=[A-Z]+\\b)' ref: re.split('[ ](?=[A-Z]+\\b)', input) Output: re. findall ('(? < = [ a - z ] ) (? = [ a - z ] ) (? = [ a bleu_metric: 16.927318970546587 ---------- NL: Split string `input` at every space followed by an upper-case letter ref: re.split('[ ](?=[A-Z])', input) Output: re. split ('\ \ b ', text ) bleu_metric: 23.369549958807866 ---------- NL: send multipart encoded file `files` to url `url` with headers `headers` and metadata `data` ref: r = requests.post(url, files=files, headers=headers, data=data) Output: r = requests. get ( url ) bleu_metric: 13.273006566054132 ---------- NL: write bytes `bytes_` to a file `filename` in python 3 ref: open('filename', 'wb').write(bytes_) Output: f. write ( struct. pack ('> i ', filename ) ) bleu_metric: 21.042990347620457 ---------- NL: get a list from a list `lst` with values mapped into a dictionary `dct` ref: [dct[k] for k in lst] Output: [ list ( lst [ i ] ) for i in lst ] bleu_metric: 28.787878181011266 ---------- NL: find duplicate names in column 'name' of the dataframe `x` ref: x.set_index('name').index.get_duplicates() Output: print ( list ( df. index. get _ duplicates ( ) ) ) bleu_metric: 19.923405658137924 ---------- NL: truncate float 1.923328437452 to 3 decimal places ref: round(1.923328437452, 3) Output: round ( 1. 005, 2 ) bleu_metric: 46.71379777282001 ---------- NL: sort list `li` in descending order based on the date value in second element of each list in list `li` ref: sorted(li, key=lambda x: datetime.strptime(x[1], '%d/%m/%Y'), reverse=True) Output: sorted ( li, key = operator. itemgetter ( 1 ), reverse = true ) bleu_metric: 19.26332463060877 ---------- NL: place the radial ticks in plot `ax` at 135 degrees ref: ax.set_rlabel_position(135) Output: ax. set _ xticklabels ( [ ] ) bleu_metric: 21.834177214239062 ---------- NL: check if path `my_path` is an absolute path ref: os.path.isabs(my_path) Output: os. path. exists ('my _ path') bleu_metric: 31.61487584488943 ---------- NL: get number of keys in dictionary `yourdict` ref: len(list(yourdict.keys())) Output: len ( mydict ) bleu_metric: 11.141275535087015 ---------- NL: count the number of keys in dictionary `yourdictfile` ref: len(set(open(yourdictfile).read().split())) Output: sum ( len ( v ) for v in yourdict. values ( ) ) bleu_metric: 18.767030063813223 ---------- NL: pandas dataframe get first row of each group by 'id' ref: df.groupby('id').first() Output: df. groupby ('id'). first ( ). first ( ) bleu_metric: 75.62722459724604 ---------- NL: split a list in first column into multiple columns keeping other columns as well in pandas data frame ref: pd.concat([df[0].apply(pd.Series), df[1]], axis=1) Output: df [ ['c ','df'] ]. apply ( lambda x : x [ 1 : - 1 ]. split ( bleu_metric: 19.037861963633805 ---------- NL: extract attributes 'src="js/([^"]*\\bjquery\\b[^"]*)"' from string `data` ref: re.findall('src="js/([^"]*\\bjquery\\b[^"]*)"', data) Output: " " " \ \ b " " " bleu_metric: 3.6253194206622026 ---------- NL: Sum integers contained in strings in list `['', '3.4', '', '', '1.0']` ref: sum(int(float(item)) for item in [_f for _f in ['', '3.4', '', '', '1.0'] if _f]) Output: sum ( [ 1, 2, 3, 4, 5 ] ) bleu_metric: 3.255351208100268 ---------- NL: Call a subprocess with arguments `c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat` that may contain spaces ref: subprocess.Popen(['c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat']) Output: subprocess. call ( ['shutdown ','/ r ','/ tmp / test. py'] ) bleu_metric: 16.665685159230854 ---------- NL: reverse a priority queue `q` in python without using classes ref: q.put((-n, n)) Output: t. start ( ) bleu_metric: 12.151662434083677 ---------- NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) Output: df. groupby ('o') ['value']. first ( ) bleu_metric: 3.5795004593328636 ---------- NL: find all matches of regex pattern '([a-fA-F\\d]{32})' in string `data` ref: re.findall('([a-fA-F\\d]{32})', data) Output: re. findall ('(? < = [ a - z ] ) ( [ a - z ] ) ','abcd _ string bleu_metric: 33.336490646495264 ---------- NL: Get the length of list `my_list` ref: len(my_list) Output: len ( my _ list ) bleu_metric: 30.739407647563223 ---------- NL: Getting the length of array `l` ref: len(l) Output: len ( l ) bleu_metric: 100.0 ---------- NL: Getting the length of array `s` ref: len(s) Output: len ( s ) bleu_metric: 100.0 ---------- NL: Getting the length of `my_tuple` ref: len(my_tuple) Output: len ( my _ list ) bleu_metric: 30.739407647563223 ---------- NL: Getting the length of `my_string` ref: len(my_string) Output: len ( my _ string ) bleu_metric: 30.739407647563223 ---------- NL: remove escape character from string "\\a" ref: """\\a""".decode('string_escape') Output: print ('\ n '. join ('[ _ name _ ]') ) bleu_metric: 15.071676257541073 ---------- NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') Output: a = re. sub ('[ a - za - z0 - 9 ] ','', line ) bleu_metric: 6.256610657452671 ---------- NL: remove directory tree '/folder_name' ref: shutil.rmtree('/folder_name') Output: os. path. dirname ( os. path. join ( dirpath, os. path. dirname ( _ _ file _ _ ) ) bleu_metric: 6.4718242450883325 ---------- NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) Output: df. apply ( lambda x : x. to _ frame ('new _ column'), axis = 1 ) bleu_metric: 34.619282106807496 ---------- NL: reverse sort Counter `x` by values ref: sorted(x, key=x.get, reverse=True) Output: sorted ( x, key = itemgetter ( 1 ) ) bleu_metric: 40.540555470323305 ---------- NL: reverse sort counter `x` by value ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) Output: sorted ( x, key = itemgetter ( 1 ), reverse = true ) bleu_metric: 15.53218858512746 ---------- NL: append a numpy array 'b' to a numpy array 'a' ref: np.vstack((a, b)) Output: np. concatenate ( ( a, b ) ) bleu_metric: 76.91605673134586 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: print(concatenate((a, b), axis=0)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 82.82477531331041 ---------- NL: numpy concatenate two arrays `a` and `b` along the second axis ref: print(concatenate((a, b), axis=1)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 100.0 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: c = np.r_[(a[None, :], b[None, :])] Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 9.520068110441361 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: np.array((a, b)) Output: print ( concatenate ( ( a, b ), axis = 1 ) ) bleu_metric: 41.70507291634131 ---------- NL: fetch address information for host 'google.com' ion port 80 ref: print(socket.getaddrinfo('google.com', 80)) Output: os. environ ['localhost : \ \ python27 \ \ python'] bleu_metric: 9.672649511413093 ---------- NL: add a column 'day' with value 'sat' to dataframe `df` ref: df.xs('sat', level='day', drop_level=False) Output: df ['value'] = df ['value']. apply ( lambda x : x ['value'] ) bleu_metric: 8.179879050970094 ---------- NL: return a 401 unauthorized in django ref: return HttpResponse('Unauthorized', status=401) Output: return httpresponse ( status = 204 ) bleu_metric: 15.451666492113134 ---------- NL: Flask set folder 'wherever' as the default template folder ref: Flask(__name__, template_folder='wherever') Output: app. run ( debug = true ) bleu_metric: 16.89983564524028 ---------- NL: How do I INSERT INTO t1 (SELECT * FROM t2) in SQLAlchemy? ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') Output: session. execute ('select * from from t1 ;') bleu_metric: 34.9740896964685 ---------- NL: sort a list of lists 'c2' such that third row comes first ref: c2.sort(key=lambda row: row[2]) Output: sorted ( l, key = lambda x : ( - x [ 1 ], x [ 2 ] ) ) bleu_metric: 22.169194554222997 ---------- NL: Sorting a list of lists in Python ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) Output: [ ( row [ i ], row [ i + 2 ] ) for i in range ( 0, len ( row ), row [ 2 ] bleu_metric: 26.189293164591476 ---------- NL: Sorting a list of lists in Python ref: c2.sort(key=lambda row: (row[2], row[1])) Output: [ ( row [ i ], row [ i + 2 ] ) for i in range ( 0, len ( row ), row [ 2 ] bleu_metric: 22.303080489498637 ---------- NL: set font `Arial` to display non-ascii characters in matplotlib ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) Output: ax. set _ ylabel ('y - r ', title ='\ u2022') bleu_metric: 6.906418747057865 ---------- NL: Convert DateTime column 'date' of pandas dataframe 'df' to ordinal ref: df['date'].apply(lambda x: x.toordinal()) Output: df ['date'] = df ['date']. map ( lambda x : x. date ( ) ) bleu_metric: 51.29497107827519 ---------- NL: get html source of Selenium WebElement `element` ref: element.get_attribute('innerHTML') Output: print ( element. get _ attribute ('innerhtml') ) bleu_metric: 18.69300079996002 ---------- NL: Get the integer location of a key `bob` in a pandas data frame ref: df.index.get_loc('bob') Output: df. reset _ index ( ) bleu_metric: 19.840332506055578 ---------- NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') Output: os. system ('echo $ filename. exe') bleu_metric: 12.044064401053362 ---------- NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` ref: my_dict.update({'third_key': 1}) Output: my _ dict ['my _ dict'] = var bleu_metric: 10.579369505074823 ---------- NL: declare an array ref: my_list = [] Output: variable = [ ] bleu_metric: 75.98356856515926 ---------- NL: Insert item `12` to a list `my_list` ref: my_list.append(12) Output: my _ list. append ('{ }, { } '. format ( c. c ) ) bleu_metric: 12.259149958656423 ---------- NL: add an entry 'wuggah' at the beginning of list `myList` ref: myList.insert(0, 'wuggah') Output: mylist = [ 'an ','i ','j'] bleu_metric: 11.502783619900047 ---------- NL: convert a hex-string representation to actual bytes ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') Output: " " " \ \ xc3 \ \ x85あ " " ". encode ('hex') bleu_metric: 24.712086993335753 ---------- NL: select the last column of dataframe `df` ref: df[df.columns[-1]] Output: df [ df. columns [ - 1 ] ] bleu_metric: 100.0 ---------- NL: get the first value from dataframe `df` where column 'Letters' is equal to 'C' ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] Output: df ['value'] = df. groupby ('value') ['value']. apply ( lambda x : x bleu_metric: 17.966302274629616 ---------- NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix ref: np.column_stack(([1, 2, 3], [4, 5, 6])) Output: matrix ( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ) bleu_metric: 35.61693754184905 ---------- NL: get the type of `i` ref: type(i) Output: print ( len ( a ) ) bleu_metric: 22.679164443904003 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type ( v ) bleu_metric: 100.0 ---------- NL: get the type of variable `variable_name` ref: print(type(variable_name)) Output: globals ( ) ['a'] bleu_metric: 16.784459625186194 ---------- NL: get the 5th item of a generator ref: next(itertools.islice(range(10), 5, 5 + 1)) Output: next ( ( iter ( range ( n ) ) ) for i in range ( n ) ) bleu_metric: 18.81478574691708 ---------- NL: Print a string `word` with string format ref: print('"{}"'.format(word)) Output: print ('\ n '. join ('% s = % s'% ( value, ) ) ) bleu_metric: 16.331948281960493 ---------- NL: join a list of strings `list` using a space ' ' ref: """ """.join(list) Output: list (''. join ( map ( str, list ) ) ) bleu_metric: 33.918247054808944 ---------- NL: create list `y` containing two empty lists ref: y = [[] for n in range(2)] Output: y = [ ( x, y ) for x in zip ( l1, l2 ) ] bleu_metric: 18.951629567590743 ---------- NL: read a file 'C:/name/MyDocuments/numbers' into a list `data` ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] Output: new _ list = [ line. rstrip ('\ t') for line in open ('filename. txt') ] bleu_metric: 37.34651115614923 ---------- NL: delete all occurrences of character 'i' in string 'it is icy' ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: re. sub ('i ','','it is icy') bleu_metric: 19.720950806796015 ---------- NL: delete all instances of a character 'i' in a string 'it is icy' ref: re.sub('i', '', 'it is icy') Output: re. sub ('i ','','it is icy') bleu_metric: 100.0 ---------- NL: delete all characters "i" in string "it is icy" ref: """it is icy""".replace('i', '') Output: " " " a b : b. c '! ='a') bleu_metric: 23.234178119893244 ---------- NL: How to delete all instances of a character in a string in python? ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: " " " " " ". join ( [ char for char in'it is icy'if char! ='i'] ) bleu_metric: 100.0 ---------- NL: Drop rows of pandas dataframe `df` having NaN in column at index "1" ref: df.dropna(subset=[1]) Output: df. drop ( df. index. duplicated ( ) bleu_metric: 17.996531271765896 ---------- NL: get elements from list `myList`, that have a field `n` value 30 ref: [x for x in myList if x.n == 30] Output: mylist = [ i for i in range ( 10 ) if i % 2 = = 0 ] bleu_metric: 11.709061630923735 ---------- NL: converting list of strings `intstringlist` to list of integer `nums` ref: nums = [int(x) for x in intstringlist] Output: nums = [ int ( x ) for x in intstringlist ] bleu_metric: 100.0 ---------- NL: convert list of string numbers into list of integers ref: map(int, eval(input('Enter the unfriendly numbers: '))) Output: [ int ( s ) for s in example _ string. split ( ',') ] bleu_metric: 11.433361115787454 ---------- NL: print "." without newline ref: sys.stdout.write('.') Output: sys. stdout. write ( '.') bleu_metric: 100.0 ---------- NL: round off the float that is the product of `2.52 * 100` and convert it to an int ref: int(round(2.51 * 100)) Output: int ( round ( 1123. 456789, 0 ) ) bleu_metric: 39.832871551569504 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: os.chdir('/mydir') for file in glob.glob('*.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 45.30799450827704 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: for file in os.listdir('/mydir'): if file.endswith('.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 95.90965597935381 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: for (root, dirs, files) in os.walk('/mydir'): for file in files: if file.endswith('.txt'): pass Output: for file in os. listdir ('/ mydir') : if file. endswith ( '. txt') : bleu_metric: 50.25808484829805 ---------- NL: plot dataframe `df` without a legend ref: df.plot(legend=False) Output: df. legend ( ). plot ( kind ='bar') bleu_metric: 21.305413619585085 ---------- NL: loop through the IP address range "192.168.x.x" ref: for i in range(256): for j in range(256): ip = ('192.168.%d.%d' % (i, j)) print(ip) Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 13.252382555750852 ---------- NL: loop through the IP address range "192.168.x.x" ref: for (i, j) in product(list(range(256)), list(range(256))): pass Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 5.334065332387164 ---------- NL: loop through the IP address range "192.168.x.x" ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) Output: 192. setsockopt ('192. 168. 1. 1 ','192. 168. 1. 255') bleu_metric: 61.58601992013119 ---------- NL: Sum the corresponding decimal values for binary values of each boolean element in list `x` ref: sum(1 << i for i, b in enumerate(x) if b) Output: float ( [ x for x in list ( d. values ( ) ) ] ) bleu_metric: 11.156857921595945 ---------- NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) Output: result = [ line. split ( ) for line in open ('filename. txt ','r') ] bleu_metric: 7.848329424931549 ---------- NL: Convert list of lists `data` into a flat list ref: [y for x in data for y in (x if isinstance(x, list) else [x])] Output: [ a for b in c for a in list _ of _ b ] bleu_metric: 8.372912227572577 ---------- NL: Print new line character as `\n` in a string `foo\nbar` ref: print('foo\nbar'.encode('string_escape')) Output: print ('\ n '. join ( chr ( i ) for i in text ) ) bleu_metric: 19.268479640608692 ---------- NL: remove last comma character ',' in string `s` ref: """""".join(s.rsplit(',', 1)) Output: s = s. replace ( ', ','' ) bleu_metric: 24.4545047029932 ---------- NL: calculate the mean of each element in array `x` with the element previous to it ref: (x[1:] + x[:-1]) / 2 Output: np. mean ( x, y ) bleu_metric: 8.67666492910357 ---------- NL: get an array of the mean of each two consecutive values in numpy array `x` ref: x[:-1] + (x[1:] - x[:-1]) / 2 Output: np. array ( [ [ 2, 3, 4 ], [ 2, 5, 6 ] ] ) bleu_metric: 9.410489957951636 ---------- NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype='