Python: Establishing List with List Comprehensions or Iterators -
i am trying improve coding, , algorithm performance replacing for
loops list comprehensions, generators, , iterators.
i having hard time rapping head around how implement tools in itertools
, , appreciate give.
i trying initialize list value 0
range of indices. here 2 ways came with:
count_list = [0 index in range(4 ** k)]
and
total_index = list(range(4 ** k)) count_list = [0 index in total_index]
(k represents integer of the number letter in word made of 4 letter alphabet)
when timed code in python 3.4, turned out first, using generator faster method when looking @ in isolation, when had reuse 4 ** k
index loop, methods ended timing @ same speed.
what have been struggling figure out how can use iterator replicate initialization. know if wanted create list of of numbers in index use list(range())
or use
index_list = [index index, values in enumerate(words)]
i not sure how assign value 0
each element using that.
i wondering how might use list comprehension rid of for
loop. guessing need map, not sure how implement.
for index in range(4 ** k): if frequency_array[index] >= t: clump[index] = 1
thank you.
to initialize list 0
:
>>> my_list = [0] * 10 >>> my_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
if want use list
comprehension instead of for
loop, can way:
>>>t = 3 >>>l = [1,2,3,1,2,3,1,2,3,3,2,1] >>>new_list = [1 if l[i] >= t else l[i] in range(len(l))] >>>new_list [1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1]
also, can use and - or
combination, way:
>>>new_list = [l[i] >= t , 1 or l[i] in range(len(l))] >>>new_list [1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1]
Comments
Post a Comment