[Python Glasgow] consecuetive members of a list

Mark Smith mark.smith at practicalpoetry.co.uk
Wed Apr 30 12:40:04 CEST 2014


Sorry - I rushed that, and the code is a bit horrible (even though it
works). A better answer is:

def ngram(seq, n):
    return zip(*[seq[i:] for i in range(n)])

(Note the extra variable 'i' instead of re-using 'n' - which was an
accident) :-)

Also, the result is already a list, so there's no need to wrap the function
call with list() in the print statement.


On 30 April 2014 11:26, Marc Cameron <marccameron3d at gmail.com> wrote:

> Ah nice! You are welcome, it would seem that is more usable straight out
> the box. Also makes a Vector2 list rather than a nasty string list I was
> building!
>
> Thanks for sharing :) good luck with your project!
>
> M
>
>
> On 30 April 2014 11:19, Hedieh Ebrahimi <hedieh.ebrahimi at amphos21.com>wrote:
>
>> Thanks Marc
>>
>> Ill try that out, but hopefully I just found this  that works for me
>>
>> def createRangeOfIndices(self,myArray,d):
>>         ranges=[]
>>         for index in range(len(myArray)):
>>             if index < len(myArray)-1:
>>                 myTuple=(myArray[index],myArray[index+1])
>>                 ranges.append(myTuple)
>>         return ranges
>>
>>
>> Thanks :)
>>
>>
>> On 30 April 2014 12:09, Marc Cameron <marccameron3d at gmail.com> wrote:
>>
>>> Hi Hedieh,
>>>
>>> myList = [1,7,19,1110]
>>> newList = []
>>> for x in range(0, len(myList)-1):
>>>     newStr = "(,)"
>>>     newList.append("("+str(myList[x])+","+str(myList[x+1])+")")
>>>
>>> Not sure if it meets your requirements, although this should work for
>>> all intents and purposes. I am still relatively new to python so there may
>>> be a better way.
>>>
>>> Output = ['(1,7)', '(7,19)', '(19,1110)']
>>>
>>> Cheers,
>>>
>>> Marc C
>>> ------------------
>>>
>>> Hi all,
>>>
>>> Imagine I have a list like this:
>>>
>>> myList= [1, 7, 19, 1110]
>>>
>>> from this list I like to create a list of tuple of ranges like this.
>>>
>>> [(1,7),(7,19),(19,1110)]
>>>
>>> How can I do this in an efficient way ?
>>>
>>> Thanks in advance.
>>>
>>>
>>> On 30 April 2014 10:53, Hedieh Ebrahimi <hedieh.ebrahimi at amphos21.com>wrote:
>>>
>>>> Hi all,
>>>>
>>>> Imagine I have a list like this:
>>>>
>>>> myList= [1, 7, 19, 1110]
>>>>
>>>> from this list I like to create a list of tuple of ranges like this.
>>>>
>>>> [(1,7),(7,19),(19,1110)]
>>>>
>>>> How can I do this in an efficient way ?
>>>>
>>>> Thanks in advance.
>>>>
>>>> _______________________________________________
>>>> Glasgow mailing list
>>>> Glasgow at python.org
>>>> https://mail.python.org/mailman/listinfo/glasgow
>>>>
>>>>
>>>
>>>
>>> --
>>> Marc Cameron
>>>
>>> *Contract Technical Artist and AR Generalist*
>>> t: *+447595491710*
>>> e: marccameron3D at gmail.com
>>>
>>
>>
>
>
> --
> Marc Cameron
>
> *Contract Technical Artist and AR Generalist*
> t: *+447595491710*
> e: marccameron3D at gmail.com
>
> _______________________________________________
> Glasgow mailing list
> Glasgow at python.org
> https://mail.python.org/mailman/listinfo/glasgow
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/glasgow/attachments/20140430/437a1c20/attachment-0001.html>


More information about the Glasgow mailing list