W
walterbyrd
This works, but it seems like there should be a better way.
This works, but it seems like there should be a better way.
--------------
week = ['sun','mon','tue','wed','thu','fri','sat']
for day in week[week.index('tue'):week.index('fri')]:
print day
---------------
print dayweek = ['sun','mon','tue','wed','thu','fri','sat']
tue_fri = slice(week.index('tue'), week.index('fri'))
for day in week[tue_fri]:
This works, but it seems like there should be a better way.
--------------
week = ['sun','mon','tue','wed','thu','fri','sat']
for day in week[week.index('tue'):week.index('fri')]:
print day
---------------
This works, but it seems like there should be a better way.--------------
week = ['sun','mon','tue','wed','thu','fri','sat']
for day in week[week.index('tue'):week.index('fri')]:
print day
---------------
I think you should provide much more information, primarily why you
want to do this. What is the larger goal you are trying to achieve?
walterbyrd said:I think you should provide much more information, primarily why youThis works, but it seems like there should be a better way.
--------------
week = ['sun','mon','tue','wed','thu','fri','sat']
for day in week[week.index('tue'):week.index('fri')]:
print day
---------------
want to do this. What is the larger goal you are trying to achieve?
I am just looking for a less verbose, more elegant, way to print a
slice of a list.
This works, but it seems like there should be a better way.--------------
week = ['sun','mon','tue','wed','thu','fri','sat']
for day in week[week.index('tue'):week.index('fri')]:
  print day
---------------
I think you should provide much more information, primarily why you
want to do this. Â What is the larger goal you are trying to achieve?
I am just looking for a less verbose, more elegant, way to print a
slice of a list. What is hard to understand about that? I am not sure
how enumerated types help.
I am just looking for a less verbose, more elegant, way to print a
slice of a list. What is hard to understand about that?
This works, but it seems like there should be a better way.--------------
week = ['sun','mon','tue','wed','thu','fri','sat'] for day in
week[week.index('tue'):week.index('fri')]:
  print day
---------------
I think you should provide much more information, primarily why you
want to do this. Â What is the larger goal you are trying to achieve?
I am just looking for a less verbose, more elegant, way to print a slice
of a list. What is hard to understand about that? I am not sure how
enumerated types help.
walterbyrd said:w> I am just looking for a less verbose, more elegant, way to print a
w> slice of a list. What is hard to understand about that? I am not sure
w> how enumerated types help.
You didn't say that in the OP.
But you can extend the list type to accept slices with strings in them.
The language spec says they should be ints but apparently this is not
enforced. Of course this makes it vulnerable for future misbehaviour.
class KeyList(list):
def __getitem__(self, indx):
if isinstance(indx, slice):
start = indx.start
stop = indx.stop
# add support for step if you want
if not isinstance(start, int):
start = self.index(start)
if not isinstance(stop, int):
stop = self.index(stop)
return list.__getitem__(self, slice(start,stop))
return list.__getitem__(self, indx)
week = KeyList(['sun','mon','tue','wed','thu','fri','sat'])
for day in week['tue':'fri']:
print day
tue
wed
thu
Note that 'fri' is not included according to standard Python conventions
about the end of a slice. Change the code if you are not happy with it
and you don't mind getting inconsistent semantics.
That's just the same micro-goal re-stated. What is your larger problem
of which this is a part? Perhaps a better approach can be suggested when
that context is known.
I am processing a huge spreadsheet which I have converted to a csv
format. Each row will be a wiki page with several sub-headings. The
spreadsheet contains information about servers. Wiki sub-headings may
include: 'hardware', 'software', 'users', 'network swith settings'.
'Hardware' may include the spreadsheet columns: 'memory', 'cpu', and
so on. So the first six columns in the spreadsheet may go under
'hardware' the next six under 'software' and so on.
I have already created the wiki pages, using a method similar to what
I first posted. But, it seems like there should be a better way to to
do it. So, for future reference, I was just wondering.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.