8 Replies - 334 Views - Last Post: 25 January 2014 - 01:05 PM Rate Topic: -----

#1 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Tuple and slicing

Posted 24 January 2014 - 02:54 PM

Just say I had a tuple which looks like this:

('hello', 'goodbye', 'evening')

how would be able to slice the last 4 letters from each word and join them together to end up like this:

'ellodbyening'



thanks in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: Tuple and slicing

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Tuple and slicing

Posted 24 January 2014 - 03:12 PM

Achieving this uses fundamental features of Python, so what have you tried?

In particular, how you would examine each element of the tuple?

This post has been edited by andrewsw: 24 January 2014 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Re: Tuple and slicing

Posted 24 January 2014 - 03:30 PM

View Postandrewsw, on 24 January 2014 - 03:12 PM, said:

Achieving this uses fundamental features of Python, so what have you tried?

In particular, how you would examine each element of the tuple?


Its been a while since ive used python but all I know in terms of slicing is being able to slice single character or word
from a tuple e.g.
[-4]

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Tuple and slicing

Posted 24 January 2014 - 03:40 PM

Looks like you have quite a lot of re-studying to do. I can only hope that the following code will encourage you to do so:

my_tup = ('hello', 'goodbye', 'evening')
result = ''
for word in my_tup:
    result = result + word[-4:]

print(result)
# ellodbyening

http://docs.python.o...s-and-sequences
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Tuple and slicing

Posted 25 January 2014 - 04:00 AM

Well, if we're throwing out code, a nice pythony way to do this would be:
>>> dat = ('hello','goodbye','evening')
>>> ''.join(w[-4:] for w in dat)
'ellodbyening'
>>>



If you aren't using list comprehensions, you're really missing at lot of the fun of python.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Tuple and slicing

Posted 25 January 2014 - 06:15 AM

I don't like to throw out code ;) but I'm being optimistic on this occasion that it will kick-start the OP.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Tuple and slicing

Posted 25 January 2014 - 09:28 AM

I understand. It's just that if you play with Python a lot, and you see something that could be done with a list comprehension and isn't, you have to restrain yourself from rewriting it.

Obviously, I failed. I believe functional languages may have scarred me...
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Tuple and slicing

Posted 25 January 2014 - 09:35 AM

Comprehensions are fab and sexy :)
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Tuple and slicing

Posted 25 January 2014 - 01:05 PM

View Postbaavgai, on 25 January 2014 - 11:28 AM, said:

I understand. It's just that if you play with Python a lot, and you see something that could be done with a list comprehension and isn't, you have to restrain yourself from rewriting it.


This is true. The trouble is that if you're not careful, you end up with a lot of nested comprehensions that can be really horrible to read. I'm still trying to find a nice way to deal with this. The best thing I've found so far has been a two-fold approach. First, when I find myself nesting a comprehension I give serious thought to wrapping the inner comp in a function, so I'll have at least one verb in there. But since this can lead to a proliferation of functions, I use this as a sort of code smell to tell me when I've come up with a bad representation of the data.

This is an important thing that I seem to learn from time to time: most of the time, when you find your code is getting unwieldy, it's a sign that your data is badly structured. I'm pretty sure that if I ever really learn this lesson, I will transcend this earthly plane and become as time, destroyer of worlds, or something like that.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1