0

After running two of my loops:

for word in words:
    for i in range (1,p+1):
        print(word,len(re.findall(word, input_[i])),i)

I am getting example result like this:

businesses 0 1
businesses 0 2
businesses 2 3

But I would like achieve this:

businesses 0 1 0 2 2 3

I can get this result by this:

for word in words:
        print(word,len(re.findall(word, input_[i])),i,len(re.findall(word, input_[i+1])),i+1,len(re.findall(word, input_[i+2])),i+2)

But it's not the point because p can be dynamically changed so i don't want to use static values like i+1 i+2....

I will be gratefull for any help and suggestions.

Deny
  • 1
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Aran-Fey Apr 02 '18 at 08:24
  • Could you please provide a minimal working example that includes definitions of `words`, `input_`, and `p`? – MPA Apr 02 '18 at 08:28

2 Answers2

1

I suppose if you add add end=' ' in the end of your print function it will work fine.
as an example:

L = [1, 2, 3, 4, 5, 6]
for i in L:
    print(i, end=' ')  

the result will be:

1 2 3 4 5 6 
0

So simple! Thank you! Finally my code looks like this:

for word in words:
    print(word,end=' ')
    for i in range (1,p+1):
        print("->",len(re.findall(word, input_[i])),i,end=' ')
    print('\n')
Deny
  • 1