Switch Lastname, Firstname to Firstname Lastname inside List - python

I have two lists of sports players. One is structured simply:
['Lastname, Firstname', 'Lastname2, Firstname2'..]
The second is a list of lists structured:
[['Firstname Lastname', 'Team', 'Position', 'Ranking']...]
I ultimately want to search the contents of the second list and pull the info if there is a matching name from the first list.
I need to swap 'Lastname, Firstname' to 'Firstname Lastname' to match list 2's formatting for simplification.
Any help would be great. Thanks!

You can swap the order in the list of names with:
[" ".join(n.split(", ")[::-1]) for n in namelist]
An explanation: this is a list comprehension that does something to each item. Here are a few intermediate versions and what they would return:
namelist = ["Robinson, David", "Roberts, Tim"]
# split each item into a list, around the commas:
[n.split(", ") for n in namelist]
# [['Robinson', 'David'], ['Roberts', 'Tim']]
# reverse the split up list:
[n.split(", ")[::-1] for n in namelist]
# [['David', 'Robinson'], ['Tim', 'Roberts']]
# join it back together with a space:
[" ".join(n.split(", ")[::-1]) for n in namelist]
# ['David Robinson', 'Tim Roberts']

Related

Find the unique values in a python string variable and separate with a comma, for a SQL criteria

Morning,
I need to retrieve the unique elements in a string which will be used as part of a SQL criteria.
sales_code_string_RFQ = 'AAA','BBB','CCC','DDD','AAA','EEE',.......
Goal is to get a unique list of sales codes from sales_code_string_RFQ separated by a , The following works but seems to be inefficient. Any suggestions to improve?
# Covert string to list
sales_code_string_RFQ = sales_code_string_RFQ.split(',')
print('2. Split sales_code_string_RFQ:')
print(sales_code_string_RFQ)
["'AAA'", "'BBB'", "'CCC'", "'DDD'", "'AAA'", "'EEE'".....]
print('3. Get unique values and convert to list:')
sales_code_string_RFQ = list(set(sales_code_string_RFQ))
print(sales_code_string_RFQ)
["'AAA'", "'BBB'", "'CCC'", "'DDD'", "'EEE'".....]
print('4.Covert list to string:')
sales_code_string_RFQ = ",".join(sales_code_string_RFQ)
print(sales_code_string_RFQ)
print("", end='\n')
Final correct output:
'AAA', 'BBB', 'CCC', 'DDD', 'EEE' ,....
Assuming you have a list of sales codes like this:
sales_codes = ["'AAA'", "'BBB'", "'CCC'", "'DDD'", "'AAA'", "'EEE'"]
The built-in Python class set will sort the list for you and take only unique items from it:
unique_codes = set(sales_codes)
Then you can join them together to form your query criterion:
criterion = ', '.join(set(sales_codes))

How can I use regex to compare a list of words with another list of words and print the matches?

How can I use regex to compare a list of words with another list of words and print the matches?
I have a list of keywords:
test_keywords=["buy house in kings landing",
"house price kings landing",
"cost of living kings landing",
"wildfire pricing"]
I have another list with keywords:
i_transactional = ["buy", "Buy", "price", "Price", "cost", "Cost", "pricing", "Pricing", "cheap", "Cheap"]
I would like to use the regex module to find the words in i_transactional in test_keywords.
I have this working in a construction in which I manually enter the keywords from i_transactional in a re.compile:
# create a regex
r_t = re.compile (r'buy|Buy|price|Price|cost|Cost|pricing|Pricing|cheap|Cheap')
# use list to find the regex words in the test_keywords list
o_t = list(filter(r_t.findall, test_keywords))
# print the results of the regex lookup
print("Transactional (", len(o_t), ") = ", o_t)
Output:
Transactional ( 5 ) = ['buy house in kings landing', 'house price kings landing', 'cost of living kings landing', 'wildfire pricing']
But instead of manually entering the words in the regex, I would like that r_t uses the i_transactional list.
I've tried it in the construction below but this doesn't work.
# create a regex
r_tl = re.compile(r"\<i_transactional>") #transactional
# use list to find the regex words in the test_keywords list
o_tl = list(filter(r_tl.findall, test_keywords))
# print the results of the regex lookup
print("Transactional (", len(o_tl), ") = ", o_tl)
There's something wrong with this code.
Output:
Transactional ( 0 ) = []
How can I use regex to compare a list of words with another list of words and print the matches?
Thank you in advance.
You can use string.join to join the list by | and create a regex.
r_t = re.compile ('|'.join(i_transactional))

Extracting first names from list of full names in Python

I've got a list of full names from a csv file, but I only want to apply my next piece of code to the first names from that list. The list looks like this:
['michelle rodriguez', 'dexter king', 'gurukavari']
But then for a few thousand names. As you can see, some of the names don't contain a last name, and some do. How do I create a new list which contains only the first names from this list?
Use a list-comprehension:
lst = ['michelle rodriguez', 'dexter king', 'gurukavari']
print([x.split()[0] for x in lst])
# ['michelle', 'dexter', 'gurukavari']
You can use map function like:-
a = ['michelle rodriguez', 'dexter king', 'gurukavari']
b= map((lambda x: x.split()[0]), a) # ['michelle', 'dexter', 'gurukavari']
Would advise to use Map function, and to split each of the iterable items by space character, and return only the first of the split items.
Use a split function to separate the first name & last name from the name list and traverse the list till the last element.
Code :
names = ['michelle rodriguez', 'dexter king', 'gurukavari']
firstname = []
for i in range(0,len(names)):
firstname.append(names[i].split()[0])
print(firstname)
Output :
['michelle', 'dexter', 'gurukavari']
name = "Saurabh Chandra Patel"
first_name = name.rsplit(' ', 1)[0]
last_name = name.rsplit(' ', 1)[1]
first_name = Saurabh Chandra
last_name = Patel
if name = "Saurabh Patel"
first_name = Saurabh
last_name = Patel

Python LOB to List

Using:
cur.execute(SQL)
response= cur.fetchall() //response is a LOB object
names = response[0][0].read()
i have following SQL response as String names:
'Mike':'Mike'
'John':'John'
'Mike/B':'Mike/B'
As you can see it comes formatted. It is actualy formatted like:\\'Mike\\':\\'Mike\\'\n\\'John\\'... and so on
in order to check if for example Mike is inside list at least one time (i don't care how many times but at least one time)
I would like to have something like that:
l = ['Mike', 'Mike', 'John', 'John', 'Mike/B', 'Mike/B'],
so i could simply iterate over the list and ask
for name in l:
'Mike' == name:
do something
Any Ideas how i could do that?
Many thanks
Edit:
When i do:
list = names.split()
I receive the list which is nearly how i want it, but the elements inside look still like this!!!:
list = ['\\'Mike\\':\\'Mike\\", ...]
names = ['\\'Mike\\':\\'Mike\\", ...]
for name in names:
if "Mike" in name:
print "Mike is here"
The \\' business is caused by mysql escaping the '
if you have a list of names try this:
my_names = ["Tom", "Dick", "Harry"]
names = ['\\'Mike\\':\\'Mike\\", ...]
for name in names:
for my_name in my_names:
if myname in name:
print myname, " is here"
import re
pattern = re.compile(r"[\n\\:']+")
list_of_names = pattern.split(names)
# ['', 'Mike', 'Mike', 'John', 'John', 'Mike/B', '']
# Quick-tip: Try not to name a list with "list" as "list" is a built-in
You can keep your results this way or do a final cleanup to remove empty strings
clean_list = list(filter(lambda x: x!='', list_of_names))

Keeping a count of words in a list without using any count method in python?

I need to keep a count of words in the list that appear once in a list, and one list for words that appear twice without using any count method, I tried using a set but it removes only the duplicate not the original. Is there any way to keep the words appearing once in one list and words that appear twice in another list?
the sample file is text = ['Andy Fennimore Cooper\n', 'Peter, Paul, and Mary\n',
'Andy Gosling\n'], so technically Andy, and Andy would be in one list, and the rest in the other.
Using dictionaries is not allowed :/
for word in text:
clean = clean_up(word)
for words in clean.split():
clean2 = clean_up(words)
l = clean_list.append(clean2)
if clean2 not in clean_list:
clean_list.append(clean2)
print(clean_list)
This is a very bad, unPythonic way of doing things; but once you disallow Counter and dict, this is about all that's left. (Edit: except for sets, d'oh!)
text = ['Andy Fennimore Cooper\n', 'Peter, Paul, and Mary\n', 'Andy Gosling\n']
once_words = []
more_than_once_words = []
for sentence in text:
for word in sentence.split():
if word in more_than_once_words:
pass # do nothing
elif word in once_words:
once_words.remove(word)
more_than_once_words.append(word)
else:
once_words.append(word)
which results in
# once_words
['Fennimore', 'Cooper', 'Peter,', 'Paul,', 'and', 'Mary', 'Gosling']
# more_than_once_words
['Andy']
It is a silly problem removing key data structures or loops or whatever. Why not just program is C then? Tell your teacher to get a job...
Editorial aside, here is a solution:
>>> text = ['Andy Fennimore Cooper\n', 'Peter, Paul, and Mary\n','Andy Gosling\n']
>>> data=' '.join(e.strip('\n,.') for e in ''.join(text).split()).split()
>>> data
['Andy', 'Fennimore', 'Cooper', 'Peter', 'Paul', 'and', 'Mary', 'Andy', 'Gosling']
>>> [e for e in data if data.count(e)==1]
['Fennimore', 'Cooper', 'Peter', 'Paul', 'and', 'Mary', 'Gosling']
>>> list({e for e in data if data.count(e)==2})
['Andy']
If you can use a set (I wouldn't use it either, if you're not allowed to use dictionaries), then you can use the set to keep track of what words you have 'seen'... and another one for the words that appear more than once. Eg:
seen = set()
duplicate = set()
Then, each time you get a word, test if it is on seen. If it is not, add it to seen. If it is in seen, add it to duplicate.
At the end, you'd have a set of seen words, containing all the words, and a duplicate set, with all those that appear more than once.
Then you only need to substract duplicate from seen, and the result is the words that have no duplicates (ie. the ones that appear only once).
This can also be implemented using only lists (which would be more honest to your homework, if a bit more laborious).
from itertools import groupby
from operator import itemgetter
text = ['Andy Fennimore Cooper\n', 'Peter, Paul, and Mary\n', 'Andy Gosling\n']
one, two = [list(group) for key, group in groupby( sorted(((key, len(list(group))) for key, group in groupby( sorted(' '.join(text).split()))), key=itemgetter(1)), key=itemgetter(1))]

Categories

Resources