Function to remove specific word from lists - python

I have several lists that contain the word 'Data' at varying locations in the list. I'd like to build a function that removes just that word (not the whole element) from each list.
example_list = ['Client 1 Data','Client 2','Client 3 Data']
output_list = ['Client 1','Client 2','Client 3']
I've tried this but it is not working:
def my_func(column):
for i in range(len(column)):
column.replace('Data', '')

Consider using a list comprehension:
def remove_word(words: list[str], word_to_remove: str) -> list[str]:
return [w.replace(word_to_remove, '').strip() for w in words]
def main() -> None:
example_list = ['Client 1 Data', 'Client 2', 'Client 3 Data']
print(f'{example_list = }')
output_list = remove_word(words=example_list, word_to_remove='Data')
print(f'{output_list = }')
if __name__ == '__main__':
main()
Output:
example_list = ['Client 1 Data', 'Client 2', 'Client 3 Data']
output_list = ['Client 1', 'Client 2', 'Client 3']

you can use map on list but if you want correct your code, you need to specify which index you want to change:
>>> list(map(lambda x: x.replace('Data', ''), example_list))
['Client 1 ', 'Client 2', 'Client 3 ']
Your Code:
def my_func(column):
for i in range(len(column)):
column[i] = column[i].replace('Data', '')
Output:
my_func(example_list)
print(example_list)
# ['Client 1 ', 'Client 2', 'Client 3 ']

Calling replace returns a modified string, it doesn't modify the original string. If you want to modify the list, you need to call replace on each individual string and assign the modified strings to the indices in the original list:
>>> def my_func(column):
... for i in range(len(column)):
... column[i] = column[i].replace('Data', '')
...
>>> example_list = ['Client 1 Data','Client 2','Client 3 Data']
>>> my_func(example_list)
>>> example_list
['Client 1 ', 'Client 2', 'Client 3 ']
If modifying the original list inside the function isn't a requirement, it's actually easier to do this by building and returning a new list:
>>> def my_func(column):
... return [entry.replace('Data', '') for entry in column]
...
>>> my_func(['Client 1 Data','Client 2','Client 3 Data'])
['Client 1 ', 'Client 2', 'Client 3 ']
Another option is to build the new list inside the function (e.g. via comprehension) and then assign it into the original list via a slice assignment:
>>> def my_func(column):
... column[:] = [entry.replace('Data', '') for entry in column]
...
>>> example_list = ['Client 1 Data','Client 2','Client 3 Data']
>>> my_func(example_list)
>>> example_list
['Client 1 ', 'Client 2', 'Client 3 ']

Related

How to split the given 'key-value' list into two lists separated as 'keys' and 'values' with python

This is my List
List = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
I need to separate the List into two differnt List's sepearted as 'keys' and 'values'
List = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
KeyList
KeyList = ['function', 'string', 'hello', 'new', 'test']
ValueList
ValueList = ['function1', 'string1', 'hello1', 'new1', 'test1']
There are different possible approach. One is the method proposed by Tim, but if you are not familiar with re you could also do:
List = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
KeyList = []
ValueList = []
for item in List:
val = item.split(' = ')
KeyList.append(val[0])
ValueList.append(val[1])
print(KeyList)
print(ValueList)
and the output is:
['function', 'string', 'hello', 'new', 'test']
['function1', 'string1', 'hello1', 'new1', 'test1']
You can simply use split(" = ") and unzip the list of key-value pairs to two tuples:
keys, values = zip(*map(lambda s: s.split(" = "), List))
# keys
# >>> ('function', 'string', 'hello', 'new', 'test')
# values
# >>>('function1', 'string1', 'hello1', 'new1', 'test1')
This is based on the fact that zip(*a_zipped_iterable) works as an unzipping function.
We can use re.findall here:
inp = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
keys = [re.findall(r'(\w+) =', x)[0] for x in inp]
vals = [re.findall(r'\w+ = (\w+)', x)[0] for x in inp]
keys = [pair[0] for pair in pairs]
values = [pair[1] for pair in pairs]

How to set the first item in a list to a dictionary key, and the following items as the value for that key?

Here is what I'm trying:
def dict1(filename):
try:
file_handle=open(filename,"r")
except FileNotFoundError:
return None
newdict = {}
for line in file_handle:
list1 = line.split(",")
(k, v) = list1
newdict[k]] = v
return newdict
The text file has lines like such:
cow 1,dog 1
john 1,rob 1,ford 1
robert 1,kyle 1,jake 1
I'm trying to create a dictionary like so:
{'cow 1': ['dog 1'],
'john 1': ['rob 1','ford 1'],
'robert 1': ['kyle 1, 'jake 1']}
The problem is in the (k,v), how can i set the k to the first item in the list, and the v to all items following it?
Using the csv module.
Ex:
import csv
result = {}
with open(filename) as infile:
reader = csv.reader(infile)
for row in reader: #Iterate each row
result[row[0]] = row[1:] #column 0 as key and others as value.
print(result)
Output:
{'cow 1': ['dog 1'],
'john 1': ['rob 1', 'ford 1'],
'robert 1': ['kyle 1', 'jake 1']}

Split list into sublists at every occurrence of element starting with specific substring

I have a large list that contains a bunch of strings. I need to sort the elements of the original list into a nested list, determined by their placement in the list. In other words, I need to break the original list into sublists, where each sublist contains all elements that fall between an element starting with 'ABC', and then join them together as a nested list.
So the original list is:
all_results = ['ABCAccount', 'def = 0', 'gg = 0', 'kec = 0', 'tend = 1234567890', 'ert = abc', 'sed = target', 'id = sadfefsd3g3g24b24b', 'ABCAccount', 'def = 0', 'gg = 0', 'kec = 0', 'tend = NA', 'ert = abc', 'sed = source', 'id = sadfefsd3g3g24b24b', 'ABCAdditional', 'addkey = weds', 'addvalue = false', 'ert = abc', 'sed = target', 'id = sadfefsd3g3g24b24b', 'time_zone = EDT’]
And I need to return:
split_results = [['ABCAccount','def = 0', 'gg = 0', 'kec = 0', 'tend = 1234567890', 'ert = abc', 'sed = target', 'id = sadfefsd3g3g24b24b'],['ABCAccount', 'def = 0', 'gg = 0', 'kec = 0', 'tend = NA', 'ert = abc', 'sed = source', 'id = sadfefsd3g3g24b24b'],['ABCAdditional', 'addkey = weds', 'addvalue = false', 'ert = abc', 'sed = target', 'id = sadfefsd3g3g24b24b', 'time_zone = EDT’]]
I have tried the following:
split_results = [l.split(',') for l in ','.join(all_results).split('ABC')]
You can work from your original list directly:
def make_split( lst ):
if len(lst) == 0:
return []
r0 = []
r1 = []
for s in lst:
if s.startswith("ABC"):
if r1:
r0.append(r1)
r1 = []
r1.append(s)
return r0 + [r1]

Python Search through list of tuples and return based on Index With multiple conditions

I have a list of tuples that is a table of reference:
classes = [('400002', 'Class1'), ('400009', 'Class2'), ('400209', 'Class3')]
I also have a list of of tuples that are records:
records = [('Record 1','400002','Memo1'),('Record 2','400003','Memo2'),
('Record 3','400009','Memo3'),('Record 4','L-REF-96-1','Memo4'),('Record
5','400002','Memo5')]
In order to import I have to assign a class and encode it:
encoding = 'utf-8'
ref = []
nom = []
memo = []
classr = []
for i in records:
ref.append(i[0].encode(encoding))
nom.append(i[1].encode(encoding))
memo.append(i[2].encode(encoding))
now I need to assign a class based on index 2 if it exist, else if the str starts with L-REF then give it a general class, else give it a different general class
I was doing something like this but it didnt work it always defaulted to the "Else" part of my if statement:
for c in classes:
if i[1] == c[0]:
newclass = c[1]
elif i[1][5:] == "L-REF":
newclass = 'general class1'
else:
newclass = 'general class2'
classr.append(newclass.encode(encoding))
newimportlist = list(zip(ref,nom,memo,classr))
Expected output:
[(b'Record 1',b'400002',b'Memo1',b'Class1'),(b'Record
2',b'400003',b'Memo2',b'general class2'), (b'Record
3',b'400009',b'Memo3',b'Class2'),(b'Record 4',b'L-REF-96-
1',b'Memo4',b'general class 1'),(b'Record
5',b'400002',b'Memo5',b'Class1')]
First of all ref, nom and memo can be constructed more easily by transposing your list records.
>>> ref, nom, memo = map(list, zip(*records))
>>> ref
>>> ['Record 1', 'Record 1', 'Record 3', 'Record 4', 'Record 5']
>>> nom
>>> ['400002', '400003', '400009', 'L-REF-96-1', '400002']
>>> memo
>>> ['Memo1', 'Memo1', 'Memo3', 'Memo4', 'Memo5']
Now construct a dictionary from classes.
>>> classes = [('400002', 'Class1'), ('400009', 'Class2'), ('400209', 'Class3')]
>>> classes_dict = dict(classes)
>>> classes_dict
>>> {'400002': 'Class1', '400009': 'Class2', '400209': 'Class3'}
classr (which I assume is the name for your expected output) can now be built as follows:
>>> classr = []
>>>
>>> for rec, clsno, mem in records:
...: clsname = classes_dict.get(clsno, 'general class2')
...: if clsno[:5] == 'L-REF':
...: clsname = 'general class1'
...: classr.append((rec, clsno, mem, clsname))
>>>
>>> classr
>>>
[('Record 1', '400002', 'Memo1', 'Class1'),
('Record 1', '400003', 'Memo1', 'general class2'),
('Record 3', '400009', 'Memo3', 'Class2'),
('Record 4', 'L-REF-96-1', 'Memo4', 'general class1'),
('Record 5', '400002', 'Memo5', 'Class1')]

Getting the index information of a specific string in a nested list? In python 3

So I have a list with lots of nested lists in which include a students name and their test score, I was wondering how to retrieve the index (position) of the sub list that contains a specific students scores by searching with the variable 'name' that the user enters when the quiz starts. The lists sub list are created from the import of a text file, the code i used for that is:
with open('class1quizscoreboard.txt') as scoreboard:
for line in scoreboard:
importedscores.append(line.strip().split(',')
This works and the list looks like this:
[['EmilyScott ', ' 3'], ['Student Name ', ' 2'], ['Another Student ', ' 1'], ['Third Student ', ' 10']]
So I want to find the sublist by searching with the variable name, then find the position of that sublist IF the name is already in the list of scores. I would then add another test score into the list, but if it is not there just create another list.
I thought of approaching this with an IF with FOR loop, but didn't work
for sublist in importedscores:
if sublist[0] == search:
print ("Found it!"), sublist
Returned 'not found' even though I used a name I knew was definitely in the list
I am probably approaching this in the complete wrong way but the end goal is to be able to add the students new score to their existing list if they already have one
Thank you for any help
By dictionary:
Read txt file by csv module because file have well define structure.
Create dictionary from the file content where key is name and value is integer number i.e. score
Add new score for existing student.
Create new entry for new sequent.
Demo:
import csv
import pprint
p = "input34.txt"
with open(p, "rb") as fp:
root = csv.reader(fp, delimiter=',')
result = {}
for i in root:
result[i[0].strip()] = int(i[1].strip())
print "Debug1 result:"
pprint.pprint (result)
serach_key = "Test student"
add_value = 5
if serach_key in result:
result[serach_key] = result[serach_key] + add_value
else:
result[serach_key] = add_value
print "Debug2 result:"
pprint.pprint (result)
serach_key = "Another Student"
add_value = 5
if serach_key in result:
result[serach_key] = result[serach_key] + add_value
else:
result[serach_key] = add_value
print "Debug3 result:"
pprint.pprint (result)
Output:
vivek#vivek:~/Desktop/stackoverflow$ python 34.py
Debug1 result:
{'Another Student': 1,
'Emily Scott': 3,
'Student Name': 2,
'Third Student': 10}
Debug2 result:
{'Another Student': 1,
'Emily Scott': 3,
'Student Name': 2,
'Test student': 5,
'Third Student': 10}
Debug3 result:
{'Another Student': 6,
'Emily Scott': 3,
'Student Name': 2,
'Test student': 5,
'Third Student': 10}
By list:
Demo:
mport csv
import pprint
p = "input34.txt"
with open(p, "rb") as fp:
root = csv.reader(fp, delimiter=',')
result = []
for i in root:
result.append([i[0].strip(), int(i[1].strip())])
print "Debug1 result:"
pprint.pprint (result)
serach_key = "Test student"
add_value = 5
add_flag = False
for i,j in enumerate(result):
if serach_key==j[0]:
j[1] = j[1] + add_value
add_flag = True
break
if add_flag==False:
result.append([serach_key, add_value])
print "Debug2 result:"
pprint.pprint (result)
serach_key = "Another Student"
add_value = 5
add_flag = False
for i,j in enumerate(result):
if serach_key==j[0]:
j[1] = j[1] + add_value
add_flag = True
break
if add_flag==False:
result.append([serach_key, add_value])
print "Debug3 result:"
pprint.pprint (result)
Output:
vivek#vivek:~/Desktop/stackoverflow$ python 34.py
Debug1 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 1],
['Third Student', 10]]
Debug2 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 1],
['Third Student', 10],
['Test student', 5]]
Debug3 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 6],
['Third Student', 10],
['Test student', 5]]
Use collection.defaultdict to optimize code. So no need to check key is in dictionary or not.
e.g.:
>>> import collections
>>> result = collections.defaultdict(int)
>>> result["student 1"] = 10
>>> result["student 2"] = 20
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 20})
>>> result["student 2"] = result["student 2"] + 2
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 22})
>>> result["student 3"] = result["student 3"] + 5
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 22, 'student 3': 5})
>>>
Try this:
def find_index(name,list):
for i, j in enumerate(list) :
if name in j :
return ('{}'.format(i))
for i, j in list:
if name == i:
ind = int(find_index(name, list))
list[ind].append(score)
Here in the find_index function the i represents the index and the j the sublist. - if name in sublist, return the index
In the for loop the i and j represent every subsection in the list. Its a bit hard to explain, but the i is the name and j the score

Categories