0

I have a list as below

mylist = ['f', 'i', 'r', 's', 't', ' ', 'r', 'a', 'y', ' ', '>', ' ', 's', 'a', 'n', 'k', 'r', 'i', 't', ' ', '>', ' ', 'f', 'e', 'm', 'a', 'l', 'e', ' ', '>', ' ', ' ', '=', ' ', 'a', 'a', 'd', 'h', 'y', 'a', '\n', 'u', 'n', 'i', 'q', 'u', 'e', ' ', '>', ' ', 's', 'a', 'n', 'k', 'r', 'i', 't', ' ', '>', ' ', 'm', 'a', 'l', 'e', ' ', '>', ' ', ' ', '=', ' ', 'a', 'd', 'h', 'v', 'a', 'i', 't', 'h']

I have a sequence generated based on the list:

sequence = np.arange(0, len(mylist) - 11, 3) 

it generates

[0,  3,  6,  9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66]

My output is a 2 dimensional List

loop on the sequence

    dimension1= mylist[sequence:sequence+11]
    dimension2 = mylist[sequence+12]

Final Output List should look like:

dimension1                                              dimension2
"f" "i" "r" "s" "t" " " "r" "a" "y" " " ">" " "         "s"
"s" "t" " " "r" "a" "y" " " ">" " " "s" "a" "n"         "s"
"r" "a" "y" " " ">" " " "s" "a" "n" "s" "k" "r"         "i"
`

I want to generate this without writing for loop.. I can do that in R as below

mylist = list (c('f', 'i', 'r', 's', 't', ' ', 'r', 'a', 'y', ' ', '>', ' ', 's', 'a', 'n', 'k', 'r', 'i', 't', ' ', '>', ' ', 'f', 'e', 'm', 'a', 'l', 'e', ' ', '>', ' ', ' ', '=', ' ', 'a', 'a', 'd', 'h', 'y', 'a', '\n', 'u', 'n', 'i', 'q', 'u', 'e', ' ', '>', ' ', 's', 'a', 'n', 'k', 'r', 'i', 't', ' ', '>', ' ', 'm', 'a', 'l', 'e', ' ', '>', ' ', ' ', '=', ' ', 'a', 'd', 'h', 'v', 'a', 'i', 't', 'h'))

mylist = unlist(mylist)
dataset <- map(
  seq(1, length(mylist) - 12-1, by = 3), 
  ~list(sentece = mylist[.x:(.x + 11)], next_char = mylist[.x + 12])
) 

dataset <- transpose(dataset)

Could some one please help me in tranlating the above R code to Python

1 Answers1

1

We could initialize a dictionary and append values to it

import pandas as pd
maxlen = 12
dictN = {'dimension1':[], 'dimension2':[]}
mlenN = maxlen  - 1

for i in range(0, len(mylist) - mlenN, 3):
    dictN['dimension1'].append(mylist[i:(i+mlenN)])
    dictN['dimension2'].append(mylist[i + maxlen-1])

pd.DataFrame(dictN)

Or as @Parfait mentioned in the comments, it can be written as a dictionary comprehension

maxlen = 12
mlenN = maxlen - 1
dictN = [{'dimension1':mylist[i:(i+mlenN)], \
          'dimension2':mylist[i + maxlen-1]} \
         for i in range(0, len(mylist) - mlenN, 3)]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • is there a way to eliminate for loop and use map function – CHANDRA TIRUPATI Oct 12 '18 at 20:00
  • @Parfait `list` comprehension or dictionary comprehension is still a loop, but it is more concise for writing in a single line – akrun Oct 12 '18 at 20:19
  • 1
    Correct. But like R's `lapply` no bookkeeping needed. See list/dict comprehension: `maxlen = 12; mlenN = maxlen - 1; dictN = [{'dimension1':mylist[i:(i+mlenN)], 'dimension2':mylist[i + maxlen-1]} for i in range(0, len(mylist) - mlenN, 3)]` – Parfait Oct 12 '18 at 20:20
  • 1
    Though there is some discussion, it runs slightly faster than traditional loop with `append`. See [Are list-comprehensions and functional functions faster than “for loops”?](https://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops). – Parfait Oct 12 '18 at 20:21