Split strings in a list of lists - python

I currently have a list of lists:
[['Hi my name is'],['What are you doing today'],['Would love some help']]
And I would like to split the strings in the lists, while remaining in their current location. For example
[['Hi','my','name','is']...]..
How can I do this?
Also, if I would like to use a specific of the lists after searching for it, say I search for "Doing", and then want to append something to that specific list.. how would I go about doing that?

You can use a list comprehension to create new list of lists with all the sentences split:
[lst[0].split() for lst in list_of_lists]
Now you can loop through this and find the list matching a condition:
for sublist in list_of_lists:
if 'doing' in sublist:
sublist.append('something')
or searching case insensitively, use any() and a generator expression; this will the minimum number of words to find a match:
for sublist in list_of_lists:
if any(w.lower() == 'doing' for w in sublist):
sublist.append('something')

list1 = [['Hi my name is'],['What are you doing today'],['Would love some help']]
use
[i[0].split() for i in list1]
then you will get the output like
[['Hi', 'my', 'name', 'is'], ['What', 'are', 'you', 'doing', 'today'], ['Would', 'love', 'some', 'help']]

l = [['Hi my name is'],['What are you doing today'],['Would love some help']]
for x in l:
l[l.index(x)] = x[0].split(' ')
print l
Or simply:
l = [x[0].split(' ') for x in l]
Output
[['Hi', 'my', 'name', 'is'], ['What', 'are', 'you', 'doing', 'today'], ['Would', 'love', 'some', 'help']]

Related

Split The Second String of Every Element in a List into Multiple Strings

I am very very new to python so I'm still figuring out the basics.
I have a nested list with each element containing two strings like so:
mylist = [['Wowza', 'Here is a string'],['omg', 'yet another string']]
I would like to iterate through each element in mylist, and split the second string into multiple strings so it looks like:
mylist = [['wowza', 'Here', 'is', 'a', 'string'],['omg', 'yet', 'another', 'string']]
I have tried so many things, such as unzipping and
for elem in mylist:
mylist.append(elem)
NewList = [item[1].split(' ') for item in mylist]
print(NewList)
and even
for elem in mylist:
NewList = ' '.join(elem)
def Convert(string):
li = list(string.split(' '))
return li
print(Convert(NewList))
Which just gives me a variable that contains a bunch of lists
I know I'm way over complicating this, so any advice would be greatly appreciated
You can use list comprehension
mylist = [['Wowza', 'Here is a string'],['omg', 'yet another string']]
req_list = [[i[0]]+ i[1].split() for i in mylist]
# [['Wowza', 'Here', 'is', 'a', 'string'], ['omg', 'yet', 'another', 'string']]
I agree with #DeepakTripathi's list comprehension suggestion (+1) but I would structure it more descriptively:
>>> mylist = [['Wowza', 'Here is a string'], ['omg', 'yet another string']]
>>> newList = [[tag, *words.split()] for (tag, words) in mylist]
>>> print(newList)
[['Wowza', 'Here', 'is', 'a', 'string'], ['omg', 'yet', 'another', 'string']]
>>>
You can use the + operator on lists to combine them:
a = ['hi', 'multiple words here']
b = []
for i in a:
b += i.split()

How can I merge a list with a nested list?

I have two lists. The first one is a simple list with one entry:
l = [hello]
The second one is a nested list, with several thousand entries
i = [[world, hi],
[world, hi],
[world, hi],
[world, hi]]
Now I want to insert the entry from list l into the nested lists of i like this:
i = [[hello, world, hi],
[hello, world, hi],
[hello, world, hi],
[hello, world, hi]]
How can I do this?
You can use list comprehension to achieve that
i = [l+x for x in i]
Just use the insert method on each sublist in i.
for sublist in i:
sublist.insert(0, l[0])
If you don't want to modify i in place, and would prefer to build a new list, then
i = [[l[0], *sublist] for sublist in i]
simple: [[*l,*el] for el in i]
Output:
[['hello', 'world', 'hi'],
['hello', 'world', 'hi'],
['hello', 'world', 'hi'],
['hello', 'world', ' hi']]
I guess the benefits is that no matter how many elements you have in l, they will all be put in front of the stuff in i
Just add the element to the list you want
l = ["hello"]
i = [["world", "hi"],
["world", "hi"],
["world", "hi"],
["world", "hi"]]
x = []
for y in i:
x.append(l+y)
x
output:
[['hello', 'world', 'hi'],
['hello', 'world', 'hi'],
['hello', 'world', 'hi'],
['hello', 'world', 'hi']]
I believe something like this should do the trick:
l = ['hello']
i = [['world', 'hi'],
['world', 'hi'],
['world', 'hi'],
['world', 'hi']]
for j in i:
for k in l:
j = j.append(k)
I'm just iterating through your list i, nesting another for loop to iterate through the simpler list l (to account for the case whereby it has multiple elements), and simply appending each entry in l to the current list in i.

Splitting a single index list into multiple list indexes?

I have a list:
lst = ['words in a list']
and I was hoping to split each one of these words in the string into their own separate indexes. So for example, it would look something like this:
lst = ['words','in','a','list']
I'm wondering if this is possible? I thought initially this would be just a simple lst.split() with a loop, but it seems like this is will throw an error.
Thanks for the help!
Use this:
print(lst[0].split())
If the list has more elements:
print([x for i in lst for x in i.split()])
Split only works for a string type. So you need to index the list item first and then split.
lst = lst[0].split()
Use this when you have a list of string or single string inside a list
lst = ['this is string1', 'this is string2', 'this is string3']
result =' '.join(lst).split()
print(result)
# output : ['this', 'is', 'string1', 'this', 'is', 'string2', 'this', 'is', 'string3']

Python: Split list based on first character of word

Im kind of stuck on an issue and Ive gone round and round with it until ive confused myself.
What I am trying to do is take a list of words:
['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']
Then sort them under and alphabetical order:
A
About
Absolutely
After
B
Bedlam
Behind
etc...
Is there and easy way to do this?
Use itertools.groupby() to group your input by a specific key, such as the first letter:
from itertools import groupby
from operator import itemgetter
for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
print letter
for word in words:
print word
print
If your list is already sorted, you can omit the sorted() call. The itemgetter(0) callable will return the first letter of each word (the character at index 0), and groupby() will then yield that key plus an iterable that consists only of those items for which the key remains the same. In this case that means looping over words gives you all items that start with the same character.
Demo:
>>> somelist = ['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']
>>> from itertools import groupby
>>> from operator import itemgetter
>>>
>>> for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
... print letter
... for word in words:
... print word
... print
...
A
About
Absolutely
After
Aint
Alabama
AlabamaBill
All
Also
Amos
And
Anyhow
Are
As
At
Aunt
Aw
B
Bedlam
Behind
Besides
Biblical
Bill
Billgone
Instead of using any library imports, or anything fancy.
Here is the logic:
def splitLst(x):
dictionary = dict()
for word in x:
f = word[0]
if f in dictionary.keys():
dictionary[f].append(word)
else:
dictionary[f] = [word]
return dictionary
splitLst(['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone'])
def split(n):
n2 = []
for i in n:
if i[0] not in n2:
n2.append(i[0])
n2.sort()
for j in n:
z = j[0]
z1 = n2.index(z)
n2.insert(z1+1, j)
return n2
word_list = ['be','have','do','say','get','make','go','know','take','see','come','think',
'look','want','give','use','find','tell','ask','work','seem','feel','leave','call']
print(split(word_list))

Search a list using a string

I have a List of Lists:
A = [['andy', 'dear', 'boy', 'tobe', 'todo'],
['where', 'what', 'when', 'how'],
['korea', 'japan', 'china', 'usa'],
['tweet', 'where', 'why', 'how']]
I have three questions to be exact:
How do I retrieve a sub-list from this list using a particular element as a keyword?
For instance, I want to retrieve all the lists having element 'why' in them? What is
the best possible way of doing so?
How do I retrieve a sub-list from this list using a part of a particular element as a keyword?
For instance, I want to retrieve all the lists having elements containing 'wh' as beginning
characters of any of the elements?
How do I get the position or index of resulting sub-lists from any of these two searching methods?
I am familiar with the concept of retrieving all the elements from a list with matching with a particular keyword, but its confusing when it comes to retrieve all the lists matching a particular keyword...
Any guesses? Thanks in advance.
Simple and straight
elem = 'why'
index_li = []
for idx, item in enumerate(A):
for word in item:
if word.startswith(elem):
index_li.append(idx)
break
print index_li
Example
>>> elem = 'wh'
... index_li = []
... for idx, item in enumerate(A):
... for word in item:
... if word.startswith(elem):
... print word, item
... index_li.append(idx)
... break
... print index_li
where ['where', 'what', 'when', 'how']
where ['tweet', 'where', 'why', 'how']
[1, 3]
For all the answers combined:
mylist = [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']]
for num, sublist in enumerate(mylist):
if 'why' in sublist:
print sublist
for ele in sublist:
if ele.startswith('wh'):
print ele, num
I love list comprehensions and functional programming, so here's that approach
A
sub_list_a = [ el for el in A if 'why' in el ]
B
sub_list_b = [ el for el in A if any( ['wh' in s for s in el] ) ]
A little more difficult to read, but concise and sensible.
C
Then to get the indices of where each of the sub_lists were found
location_a = [ ii for ii, el in enumerate(A) if el in sub_list_a ]
Simply replace b for a to get the locations for part b.
For first:
>>> for l in A:
... if 'why' in l:
... print l
...
['tweet', 'where', 'why', 'how']
For the second:(wy any where)
>>> for l in A:
... for i in l:
... if 'wh' in i:
... print l
... break
...
['where', 'what', 'when', 'how']
['tweet', 'where', 'why', 'how']
to test at beginning try this: (using startswith() from #Harido)
>>> for l in A:
... for i in l:
... if i.startswith('wh'):
... print l
... break
...
['where', 'what', 'when', 'how']
['tweet', 'where', 'why', 'how']
For third:
To find index, you can use A.index(l) method after print stamens for example:
>>> for l in A:
... for i in l:
... if 'wh' in i:
... print l
... print A.index(l)
... break
...
['where', 'what', 'when', 'how']
1
['tweet', 'where', 'why', 'how']
3
But remember I am not good in Python. Some one can give you better ways. (I am writing C like code that is poor) I would like to share this link: Guido van Rossum
Edit:
thanks to #Jaime to suggesting me for k, l in enumerate(A):
>>> for k, l in enumerate(A):
... for i in l:
... if 'wh' in i:
... print l
... print "index =", k
... break
...
['where', 'what', 'when', 'how']
index = 1
['tweet', 'where', 'why', 'how']
index = 3
You'd do this the same way you'd do it for a single list, except you'll require an extra loop:
for inner_list in A:
for element in inner_list:
if # <some condition>
# <do something>
For positions, adding an enumerate in the specific loop you want the position will help, or simply using something like find for each inner list will also do the job.
>>> A = [['andy', 'dear', 'boy', 'tobe', 'todo'], ['where', 'what', 'when', 'how'], ['korea', 'japan', 'china', 'usa'], ['tweet', 'where', 'why', 'how']]
>>> for i, sublist in enumerate(A):
if 'why' in sublist:
print i, sublist
3 ['tweet', 'where', 'why', 'how']
>>> for i, sublist in enumerate(A):
if any(word.startswith('wh') for word in sublist):
print i, sublist
1 ['where', 'what', 'when', 'how']
3 ['tweet', 'where', 'why', 'how']
a. suppose you have a list A and the word your are looking for is x.
def howmuch(word)
lis = []
for x in A:
//point A
if x.count(word) > 0:
//point A
lis.append(x)
return lis
basically lis is a list that holds all the lists that has that word. you iterate through the original list. each element x is a list. x.count(word) tells you how many times that word is in this list. 0 means it never appeared in the list. so if it is greater than 0, it must be in the list. if so, we added to our lis variable. then we return it.
b. it is the same thing as problem a except at point A, after you get the list, do another for loop for each word in there. for python, there is a find function for strings to see if a substring exists in there if it does exists, then append it to the list element:
http://docs.python.org/2/library/string.html
c. for problem a, after you check to see that count is greater than 0, then that means the word exist in the list. do a list.index(word) and it will return the index. for problem b, once you find a string with the proper substring, do a list.index(word).
all in all, you should look at the python site, it tells you a lot of the functions you can use.
You can use this simple approach:
>>> key='wh'
>>> res=[(index, sublist) for index, sublist in enumerate(A) for value in sublist if value.startswith(key)]
>>> dict(res)
{1: ['where', 'what', 'when', 'how'], 3: ['tweet', 'where', 'why', 'how']}
enjoy!

Categories