give python code for `arr = %w("blink", "freakonomics", and "outliers") arr.delete_at(2) "[\"\\\"blink\\\",\", \"\\\"freakonomics\\\",\", \"\\\"outliers\\\"\"]" arrnew = str.scan(/\w+/)` in python

To achieve the same functionality in Python, you can use the following code:

main.py
arr = ["blink", "freakonomics", "outliers"]
arr.pop(2)
print(arr)  # Output: ['blink', 'freakonomics']

arrnew = ' '.join(arr).split()
print(arrnew)  # Output: ['blink', 'freakonomics']
186 chars
7 lines

Explanation:

  1. In Python, you can define an array using square brackets [] instead of %w() in Ruby.
  2. The pop() function is used to remove and return the element at the specified index from the array. In this case, we remove the element at index 2 using arr.pop(2).
  3. Printing arr after removing the element will give you ['blink', 'freakonomics'].
  4. To convert the resulting string blink freakonomics back into an array, we can use the split() function. The ' '.join(arr) part joins the elements of arr with a space in between, and split() splits the resulting string at each space, creating a list of words.

Note: In Python, the array indices start from 0, so the element at index 2 is 'outliers' and not 'and'.

related categories

gistlibby LogSnag