Given a list with several names as a parameter, I was wondering if there was a way to split the names by first and last and create a 2d list of all first names and all last names. For example given:
lst=(["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"])
the output should be:
[["Leonoardo","Brad","Angelina"],["DiCaprio","Pitt","Jolie"]]
I know that I need to iterate over the given list, and append the first names and last names to a new lists but I'm not quite sure how to go about it.
This is what I've got so far:
fname=[]
lname=[]
for i in nlst:
i.split()
fname.append(i[0])
lname.append(i[1])
return lname,fname
You can use a simple list comprehension:
>>> lst=(["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"])
>>> new = [[item.split(',')[1] for item in lst], [item.split(',')[0] for item in lst]]
>>> new
[['Leonardo', ' Brad', ' Angelina'], ['DiCaprio', 'Pitt', 'Jolie']]
Or you can zip it:
>>> x = [item.split(',') for item in lst]
>>> list(reversed(zip(*x)))
[('Leonardo', ' Brad', ' Angelina'), ('DiCaprio', 'Pitt', 'Jolie')]
This might help you:
list_names = []
list_last =[]
lst=["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"]
for element in lst:
list_names.append(element.split(',')[0])
list_last.append(element.split(',')[1])
Let me know if you need anything else
a function to return the names after splitting.
def split_list(the_list=["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"]):
list_2d = [[],[]]
for full_name in the_list:
first_name, second_name = full_name.split(',')
list_2d[0].append(first_name)
list_2d[1].append(second_name)
return list_2d
print split_list()
According your code, you can try this:
lst=(["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"])
fname=[]
lname=[]
for i in lst:
fname.append(i.split(",")[0])
lname.append(i.split(",")[1])
print [lname,fname]
You can split and transpose:
lst = ["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"]
print([list(ele) for ele in zip(*(ele.split(",") for ele in lst))])
Or use map:
print(list(map(list, zip(*(ele.split(",") for ele in lst)))))
[['DiCaprio', 'Pitt', 'Jolie'], ['Leonardo', ' Brad', ' Angelina']]
If you need the order reversed:
print(list(map(list, zip(*(reversed(ele.split(",")) for ele in lst)))))
[['Leonardo', ' Brad', ' Angelina'], ['DiCaprio', 'Pitt', 'Jolie']]
For python2 you can use izip and the remove the list call on map:
from itertools import izip
print(map(list, izip(*(reversed(ele.split(",")) for ele in lst))))
You can use zip function :
>>> l=["DiCaprio,Leonardo","Pitt, Brad", "Jolie, Angelina"]
>>> zip(*[i.split(',') for i in l])[::-1]
[('Leonardo', ' Brad', ' Angelina'), ('DiCaprio', 'Pitt', 'Jolie')]
If you want the result as list you can convert to list with map function :
>>> map(list,zip(*[i.split(',') for i in l])[::-1])
[['Leonardo', ' Brad', ' Angelina'], ['DiCaprio', 'Pitt', 'Jolie']]
You could use a regex:
>>> re.findall(r'(\w+),\s*(\w+);?', ';'.join(lst))
[('DiCaprio', 'Leonardo'), ('Pitt', 'Brad'), ('Jolie', 'Angelina')]
Then use either zip or map to transpose the list of tuples:
>>> map(None, *re.findall(r'(\w+),\s*(\w+);?', ';'.join(lst)))
[('DiCaprio', 'Pitt', 'Jolie'), ('Leonardo', 'Brad', 'Angelina')]
On Python 3, you can't use map that way. Instead, you would do:
>>> list(map(lambda *x: [e for e in x], *re.findall(r'(\w+),\s*(\w+);?', ';'.join(lst))))
Then reverse the order and make a list of lists rather than list of tuples if you wish:
>>> lot=map(None, *re.findall(r'(\w+),\s*(\w+);?', ';'.join(lst)))
>>> [list(lot[1]), list(lot[0])]
[['Leonardo', 'Brad', 'Angelina'], ['DiCaprio', 'Pitt', 'Jolie']]
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 have a simple list that I am splitting and concatenating. My code uses for loop and if condition and ugly. Can you suggest a better way using list comprehension?
My code
mylist = ['10.10.10.1','10.10.10.2,10.10.10.3,10.10.10.4,10.10.10.5','10.10.10.6']
mylist = [i.split(",") for i in mylist]
list =[]
for x,y in enumerate(mylist):
if len(y) == 1:
list.append(y[0])
else:
for z in y:
list.append(z)
print(list)
I am getting the below result and exactly the way i want
['10.10.10.1','10.10.10.2','10.10.10.3','10.10.10.4','10.10.10.5','10.10.10.6']
You want:
[s for string in mylist for s in string.split(',')]
Note, your original approach wouldn't be so bad if you just simplified. No need for enumerate and no need to check the length, so just:
final_list =[]
for sub in mylist:
for s in sub:
final_list.append(s)
By the way, you shouldn't shadow the built-in list. Use another name
I agree with #juanpa.arrivillaga. However hope we can avoid that second looping since he is checking for empty values returning while splitting
In [7]: s=['10.10.10.1','','10.10.10.2,10.10.10.3,10.10.10.4,10.10.10.5','10.10.10.6']
In [8]: [splitRec for rec in s for splitRec in rec.split(',') if splitRec]
Out[8]:
['10.10.10.1',
'10.10.10.2',
'10.10.10.3',
'10.10.10.4',
'10.10.10.5',
'10.10.10.6']
In [9]: s=['10.10.10.1',',,','10.10.10.2,10.10.10.3,10.10.10.4,10.10.10.5','10.10.10.6']
In [10]: [splitRec for rec in s for splitRec in rec.split(',') if splitRec]Out[10]:
['10.10.10.1',
'10.10.10.2',
'10.10.10.3',
'10.10.10.4',
'10.10.10.5',
'10.10.10.6']
Not a comprehension, but good anyway, I think.
','.join(mylist).split(',')
You can first just split each string on ',':
>>> mylist = ['10.10.10.1','10.10.10.2,10.10.10.3,10.10.10.4,10.10.10.5','10.10.10.6']
>>> split_str = [x.split(',') for x in mylist]
>>> split_str
[['10.10.10.1'], ['10.10.10.2', '10.10.10.3', '10.10.10.4', '10.10.10.5'], ['10.10.10.6']]
Then if you want to flatten it, you can use itertools.chain.from_iterable:
>>> from itertools import chain
>>> list(chain.from_iterable(split_str))
['10.10.10.1', '10.10.10.2', '10.10.10.3', '10.10.10.4', '10.10.10.5', '10.10.10.6']
Im looking for the best way to take a list of stirngs, generate a new list with each item from the previous list concatenated with a specific string.
Example sudo code
list1 = ['Item1','Item2','Item3','Item4']
string = '-example'
NewList = ['Item1-example','Item2-example','Item3-example','Item4-example']
Attempt
NewList = (string.join(list1))
#This of course makes one big string
If you want to create a list, a list comprehension is usually the thing to do.
new_list = ["{}{}".format(item, string) for item in list1]
Use string concatenation in a list comprehension:
>>> list1 = ['Item1', 'Item2', 'Item3', 'Item4']
>>> string = '-example'
>>> [x + string for x in list1]
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
An alternative to list comprehension is using map():
>>> map(lambda x: x+string,list1)
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
Note, list(map(lambda x: x+string,list1)) in Python3.
concate list item and string
>>>list= ['Item1', 'Item2', 'Item3', 'Item4']
>>>newList=[ i+'-example' for i in list]
>>>newList
['Item1-example', 'Item2-example', 'Item3-example', 'Item4-example']
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....
I have a list:
my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
How can I delete the \t and everything after to get this result:
['element1', 'element2', 'element3']
Something like:
>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
myList = [i.split('\t')[0] for i in myList]
Try iterating through each element of the list, then splitting it at the tab character and adding it to a new list.
for i in list:
newList.append(i.split('\t')[0])
Do not use list as variable name.
You can take a look at the following code too:
clist = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', 'element5']
clist = [x[:x.index('\t')] if '\t' in x else x for x in clist]
Or in-place editing:
for i,x in enumerate(clist):
if '\t' in x:
clist[i] = x[:x.index('\t')]
Solution with map and lambda expression:
my_list = list(map(lambda x: x.split('\t')[0], my_list))
I had to split a list for feature extraction in two parts lt,lc:
ltexts = ((df4.ix[0:,[3,7]]).values).tolist()
random.shuffle(ltexts)
featsets = [(act_features((lt)),lc)
for lc, lt in ltexts]
def act_features(atext):
features = {}
for word in nltk.word_tokenize(atext):
features['cont({})'.format(word.lower())]=True
return features