Adding a string to an array adds all characters separately - python-3.x

Any suggestion how to merge it better so the dual digits numbers does not split?
Sorry for bad english.
def merge(strArr):
newList = []
for x in range(len(strArr)):
newList += strArr[x]
return newList
array_test = ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
print(merge(array_test))
output =['1', ',', ' ', '3', ',', ' ', '4', ',', ' ', '7', ',', ' ', '1', '3', '1', ',', ' ', '2', ',', ' ', '4', ',', ' ', '1', '3', ',', ' ', '1', '5']`
expected output= [1,2,3,4,7,13,1,2,4,13,15]

Using list comprehension:
merged_arr = [n for s in array_test for n in s.split(", ")]
print(merged_arr)
This prints:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']

It merges this way because for lists += is an array concatenation and that in this context your string object is interpreted as an array of characters:
[] += "Hello"
# Equivalent to
[] += ["H", "e", "l", "l", "o"]
If you want to join strings you can do:
out = "".join(array_test)

Your result becomes the way it is, because you take each inner string and add each character of it to your return-list without removing any spaces or commas.
You can change your code to:
def merge(strArr):
new_list = []
for inner in strArr:
new_list.extend(inner.split(", ")) # split and extend instead of += (== append)
return new_list
array_test =["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
merged = merge(array_test)
as_int = list(map(int,merged))
print(merged)
print(as_int)
Output:
['1', '3', '4', '7', '13', '1', '2', '4', '13', '15']
[1, 3, 4, 7, 13, 1, 2, 4, 13, 15]
Without as_int()you will still hav strings in your list, you need to convert them into integers.

Related

Trying to understand the Python enumerate function and "Key not found" error

I have been going over and over this code but I can't understand why I am getting a Key not found 1 error.
The code below may not be enough, but basically, I am comparing grades in a list excel_data_choice with those pulled from a spreadsheet video_grades. It should iterate through the list to determine that grade exists in the video_grades list and if not, then add it.
However, I keep getting the error. Here is my output log. I am sure I am doing something wrong, but just can't see it.
Output
Video grades are: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
K is 0 and v is K
K is 1 and v is 1
Key not found 1
Code
excel_data_choice = ['K', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
try:
for k,v in enumerate(excel_data_choice):
print(f'K is {k} and v is {v}')
if row[v] == v and str(row[v]) not in video_grades:
print(f'row v = {row[v]}')
video_grades.append(str(v))
print(f'Video grades is now {video_grades}')
elif row[v] == 'delete' and str(v) in video_grades:
video_grades.remove(str(v))
acf_dict['video_grades'] = video_grades
except KeyError as ke:
print(f'Key not found {ke}')

How to compare particular element in list python3?

l1= [['1', 'apple', '1', '2', '1', '0', '0', '0'], ['1',
'cherry', '1', '1', '1', '0', '0', '0']]
l2 = [['1', 'cherry', '2', '1'],
['1', 'plums', '2', '15'],
['1', 'orange', '2', '15'],
['1', 'cherry', '2', '1'],
['1', 'cherry', '2', '1']]
output = []
for i in l1:
for j in l2:
if i[1] != j[1]:
output.append(j)
break
print(output)
Expected Output:
[['1', 'plums', '2', '15'], ['1', 'orange', '2', '15']]
How to stop iteration and find unique elements and get the sublist?
How to stop iteration and find unique elements and get the sublist?
To find the elements in L2 that are not in L1 based on the fruit name:
l1= [[1,'apple',3],[1,'cherry',4]]
l2 = [[1,'apple',3],[1,'plums',4],[1,'orange',3],[1,'apple',4]]
output = []
for e in l2:
if not e[1] in [f[1] for f in l1]: # search by matching fruit
output.append(e)
print(output)
Output
[[1, 'plums', 4], [1, 'orange', 3]]
You can store all the unique elements from list1 in a new list, then check for list2 if that element exists in the new list. Something like:
newlist = []
for item in l1:
if item[1] not in newlist:
newlist.append(item)
output = []
for item in l2:
if item[1] not in newlist:
output.append(item)
print(output)
This is slightly inefficient but really straightforward to understand.

Convert a list of strings to a list of strings and ints

I have a dictionary of lists, each list has 3 elements, i want to convert the 2nd and 3rd element to ints from strings
dict = {1:['string', '2', '3',],
2:['string', '2', '3',],
3:['string', '2', '3',],}
to become:
dict = {1:['string', 2, 3,],
2:['string', 2, 3,],
3:['string', 2, 3,],}
Thank you
Firstly, don't name your dictionaries dict, as it's a reserved keyword and you don't wanna overwrite it.
Coming to your solution.
d = {
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
d2 = {
k: [
int(i) if i.isdigit() else i
for i in v
]
for k, v in d.items()
}
Will give you an output of:
{
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3']
}
{
1: ['string', 23, 3],
2: ['string', 2, 3],
3: ['string', 2, 3]
}
If you dictionary has many elements you might not want to create a second dictionary in memory, but modify the existing one in-place:
data = {
1: ['string', '2', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
for v in data.values():
v.append(int(v.pop(1)))
v.append(int(v.pop(1)))
print(data)
Output:
{1: ['string', 2, 3], 2: ['string', 2, 3], 3: ['string', 2, 3]}

Appending a sublist to another list in python

with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding=[]
rawrow=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
nofinding.append(rawrow)
print(nofinding[:2])
I am reading datarows from a csv file and want to create a customized nested list based on certain columns. I want that
list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
shall return a list like
['00030805_000.png,No Finding,1,34777,69373']
which is stored in rawrow and then append to a bigger list i.e. nofinding but i am getting output as
[['0', '0', '0', '3', '0', '8', '0', '5', '', '0', '0', '0', '.',
'p', 'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g',
',', '1', ',', '3', '4', '7', '7', '7', ',', '6', '9', '3', '7', '3'],
['0', '0', '0', '3', '0', '8', '0', '4', '', '0', '0', '0', '.', 'p',
'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g', ',',
'1', ',', '3', '5', '4', '0', '5', ',', '6', '3', '0', '8', '8']]
Desired output
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No
Finding,1,35405,63088'] ]
Thank you
Your issue is that rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18])) is turning the string in to a list of characters
if you want to leave this as a comma delimited string replace that line with the following:
rawrow = row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]
or more cleanly:
rawrow = ",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]])
I am curious though why you want:
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No Finding,1,35405,63088'] ]
Instead of this:
[ ['00030805_000.png','No Finding',1,34777,69373], ['00030804_000.png','No Finding',1,35405,63088] ]
which you could achieve with the following:
rawrow = []
for row_index in [0, 1, 2, 17, 18]:
rawrow.append(row[row_index].split(","))
or in one line:
rawrow = [row[row_index].split(",") for row_index in [0, 1, 2, 17, 18]]
Furthermore, your whole code could be consolidated as follows:
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding = [",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]]) for row in datarows if row[1]=='No Finding' and row[2]=='1']
print(nofinding[:2])
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
rawrow = []
nofindings=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = [''.join(row[row_index]) for row_index in [0, 1, 2, 17, 18] ]
nofindings.append(rawrow)
print(nofindings[:3])
Solved my issues.

How to make my program faster

"""
ID: nihaowa2
LANG: PYTHON3
TASK: namenum
"""
import time
start=time.time()
translate = {2: ("A","B","C"), 5: ("J","K","L"), 8: ("T","U","V"), 3:("D","E","F"), 6: ("M","N","O"), 9: ("W","X","Y"), 4: ("G","H","I") ,7 ("P","R","S")}
names=[""]
def newlist(nl,num):
nnl=[]
for i in translate[num]:
for j in nl:
nnl.append(j+i)
return nnl
gnd=open ('dict.txt', 'r')
goodnames=[]
for i in range(4616):
goodnames.append(gnd.readline()[:-1])
goodnames.append(gnd.readline())
fin = open ('namenum.in', 'r')
fout = open ('namenum.out', 'w')
namenumber=(fin.readline().split()[0])
for i in namenumber:
names=newlist(names,int(i))
ok=[]
for i in names:
if i in goodnames:
ok.append(i)
if ok==[]:
fout.write("NONE\n")
else:
ok.sort()
for i in ok:
fout.write(i+"\n")
fout.close()
The code above is my attempt to solve this problem . But the grader says it is too slow . I need it to run under 1 second . Could anyone please give me some suggestions?
It is faster if you read in the whole file, prepare a dictionary from {"Number" : [list of names]} - For simplicity I choose to keep the string input and make a string out of the key for the dict as {str:[str,...], ...}
You can then simply look up the cows number in the dict wich is O(1):
mapping = {'A': '2', 'B': '2', 'C': '2',
'D': '3', 'E': '3', 'F': '3',
'G': '4', 'H': '4', 'I': '4',
'J': '5', 'K': '5', 'L': '5',
'M': '6', 'N': '6', 'O': '6',
'P': '7', 'R': '7', 'S': '7',
'T': '8', 'U': '8', 'V': '8',
'W': '9', 'X': '9', 'Y': '9',
'Q': '', 'Z': '' # part of names in the file, not mapped to a number
}
# read file, one word per list entry, cleaned
def readDict(fn="dict.txt"):
with open(fn) as f:
all = f.readlines()
return [x.strip('\n').strip() for x in all]
# a name as number
def getNameAsNum(name):
return ''.join( mapping[c] for c in name)
# dict of numbers as key and all names as list
def namesAsNums():
names = readDict()
d={}
for name in names:
asNum = getNameAsNum(name)
if asNum and name:
d.setdefault(asNum[:], list())
d[asNum].append(name[:])
d[asNum].sort()
return d
# get the mapping of number to list of names
nan = namesAsNums()
# get input and ---
cow = input("Which cow serial number?")
# limit string to 12
cow = ''.join(cow[0:12])
# ... check and print
if cow in nan:
for n in nan[cow]:
print(n)
else:
print ("NONE")
Parsing the file takes my laptop 0.16s - lookup of it in the dict is negligable.
If there were many names with same numbers in the file I would forgoe the sort on every dict-insert and use a second go over the whole dict to sort the list when all names are in it - but for this file its the same runtime.

Resources