Simple Python Code Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Python Code Question
# 1  
Old 05-27-2018
Simple Python Code Question

I have the following code:

Code:
#!/usr/bin/env python

mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        #print(objects)
        objects = objects.replace('hello', "hi")
        print objects

When executed, it gives the following output:

Code:
## ./loop.py 
hi
you
are
so
cool

But i want it to print the output all in one line. I want to be able to decide on whether i allow it to print the items on a new line, or to print them all on one line.

In order words, the desired output should be:

Code:
## ./loop.py 
hi you are so cool

how can my code be modified to do this?

OS: Linux/Unix
# 2  
Old 05-27-2018
Hi skysmart...

I have no idea what you are trying to do but your function is not called.
I guess you are trying to put that function in the interpreter namespace for other uses.

The function has a useless line and looks as though you want Python version 2.x.x and the print STATEMENT.
If you require version 3.x.x then you need the print FUNCTION and the end=" " _flag_.
Code:
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        # Note the comma below.
        print objects ,

printWithoutNewlines()

OSX 10.13.4, default bash terminal calling python2.7.
Code:
Last login: Sun May 27 10:36:33 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> chmod 755 loop.py
AMIGA:barrywalker~/Desktop/Code/Python> ./loop.py
hi you are so cool
AMIGA:barrywalker~/Desktop/Code/Python> _


Last edited by wisecracker; 05-27-2018 at 06:57 AM..
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 05-28-2018
Quote:
Originally Posted by wisecracker
Hi skysmart...

I have no idea what you are trying to do but your function is not called.
I guess you are trying to put that function in the interpreter namespace for other uses.

The function has a useless line and looks as though you want Python version 2.x.x and the print STATEMENT.
If you require version 3.x.x then you need the print FUNCTION and the end=" " _flag_.
Code:
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        # Note the comma below.
        print objects ,

printWithoutNewlines()

OSX 10.13.4, default bash terminal calling python2.7.
Code:
Last login: Sun May 27 10:36:33 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> chmod 755 loop.py
AMIGA:barrywalker~/Desktop/Code/Python> ./loop.py
hi you are so cool
AMIGA:barrywalker~/Desktop/Code/Python> _


thank you. i intended to put the following in my original code:

Code:
printWithoutNewlines()

but it looks like its not part of what i copied and pasted.

another problem im having is, before and after implementing your suggested change, i have been unable to put the output of the called function into a variable.

My original code:

Code:
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        print objects

functionsOutput = printWithoutNewlines()


when I run the script, even though Im not printing out the output of "functionOutput", i still see the output.

how can i keep the results of the printWithoutNewlines function in the "functionOutput" variable without it being outputted out to the screen?

and how can I do a replacement on the content of the functionOutput variable?

here is what i have tried:

# note that im removed the comma you recommended. i did that on purpose.

Code:
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        print objects

functionsOutput = printWithoutNewlines()
functionsOutput.replace('\n', ' ')

what I'm intending to do with the above code is to run the defined function, then, remove the new lines from the output of the function.

When I run the above code, I get this error:

Code:
hi
you
are
so
cool
Traceback (most recent call last):
  File "./vego.py", line 10, in <module>
    functionsOutput.replace('\n', ' ')
AttributeError: 'NoneType' object has no attribute 'replace'

# 4  
Old 05-28-2018
Again I have no idea why so convoluted a method as your function:
DEMO:
Code:
#!/usr/bin/python2.7

mylist = [ "hello", "you", "are", "so", "cool" ]

STRING = ' '.join(mylist)
NEWSTRING = STRING.replace('hello', "hi")
print STRING
print NEWSTRING
print

# This function should work on ANY version of Python from at least 1.4.
def printWithoutNewlines():
    mystring = ""
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        mystring = mystring + objects + " "
    return(mystring)

print printWithoutNewlines()

functionsOutput_sp = printWithoutNewlines()

print functionsOutput_sp

functionsOutput_nl = functionsOutput_sp.replace(' ', '\n')

print functionsOutput_nl ,

Results, same machine as before.
Code:
Last login: Mon May 28 14:14:06 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> execfile("loop.py")
hello you are so cool
hi you are so cool

hi you are so cool 
hi you are so cool 
hi
you
are
so
cool
>>> dir()
['NEWSTRING', 'STRING', '__builtins__', '__doc__', '__name__', '__package__', 'functionsOutput_nl', 'functionsOutput_sp', 'mylist', 'printWithoutNewlines']
>>> exit()
AMIGA:barrywalker~/Desktop/Code/Python> _

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. OS X (Apple)

Python script to do simple audio capture...

This site is the first to get this snippet. It will capture an audio recording of any time length within the limits of OSX's QuickTime Player's capablility... A shell script derivative of this will be used as a further capture for CygWin's AudioScope.sh. Thoroughly read ALL the comments in... (0 Replies)
Discussion started by: wisecracker
0 Replies

2. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

3. Programming

Simple encryption in python

Hi, i have the below script: ###############################SimpleEncryption################################ #Simple encryption implemented in python #Author:pandeeswaran ############################################################################### def Encrypt(input): res='' ... (13 Replies)
Discussion started by: pandeesh
13 Replies

4. Homework & Coursework Questions

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

5. Shell Programming and Scripting

Help with Simple Perl/Python Script

I have the following problem, which I need done in Perl/ or Python using Unix/linux filters... 1. You have a very large file, named 'ColCheckMe', tab-delmited, that you are asked to process. You are told that each line in 'ColCheckMe' has 7 columns, and that the values in the... (1 Reply)
Discussion started by: Swapnilsagarwal
1 Replies

6. Red Hat

Writing simple python setup commands

Building software in most languages is a pain. Remember ant build.xml, maven2 pom files, and multi-level makefiles? Python has a simple solution for building modules, applications, and extensions called distutils. Disutils comes as part of the Python distribution so there are no other packages... (0 Replies)
Discussion started by: Linux Bot
0 Replies

7. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question