Anyone Here Who Is Know Both Python And C++ ?


Recommended Posts

this is my python code

----------------------------------

read = open('sample.txt', 'r')

a = read.readlines()

for line in a :

print line,

------------------------------------

the above code will print all the line in the file. but if i wan it to print only certain % of the line(for example: 50% of the line). how do i code it?

Edited by davalend
Link to post
Share on other sites

The syntax foo[m:n] will give you a slice of the list foo from indices m (inclusive) to n (exclusive). len(foo) will give you the length of the list foo. Sooo...

for e in foo[0:len(foo)/2]:
print e

will print the first half the list. Careful if len(foo) is odd: integer division rounds down. Same trick works with strings.

Edited by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...