Sponsored By

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this