Python print array with new line - python

I'm new to python and have a simple array:
op = ['Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye']
When i use pprint, i get this output:
['Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye']
Is there anyway i can remove the quotes, commas and brackets and print on a seperate line. So that the output is like this:
Hello
Good Morning
Good Evening
Good Night
Bye

You could join the strings with a newline, and print the resulting string:
print "\n".join(op)

For Python 3, we can also use list unpack
https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists
print(*op, sep='\n')
It's the same as
print('Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye', sep='\n')

Here's a couple points of clarification
First of all what you have there is a list, not an array. The difference is that the list is a far more dynamic and flexible data structure (at list in dynamic languages such as python). For instance you can have multiple objects of different types (e.g have 2 strings, 3 ints, one socket, etc)
The quotes around the words in the list denote that they are objects of type string.
When you do a print op (or print(op) for that matter in python 3+) you are essentially asking python to show you a printable representation of that specific list object and its contents. Hence the quotes, commas, brackets, etc.
In python you have a very easy for each loop, usable to iterate through the contents of iterable objects, such as a list. Just do this:
for greeting in op:
print greeting

Print it line by line
for word in op:
print word
This has the advantage that if op happens to be massively long, then you don't have to create a new temporary string purely for printing purposes.

You can also get some nicer print behavior by using the Pretty Print library pprint
Pretty print will automatically wrap printed contents when you go over a certain threshold, so if you only have a few short items, it'll display inline:
from pprint import pprint
xs = ['Hello', 'Morning', 'Evening']
pprint(xs)
# ['Hello', 'Morning', 'Evening']
But if you have a lot of content, it'll auto-wrap:
from pprint import pprint
xs = ['Hello', 'Good Morning', 'Good Evening', 'Good Night', 'Bye', 'Aloha', 'So Long']
pprint(xs)
# ['Hello',
# 'Good Morning',
# 'Good Evening',
# 'Good Night',
# 'Bye',
# 'Aloha',
# 'So Long']
You can also specify the column width with the width= param.
See Also: How to print a list in Python "nicely"

Related

What is the most efficient way to grab and store part of a given string between keywords with python?

I have an array of keywords:
keyword_list = ['word1', 'anotherWord', 'wordup', 'word to your papa']
I have a string of text:
string_of_text = 'So this is a string of text. I want to talk about anotherWord...and then I'm going to say something I've been meaning to say "wordup". But I also wanted to say the following: word to your papa. And lastly I wanted to talk about word1...'
I want to return the following:
{'list_word': 'word1', 'string_of_text_after': '...'}, {'list_word': 'anotherWord', 'string_of_text_after': '...and then I'm going to say something I've been meaning to say "'}, {'list_word': 'wordup', 'string_of_text_after': '". But I also wanted to say the following: '}, {list_word: 'word to your papa', 'string_of_text_after':'. And lastly I wanted to talk about '}
As you can see it is a list of dictionaries with the list word and then the text that comes after the list word item but only until the next list word is detected is when it would discontinue.
What would be the most efficient way to do this in python (python 3 or later, 2 is also ok if there are any issues with deprecated methods).
you could try something like this:
keyword_list = ['word1', 'anotherWord', 'wordup', 'word to your papa']
string_of_text = """So this is a string of text. I want to talk about anotherWord...\
and then I'm going to say something I've been meaning to say "wordup".\
But I also wanted to say the following: word to your papa.\
And lastly I wanted to talk about word1..."""
def t(k, t):
ls = len(t)
tmp = {i:len(i) for i in k}
return [{"list_word":i,"string_of_text_after":t[t.find(i)+tmp[i]:]} for i in tmp if t.find(i)>0]
from pprint import pprint
pprint(t(keyword_list,string_of_text))
Result:
[{'list_word': 'wordup',
'string_of_text_after': '". But I also wanted to say the following: word to your papa. And lastly I wanted to talk about word1...'},
{'list_word': 'word1', 'string_of_text_after': '...'},
{'list_word': 'anotherWord',
'string_of_text_after': '... and then I\'m going to say something I\'ve been meaning to say "wordup". But I also wanted to say the following: word to your papa. And lastly I wanted to talk about word1...'},
{'list_word': 'word to your papa',
'string_of_text_after': '. And lastly I wanted to talk about word1...'}]
ATTENTION
This code has several implications :
the keyword_list has to be of unique elements ...
the call t.find(i) is doubled
the function returns a list, which must be saved in your memory, this could be fixed if you chose to return a generator like this :
return ({"list_word":i,"string_of_text_after":t[t.find(i)+tmp[i]:]} for i in tmp if t.find(i)>0) and to call it where und when needed.
Good luck ! :)

How to shuffle a list by omitting certain characters in Python

I am a middle school student studying Python. Is there a way to omit certain characters from the list and mix them?
Input list
['Hello', 'Middle school student', 'I am']
Expected output
['Middle school student', 'Hello', 'I am']
If you specify is, everything except for is mixed.
Here is a simple shuffle that is effective and efficient. Basically, you randomly swap each element with another element.
import random
def shuffle(lst):
for i in range(len(lst)):
j = random.randrange(len(lst))
lst[i],lst[j] = lst[j],lst[i]

How to "properly" display a list in python without brackets

currently I am using
for s in list:
print(*s)
but it displays the list output as
['this is']
['the output']
But I would like the output to be displayed as
this is
the output
There should be a simple solution but i am still yet to come across one.
l = [['this is'], ['the output']]
for sub_list in l:
print(sub_list[0])
list_string = ', '.join(list_name)
print (list_string) #without brackets
Join using the newline character:
print("\n".join(your_list))
Please note that list is a Python type and shouldn't be used as a variable name.

Regex to find Capital Words in a Series while allowing and,to etc. to be between them

So say I have a string such as:
Hello There what have You Been Doing.
I am Feeling Pretty Good and I Want to Keep Smiling.
I'm looking for the result:
['Hello There', 'You Been Doing', 'I am Feeling Pretty Good and I Want to Keep Smiling']
After a long time of head scratching which later evolved into head slamming, I turned to the internet for my answers. So far, I've managed to find the following:
r"([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)"
The above works but it clearly does not allow for 'and', 'to', 'for', 'am' (these are the only three I'm looking for) to be in the middle of the words and I can not figure out how to add that in there. I'm assuming I have to use the Pipe to do that, but where exactly do I put that group in?
I've also tried the answers over here, but they didn't end up working for me either.
If you are able to enumerate the words you're ok with being uncapitalized in the middle of a capitalized sentence, I would use an alternation to represent them :
\b(?:and|or|but|to|am)\b
And use that alternation to match a sequence of capitalized words and accepted uncapitalized words, which must start with a capitalized word :
[A-Z][a-z]*(?:\s(?:[A-Z][a-z]*|(?:and|or|but|to|am)\b))*
If you are ok with any word of three letters or less (including words like 'owl' or 'try', but not words like 'what') being uncapitalized, you can use the following :
[A-Z][a-z]*(?:\s(?:[A-Z][a-z]*|[a-z]{1,3}\b))*
I guess below works too with itertools.groupby
from itertools import groupby
s = 'Hello There what have You Been Doing. I am Feeling Pretty Good and I Want to Keep Smiling.'
[ ' '.join( list(g) ) for k, g in groupby(s.split(), lambda x: x[0].islower() and x not in ['and','to'] ) if not k ]
Output:
['Hello There',
'You Been Doing. I',
'Feeling Pretty Good and I Want to Keep Smiling.']

How to break a sentence in python depending on fullstop '.'?

I writing a script in python in which I have the following string:
a = "write This is mango. write This is orange."
I want to break this string into sentences and then add each sentence as an item of a list so it becomes:
list = ['write This is mango.', 'write This is orange.']
I have tried using TextBlob but it is not reading it correctly.(Reads the whole string as one sentence).
Is there a simple way of doing it?
One approach is re.split with positive lookbehind assertion:
>>> import re
>>> a = "write This is mango. write This is orange."
>>> re.split(r'(?<=\w\.)\s', a)
['write This is mango.', 'write This is orange.']
If you want to split on more than one separator, say . and ,, then use a character set in the assertion:
>>> a = "write This is mango. write This is orange. This is guava, and not pear."
>>> re.split(r'(?<=\w[,\.])\s', a)
['write This is mango.', 'write This is orange.', 'This is guava,', 'and not pear.']
On a side note, you should not use list as the name of a variable as this will shadow the builtin list.
This should work. Check out the .split() function here: http://www.tutorialspoint.com/python/string_split.htm
a = "write This is mango. write This is orange."
print a.split('.', 1)
you should look in to the NLTK for python.
Here's a sample from NLTK.org
>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
>>> tagged = nltk.pos_tag(tokens)
>>> tagged[0:6]
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'),
('Thursday', 'NNP'), ('morning', 'NN')]
for your case you can do
import nltk
a = "write This is mango. write This is orange."
tokens = nltk.word_tokenize(a)
You know about string.split? It can take a multicharacter split criterion:
>>> "wer. wef. rgo.".split(". ")
['wer', 'wef', 'rgo.']
But it's not very flexible about things like amount of white space. If you can't control how many spaces come after the full stop, I recommend regular expressions ("import re"). For that matter, you could just split on "." and clean up the white space at the front of each sentence and the empty list that you will get after the last ".".
<code>a.split()</code>
a.split() seems like a simple way of doing it, but you will eventually run into problems.
For example suppose you have
a = 'What is the price of the orange? \
It costs $1.39. \
Thank you! \
See you soon Mr. Meowgi.'
The a.split('.') would return:
a[0] = 'What is the price of the orange? It costs $1'
a[1] = '39'
a[2] = 'Thank you! See you soon Mr'
a[3] = 'Meowgi'
I am also not factoring in
code snippets
e.g. 'The problem occured when I ran ./sen_split function. "a.str(" did not have a closing bracket.'
Possible Names of Company's
e.g. 'I work for the Node.js company'
etc.
This eventually boils down to English syntax. I would recommend looking into nltk module as Mike Tung pointed out.

Categories

Resources