como podría hacer una función que pasandole una lista y N(número entero) devuelva una nueva lista formada por los valores N-ésimo, 2N-ésimo, 3N-ésimo, etc., de la lista?
Por ejemplo, si la lista contiene los valores [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] y N=3, devolverá la lista formada por los valores 30, 60, 90 ,es decir, los valores de 3 en 3
Y como se podría hacer con lista posicionales con el propio TAD de python con estas funciones?
PositionalList()
• add_last(e)
• add_first(e)
• add_before(p, e)
• add_after(p, e)
• replace(p, e)
• delete(p)
• first() -> p
• last() -> p
• is_empty() -> b
• before(p) -> p’
• after(p) -> p’
• get_element(p) -> e
• len() -> n
Esta sería la clase TAD :
class LinkedOrderedPositionalList(_DoublyLinkedBase):
"""A sequential container of elements allowing positional access."""
#-------------------------- nested Position class --------------------------
class Position:
"""An abstraction representing the location of a single element.
Note that two position instaces may represent the same inherent
location in the list. Therefore, users should always rely on
syntax 'p == q' rather than 'p is q' when testing equivalence of
positions.
"""
def __init__(self, container, node):
"""Constructor should not be invoked by user."""
self._container = container
self._node = node
def element(self):
"""Return the element stored at this Position."""
return self._node._element
def first(self):
"""Return the first Position in the list (or None if list is empty)."""
return self._make_position(self._header._next)
def last(self):
"""Return the last Position in the list (or None if list is empty)."""
return self._make_position(self._trailer._prev)
def before(self, p):
"""Return the Position just before Position p (or None if p is first)."""
node = self._validate(p)
return self._make_position(node._prev)
def after(self, p):
"""Return the Position just after Position p (or None if p is last)."""
node = self._validate(p)
return self._make_position(node._next)
def get_element(self, p):
"""Return the Element at position p of the list."""
self._validate(p)
return p.element()
def _add_last(self, e):
"""Insert element e at the back of the list and return new Position."""
return self._insert_between(e, self._trailer._prev, self._trailer)
# helper method
def _add_first(self, e):
"""Insert element e at the front of the list and return new Position."""
return self._insert_between(e, self._header, self._header._next)
# helper method
def _add_after(self, p, e):
"""Insert element e into list after Position p and return new Position."""
original = self._validate(p)
return self._insert_between(e, original, original._next)
def add(self, e):
"""Insert element e into list and return new Position."""
if self.is_empty() or e > self.get_element(self.last()):
return self._add_last(e)
else:
p = self.last()
while p != None and self.get_element(p) >= e:
p = self.before(p)
if p == None:
return self._add_first(e)
else:
return self._add_after(p, e)
def delete(self, p):
"""Remove and return the element at Position p."""
original = self._validate(p)
return self._delete_node(original) # inherited method returns element
def replace(self, p, e):
"""Replace the element at Position p with e.
Return the element formerly at Position p.
"""
original = self._validate(p)
old_value = original._element # temporarily store old element
original._element = e # replace with new element
return old_value # return the old element value
Esto es lo que he intentado pero no lo he conseguido:
l=Lista() #lista con los valores agregados
l2=Lista() #lista vacía
def ej1(l,l2,N):
count=0
pos = l.first()
for i in l:
if l.get_element(pos)!=None:
count+=N
l2.add(l.get_element(pos))
ej1(l,l2,3)
nueva_lista = lista[2::3]