Suppose, I have a list like the following:
names= ["my name xyz","your name is abc","her name is cfg zyx"]
I want to split them like following:
my_array[0]= ["my","name","xyz"]
my_arrray[1]= ["your","name","is","abc"]
my_array[2]= ["her","name","is","cfg","zyx"]
So that I can choose the words individually like following:
my_array[0][0]= my
my_array[0][1]= name
my_array[1][2]= is
my_array[0][2]= xyz
The python code i tried is following:
names_abc= names.splitlines(True)
#print(names_abc)
i=0
while i < len(names_abc):
my_array= names_abc[i].split()
print(my_array[0])
i=i+1
But this code doesn't do what I expect. It splits the list but cannot choose the words individually. For example, the output of the above code is following:
my
your
her
which is definitely wrong. I will be grateful to get a help from the python experts. Thanks in advance.
You could define my_array using a list comprehension:
my_array = [phrase.split() for phrase in names]
which is equivalent to
my_array = list()
for phrase in names:
my_array.append(phrase.split())
Demo:
In [21]: names= ["my name xyz","your name is abc","her name is cfg zyx"]
In [22]: my_array = [phrase.split() for phrase in names]
In [23]: my_array
Out[23]:
[['my', 'name', 'xyz'],
['your', 'name', 'is', 'abc'],
['her', 'name', 'is', 'cfg', 'zyx']]
In [24]: my_array[0][1]
Out[24]: 'name'
One problem with defining my_array this way:
while i < len(names_abc):
my_array= names_abc[i].split()
is that my_array is assigned to a new list each time through the while-loop. So my_array does not become a list of lists. Instead, after the while-loop, it only retains its last value.
Instead of printing my_array[0], try print(my_array) itself. It might become more clear why you're getting the result you were getting.
You could split() every element in your list, which by default will split on spaces.
>>> names= ["my name xyz","your name is abc","her name is cfg zyx"]
>>> [i.split() for i in names]
[['my', 'name', 'xyz'], ['your', 'name', 'is', 'abc'], ['her', 'name', 'is', 'cfg', 'zyx']]
I would create a new list and fill it with lists containing temporaray strings build from the characters of your starting list.
The temporary string is ended by the space, when a string is complete it is added to a temoprary list (list_tmp).
After all items are processed as strings and added to the temporary list, we append that list the final list:
a=["my name xyz","your name is abc","her name is cfg zyx"]
l=[]
for i in enumerate(a):
tmp_str = ''
list_tmp=[]
for j in i[1]:
if " " in j:
list_tmp.append(tmp_str)
tmp_str=''
else:
tmp_str+=j
else:
list_tmp.append(tmp_str)
l.append(list_tmp)
print l
print l[0][1]
Related
I used python to import txt data as list. As a result, there are double quotes on the items in the list which i do not want. I want to remove them but I'm currently having some difficulties. I would be glad if someone can kindly help me with this issue.
The list in python is of this nature:
lst = ["'BEN','JIM'", "'GIN','ANN'"]
I want to have the double quotes removed so I can get this list:
lst = ['BEN','JIM', 'GIN','ANN']
I tried this lines of code but to no avail:
lst = ["'BEN','JIM'", "'GIN','ANN'"]
lst = [x.strip("'") for x in lst]
lst
and the result is not what im expecting:
["BEN','JIM", "GIN','ANN"]
Again, I would be grateful for your help.
Use the replace() string function to get rid of the single quotes, and then split on commas.
lst = ["'BEN','JIM'", "'GIN','ANN'"]
newlst = []
for pair in lst:
for name in pair.replace("'", "").split(","):
newlst.append(name)
You have confused the display representation of an item as being equivalent to its value.
Look at what you have: a list of two elements:
["'BEN','JIM'",
"'GIN','ANN'"]
You want to obtain a list of four elements:
['BEN',
'JIM',
'GIN',
'ANN']
You cannot do this by simple character manipulation: the operations you tried do not change the quantity of elements.
Instead, you have to process the elements you have, splitting 2-for-1.
I'll keep the Python technology low ...
new_lst = []
for two_name in lst:
name_pair = two_name.split(',')
new_lst.extend(name_pair)
Output:
["'BEN'", "'JIM'", "'GIN'", "'ANN'"]
Now you can use your previous technique to remove the single-quotes, leaving you with the four, 3-letter names you wanted.
Does that solve your problem?
The data looks similar to a CSV file where the individual cells are quoted. If it looks like a duck... use a reader to strip them for you. This is a nested list comprehension to first build rows from the the list and then to flatten them to a single list.
>>> import csv
>>> lst = ["'BEN','JIM'", "'GIN','ANN'"]
>>> [cell for row in csv.reader(lst, quotechar="'") for cell in row]
['BEN', 'JIM', 'GIN', 'ANN']
lst = ["'BEN','JIM'", "'GIN','ANN'"]
lst = "".join(lst)
print(lst)
Output:
'BEN','JIM''GIN','ANN'
You can do thi with list comprehension on two lines:
input:
lst = ["'BEN','JIM'", "'GIN','ANN'"]
lst1 = [x.replace("'", "").split(',') for x in lst]
lst2 = [z for y in lst1 for z in y]
or on one line
lst2 = [z for y in [x.replace("'", "").split(',') for x in lst] for z in y]
output:
['BEN', 'JIM', 'GIN', 'ANN']
I am trying to write some code which adds an element in one list to another list and then removes it from the first list. It also should not add duplicates to the new list which is where the if statement comes in.
However, when adding to the 'individuals' list and removing from the 'sentence_list' list it misses out certain words such as 'not' and 'for'. This is also not random and the same words are missed each time. Any help?
sentence = "I am a yellow fish"
sentence_list = sentence.lower().split()
individuals = []
for i in sentence_list:
if i in individuals:
print ("yes")
sentence_list.remove(i)
else:
individuals.append(i)
sentence_list.remove(i)
print ("individuals", individuals)
print ("sentence_list", sentence_list)
The issue is that you are removing items from the list you are looping through. You can fix this just by making a copy of the list and looping through it instead, like this:
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
sentence_list = sentence.lower().split()
individuals = []
#We slice with [:] to make a copy of the list
orig_list = sentence_list[:]
for i in orig_list:
if i in individuals:
print ("yes")
sentence_list.remove(i)
else:
individuals.append(i)
sentence_list.remove(i)
print ("individuals", individuals)
print ("sentence_list", sentence_list)
The lists are now what was expected:
print(individuals)
print(sentence_list)
['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you']
[]
In general you should not add or remove elements to a list as you iterate over it. Given that you are removing every single element of the list, just remove the lines with sentence_list.remove(i).
If you actually need to remove just some elements from the list you're iterating I'd either: make a new empty list and add the elements you want to keep to that, or keep a track of which indices in the list you want to remove as you iterate and then remove after the loop.
For the first solution,
oldList = [1, 2, 3, 4]
newList = []
for i in oldList:
shouldRemove = i % 2
if not shouldRemove:
newList.append(i)
For the second,
oldList = [1, 2, 3, 4]
indicesToKeep = []
for i, e in enumerate(oldList):
shouldRemove = e % 2
if not shouldRemove:
indicesToKeep.append(i)
newList = [e for i, e in enumerate(oldList) if i in indicesToKeep]
I want to loop through a list of strings...
eg:
list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty']
I want to loop through the list items and search for the location of 'Hello!' within the string in each case an record its location.
Any ideas?
Use a list comprehension to collect the indexes of your wanted string:
>>> list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty', 'My Name is Josh']
>>> [s.find('Hello!') for s in list_of_strings]
[0, 0, 0, -1]
Note how the negative 1 shows that the final string does not contain the value you're looking for.
Use the string.find method.
>>> phrase = 'Hello, my name is Syko'
>>> phrase.find('Hello')
0
You could try list comprehensions
[(s, s.lower().index('hello')) for s in list_of_strings if s.lower() == 'hello']
>>> list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty']
>>> indices = [position for position, phrase in enumerate(list_of_strings) if 'Hello!' in phrase]
>>> print indices
[0, 1, 2]
Using a comprehension list, loop over the list and check if the string has the Hello! string inside, if yes, append the position to the matches list.
Note: The enumerate build in brings you the index for each element in the list.
in case you are searching for multiple items in a list say Hello and name
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
find(list_of_strings,['Hello','name'])
output is a list with the positions of the elements you are searching for in the order that you listed them
How can I take a string and insert it into a list in the place of another string that is already there (so I don't get out of range error)?
example:
l = ["rock", "sand", "dirt"]
l.remove[1]
l.insert(1, "grass")
is there an easier way to do it than this? What do I do if I have an empty list and the order matters?
All you need is:
>>> l = ["rock", "sand", "dirt"]
>>> l[1] = "grass"
>>> l
['rock', 'grass', 'dirt']
>>>
Lists support direct replacement in Python via list[index] = value.
you can also directly replace the element: l[1] = 'grass'
Also if you are unsure of the index of the item you would like to replace you just use:
Say the item you want to replace is "dirt", you'd just go:
rightIndex = l.index("dirt")
l[rightIndex] = "grass
This will replace "dirt" with "grass" if you are unsure about the index of "grass" in list "l".
If you are looking at an arbitrary list, you might not know if the item is in the list or what index it is. You might first check that the item is in the list, then look for the index, so that you can replace it. The following example would do this for the all elements in a list that matches what you want to replace:
def replace_list_item(old, new, l):
'''
Given a list with an old and new element, replace all elements
that match the old element with the new element, and return the list.
e.g. replace_list_item('foo', 'bar', ['foo', 'baz', 'foo'])
=> ['bar', 'baz', 'bar']
'''
while old in l: # check for old item, otherwise index gives a value error
index = l.index(old)
l[index] = new
return l
then:
l = ["rock", "sand", "dirt", "sand"]
replace_list_item('sand', 'grass', l)
returns:
['rock', 'grass', 'dirt', 'grass']
I have a list:
l = ['abc,def,ghi,jkl']
Now I want the result should be in the format abc, def, ghi,jkl as a separate element using python.
How do I do that?
I tried doing something else, but I am getting the result as 'a',b','c', etc.
Thanks in advance.
Use split function:
res = 'abc,def,ghi,jkl'.split(',')
Or:
l = ['abc,def,ghi,jkl']
output = []
for row in l:
output.extend(row.split(','))
# output will contain all the separate items for all items of l if more than one
print output
Not exactly sure what result you want:
>>> l = ['abc,def,ghi,jkl']
>>> l[0]
'abc,def,ghi,jkl'
>>> l[0].split(',')
['abc', 'def', 'ghi', 'jkl']
>>> ', '.join(l[0].split(','))
'abc, def, ghi, jkl'
A oneliner:
', '.join([ e for s in l for e in s.split(',') ])
'abc, def, ghi, jkl'
What you have as your example:
l = ['abc,def,ghi,jkl']
Is indeed a list, but a list with only one single item in it. This item is 'abc,def,ghi,jkl' The reason you only have one item in your list is because you created a string by starting with this character: ' and then everything that comes after that until you close the string with another ' is still the same string.
If you wanted to have four strings in your list, you should have written:
l = ['abc', 'def', 'ghi', 'jkl']
This gives you a list, here the first item is 'abc' and then the comma (,) tells the list constructor to treat the next object as another item, and so you get 'def' and so on....