Sign in to follow this  
davalend

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

Recommended Posts

Give us some specific questions and sure, we'll help. And in case this occurs to you to try it, a listing of your C++ code with "How do I convert this to Python?" is NOT a specific question.

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites

now i got other problem.

t = len(a) * 0.6

t will definitly be a decimal. how can i change it to the whole number without the decimal, so that i can apply to n in foo[m:n].

n has to be integer

Share this post


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...
Sign in to follow this