-6
import random
import timeit
data = [random.randint(-10,10) for i in range(10)]
timeit.timeit('filter(lambda x : x>=0 ,"data")')

As shown in the code. if I try to remove the "" from "data", it would throw an error. Why? There is still a single quote containing the whole filter line. Thank you for helping!!

2 Answers2

0
timeit.timeit('filter(lambda x : x >= 0, {})'.format(data))
PygmyPony
  • 21
  • 4
  • Thank you for your help, but why would data without quotation marks make the syntax wrong? – Charlie Li Mar 16 '18 at 06:24
  • Not familiar with timeit, but it looks like it accepts strings as arguments. In this scenario without the double quotes, it is recognizing your argument as one entire string, instead of a string with a variable. – PygmyPony Mar 16 '18 at 06:28
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Mar 16 '18 at 08:24
-1

data is string and it should be under quote "" unless you store it in another variable like

data="data"
Roushan
  • 4,074
  • 3
  • 21
  • 38