简体   繁体   中英

Printing from a single list python

EDIT:

 ['Station 5 average is less than 1.62644628099', 'Station 6 average is less than 1.62644628099', 'Station 7 average is less than 1.62644628099', 'Station 8 average is less than 1.62644628099', 'Station 9 average is less than 1.62644628099', 'Station 10 average is less than 1.62644628099']

and

['Station 1 average is greater than 1.62644628099', 'Station 2 average is greater than 1.62644628099', 'Station 3 average is greater than 1.62644628099']

Is there a code that I can use to produce a result such that the output when printed looks like this

Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099 
Station 8 average is less than 1.62644628099 
Station 9 average is less than 1.62644628099 
Station 10 average is less than 1.62644628099

and

Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

Thanks for reading and any advice/help you can give.

Use the str.join() function, which prints each element in a list with the str in between. Here I insert a new line between each element.

>>> lst1 = ['Station 5 average is less than 1.62644628099', 'Station 6 average is less than 1.62644628099', 'Station 7 average is less than 1.62644628099', 'Station 8 average is less than 1.62644628099', 'Station 9 average is less than 1.62644628099', 'Station 10 average is less than 1.62644628099']
>>> print '\n'.join(lst1)
Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099
Station 8 average is less than 1.62644628099
Station 9 average is less than 1.62644628099
Station 10 average is less than 1.62644628099

Same for the second list:

>>> lst2 = ['Station 1 average is greater than 1.62644628099', 'Station 2 average is greater than 1.62644628099', 'Station 3 average is greater than 1.62644628099']
>>> print '\n'.join(lst2)
Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

Or, you can use a for-loop:

>>> for i in lst1:
...     print i
... 
Station 5 average is less than 1.62644628099
Station 6 average is less than 1.62644628099
Station 7 average is less than 1.62644628099
Station 8 average is less than 1.62644628099
Station 9 average is less than 1.62644628099
Station 10 average is less than 1.62644628099

>>> for i in lst2:
...     print i
... 
Station 1 average is greater than 1.62644628099
Station 2 average is greater than 1.62644628099
Station 3 average is greater than 1.62644628099

It's probably preferable to use the join() function, as it actually returns the result, so you can use it later in your code if you wish.

Join the list members with the newline character.

print "\n".join(t)
print
print "\n".join(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM