Python print statement dependent on lines following it? [duplicate] - python

This question already has answers here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Python print immediately?
(1 answer)
Closed 2 years ago.
I have the following code:
for file_name, content in corpus.items():
print('here')
content = [list(filter(lambda index: index not in remove_indices, content))]
corpus[file_name] = np.array(content).astype(np.uint32)
Where corpus is a 800,000 long dictionary with string keys and array values.
Things were taking forever so I decided to check how fast each iteration was by adding in that print statement.
If I comment the last two lines out it prints lots of heres really fast, so there's no problem with my iterator. What's really weird is that when I uncomment the last two lines, here takes a long time to print, even for the first one! It's like the print statement is somehow aware of the lines that follow it.
I guess my question speaks for itself. I'm in Jupyter notebook, if that helps.

Related

I don't understand what is within the brackets of some sample code [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I have a simple two lines of code but fail to understand what is actually happening.
s = 'We\'re' + 'Here'
print(s[4::2])
The result is just eee. I fail to see how the [4::2] is working to cause the code to print eee.
It is something like [start: end: step], slice addresses can be written as s[start:end:step] and any of start, stop or end can be dropped. So, s[4::2] is starting from 4th and every 2nd element of the sequence(Here you dropped the end part). That's why it is returning eee
s = 'We\'re' + 'Here'
print(s[4::2])

Overwriting printed statements in the IDLE in Python [duplicate]

This question already has answers here:
How to output to the same line overwriting the previous line?
(4 answers)
Closed 3 years ago.
I am looking for a method so that when something is printed continuously, it overwrite what the previous printed statement says. for example, if I am making a counter, normally the outcome in the IDLE would be: 1 2 3 4...., but however I'm looking to rewrite/overwrite what the previous printed statement says so it say "1" for a second then "2" appears but we can no longer see "1". Any suggestions? Sorry about how wordy this question is, I was having a hard time trying to write this where another person understands.
import time
arr = [1,2,3,4]
for element in arr:
print(element, end='\r')
time.sleep(1)
end='\r' in the print statement does the trick

how to get out of nested loop and continue with iteration of outer loop [duplicate]

This question already has answers here:
Exit while loop in Python [duplicate]
(6 answers)
Closed 3 years ago.
Write a python function, find_ten_substring(num_str) which accepts a string and returns the list of 10-sub strings of that string.
A 10-sub string of a number is a sub string of its digits that sum up to 10.
sample input='3523014'
actual output=['5230', '23014', '523', '352']
i have tried the below code it is printing only one sub string that adds up 10 (only [28])and then terminating.
def find_ten_substring(num_str):
sumi=0
list1=[]
substr=''
for i in range(0,len(num_str)):
for j in range(i,len(num_str)):
sumi=sumi+int(num_str[j])
substr=substr+num_str[j]
if(sumi==10):
list1.append(substr)
print(list1)
break
sumi=0
substr=''
continue
num_str="2825302"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
You specifically told it to quit as soon as it found that one solution. Look at the bottom of your outer loop:
sumi=0
substr=''
break
This resets the accumulation variables, but then breaks the loop rather than repeating. Remove the break and return to your code development -- you have other errors, starting with lack of any return value.
Also, you should learn basic debugging. For starters, a few print statements inside your code can trace the data and control flow. See this lovely debug blog for help.

Generate multiple OR/AND statements [duplicate]

This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 4 years ago.
I am working on python script that splits text in different blocks based on keywords used in text.
Currently I split text into blocks with sth like this (for 1 block, others have pretty much the same strucure):
if (line.strip().lower().startswith('ключевые навыки')
or line.strip().lower().startswith('дополнительная информация')
or line.strip().lower().startswith('знания')
or line.strip().lower().startswith('личные качества')
or line.strip().lower().startswith('профессиональные навыки')
or line.strip().lower().startswith('навыки')):
But, it is possible that list of keywords is going to expand. Is there a possibility to generate multiple or statements based on some array of possible keywords?
Try this code
values=['ключевые навыки','дополнительная информация','знания']
val=True
#enter any words you want to check
while val
for i in values:
if (line.strip().lower().startswith(i)):
#whatever code you want to implement
val=False
#to exit loop
Hope it helps :)

Python overwrite new line not behaving as expected [duplicate]

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 9 years ago.
I am trying to output text to stdout, overwriting the previous text, for example
for i in range(12):
print i
but with i replacing the previous value each time rather than appearing on a new line
From looking at quite a few previous posts with similar questions it seems that there are a few ways of doing this, possibly the simplest being (for Python 3.x on)
for i in range(12):
print(i,end="\r")
sometimes with a comma at the end of the print statement, sometimes not. However, without the comma I get no output at all and with the comma I get
(None,)
(None,)
(None,)
...
Is this something related to my terminal perhaps? I get similar results no matter which of the previous posted solutions to the problem I try.
Thanks for any help!
If you do this in a terminal you won't see any output because the for loop finishes too fast for you to see the output changing. After the loop is done the terminal's prompt overwrites the output.
Try this instead:
>>> for i in range(9999999):
... print(i, end="\r")
I'm surprprised you don't get a syntax error when omitting the comma.

Categories