[Tutor] Make a linked list subscriptable?

2019-07-11 Thread Sarah Hembree
How might I best make a linked list subscriptable? Below is skeleton code
for a linked list (my
actual is much more). I've done __iter__ and __next__ but I would like to
be able to do start:stop:stride I just can't figure out how. Suggestions or
just hints please?



# -*- coding: utf8 -*-

class Node:
def __init__(self, val=None, nxt=None):
self.val = val
self.next = nxt

class LinkedList:
def __init__(self, node=None):
self.root = node
self.length = 0

def prepend(self, data):
""" add data to head/root of list """
if self.root == None:
self.root = Node(data)
self.length = self.length + 1
else:
n = Node(data)
n.next = self.root
self.root = n
self.length = self.length + 1

def pop(self):
""" Remove first data node """
t = self.root.val
if self.root:
self.root = self.root.next
self.length = self.length - 1
return t

def __repr__(self):
tmp = self.root
s = ''
while tmp:
s = s + str(tmp.val) + '> '
tmp = tmp.next

return s[:-2]

ll = LinkedList()
[ll.prepend(x) for x in range(14,-1,-1)]

>>> ll
0> 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14







--- We not only inherit the Earth from our Ancestors, we borrow it from our
Children. Aspire to grace.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] __weakref__ question

2019-07-24 Thread Sarah Hembree
Can anyone provide some simple example/s or two or three of using weakref?
I'm baffled and not seeing any documentation that is meaningful. My
interest is to minimize memory usage (generally speaking, overall) and am
wondering if this might help.

--- We not only inherit the Earth from our Ancestors, we borrow it from our
Children. Aspire to grace.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Chunking list/array data?

2019-08-22 Thread Sarah Hembree
How do you chunk data? We came up with the below snippet. It works (with
integer list data) for our needs, but it seems so clunky.

def _chunks(lst: list, size: int) -> list:
return  [lst[x:x+size] for x in range(0, len(lst), size)]

What do you do? Also, what about doing this lazily so as to keep memory
drag at a minimum?


--- We not only inherit the Earth from our Ancestors, we borrow it from our
Children. Aspire to grace.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor