I am taking a user input of "components" splitting it into a list and comparing those components to a list of available components generated from column A of a google sheet. Then what I am attempting to do is return the cell value from column G corresponding the Column A index. Then repeat this for all input values.
So far I am getting the first value just fine but I'm obviously missing something to get it to cycle back and to the remaining user input components. I tried some stuff using itertools but wasn't able to get the results I wanted. I have a feeling I will facepalm when I discover the solution to this through here or on my own.
mix = select.split(',') # sets user input to string and sparates elements
ws = s.worksheet("Details") # opens table in google sheet
c_list = ws.col_values(1) # sets column A to a list
modifier = [""] * len(mix) # sets size of list based on user input
list = str(c_list).lower()
for i in range(len(mix)):
if str(mix[i]).lower() in str(c_list).lower():
for j in range(len(c_list)):
if str(mix[i]).lower() == str(c_list[j]).lower():
modifier[i] = ws.cell(j+1,7).value # get value of cell from Column G corresponding to Column A for component name
print(mix)
print(modifier)
You are over complicating the code by writing C like code.
I have changed all the loops you had to a simpler single loop, I have also left comments above each code line to explain what it does.
# Here we use .lower() to lower case all the values in select
# before splitting them and adding them to the list "mix"
mix = select.lower().split(",")
ws = s.worksheet("Details")
# Here we used a list comprehension to create a list of the "column A"
# values but all in lower case
c_list = [cell.lower() for cell in ws.col_values(1)]
modifier = [""] * len(mix)
# Here we loop through every item in mix, but also keep a count of iterations
# we have made, which we will use later to add the "column G" element to the
# corresponding location in the list "modifier"
for i, value in enumerate(mix):
# Here we check if the value exists in the c_list
if value in c_list:
# If we find the value in the c_list, we get the index of the value in c_list
index = c_list.index(value)
# Here we add the value of column G that has an index of "index + 1" to
# the modifier list at the same location of the value in list "mix"
modifier[i] = ws.cell(index + 1, 7).value
Related
I am trying workarounds for a discord command that modifies a document.
I am have a simple question: is it possible to update a whole column with something like sheet.update('B:B', scores) with a dictionary (multiple objects)? I want to have columns B and C updated with the keys being in B and values in C.
Before I had to use code specified in this post, and make two lists (one with the names [B] and one with the values [C]) with the names and values in their own lists, so they were nested.
for count in range(len(data['guild']['members'])): # For every guild member:
res = requests.get("https://playerdb.co/api/player/minecraft/" + data['guild']['members'][count]['uuid']) # Response from server
if res.status_code != 200: # If it wasn't a success, continue the loop and print a message
ctx.author.send("Error 1: Invaild name from player database API!")
continue
name = res.json()['data']['player']['username'] # We know it was successful if we got to this point, so it's safe to try and get data from our response
names.append(name)
# Members' GXP
xp = data['guild']['members'][count]['expHistory']
xp = sum(xp.values())
xpHist.append(xp)
# Weekly Total
wTotal = sum(xpHist)
print(xpHist)
That didn't work in the command and I am trying a dictionary method with the enumerate function.
Thanks!
EDIT: Updated to reference code as got striked for that :)
You can achieve the following with a simple for loop.
Add "Keys" to Cell A1 and "Values" to Cell B1.
Make a variable idx or anything and set it 2 as we already set A1 and B1 cell to "Keys" and "Values".
Iterate through dict.items() and update cell "A{idx}" to key and "B{idx}" to key and value respectively.
Add one to idx so while iterating to nth key and value, it does not update the same cell
sheet.update_cell("A1", "Keys")
sheet.update_cell("B1", "Values")
idx = 2
for k, v in dict.items():
sheet.update_cell(f"A{idx}", k)
sheet.update_cell(f"B{idx}", v)
idx += 1
I want to append the key value pairs in my python dictionary without including the brackets... I'm not really sure how to do that.
I've tried looking at similar questions but it isn't working for me.
#this creates a new workbook call difference
file = xlrd.open_workbook('/Users/im/Documents/Exception_Cases/Orders.xls')
wb = xl_copy(file)
Sheet1 = wb.add_sheet('differences')
#this creates header for two columns
Sheet1.write(0,0,"S_Numbers")
Sheet1.write(0,1," Values")
#this would store all the of Key, value pair of my dictionary into their respective SO_Numbers, Booking Values column
print(len(diff_so_keyval))
rowplacement = 1
while rowplacement < len(diff_so_keyval):
for k, v in diff_so_keyval.items():
Sheet1.write(rowplacement,0,k)
Sheet1.write(rowplacement,1,str(v))
rowplacement = rowplacement + 1
#This is what I have in my diff_so_keyval dictionary
diff_so_keyval = {104370541:[31203.7]
106813775:[187500.0]
106842625:[60349.8]
106843037:[492410.5]
106918995:[7501.25]
106919025:[427090.0]
106925184:[30676.4]
106941476:[203.58]
106941482:[203.58]
106941514:[407.16]
106962317:[61396.36]}
#this is the output
S_numbers Values
104370541 [31203.7]
106813775 [187500.0]
106842625 [60349.8]
I want the values without the brackets
Looks to me like the 'values' in the dictionary are actually single-element lists.
If you simply extract the 0th element out of the list, then that should work for 'removing the brackets':
Sheet1.write(rowplacement, 1, v[0])
I am trying to figure out the most efficient way of finding similar values of a specific cell in a specified column(not all columns) in an excel .xlsx document. The code I have currently assumes all of the strings are unsorted. However the file I am using and the files I will be using all have strings sorted from A-Z. So instead of doing a linear search I wonder what other search algorithm I could use as well as being able to fix my coding eg(binary search etc).
So far I have created a function: find(). Before the function runs the program takes in a value from the user's input that then gets set as the sheet name. I print out all available sheet names in the excel doc just to help the user. I created an empty array results[] to store well....the results. I created a for loop that iterates through only column A because I only want to iterate through a custom column. I created a variable called start that is the first coordinate in column A eg(A1 or A400) this will change depending on the iteration the loop is on. I created a variable called next that will get compared with the start. Next is technically just start + 1, however since I cant add +1 to a string I concatenate and type cast everything so that the iteration becomes a range from A1-100 or however many cells are in column A. My function getVal() gets called with two parameters, the coordinate of the cell and the worksheet we are working from. The value that is returned from getVal() is also passed inside my function Similar() which is just a function that calls SequenceMatcher() from difflib. Similar just returns the percentage of how similar two strings are. Eg. similar(hello, helloo) returns int 90 or something like that. Once the similar function is called if the strings are above 40 percent similar appends the coordinates into the results[] array.
def setSheet(ws):
sheet = wb[ws]
return sheet
def getVal(coordinate, worksheet):
value = worksheet[coordinate].value
return value
def similar(first, second):
percent = SequenceMatcher(None, first, second).ratio() * 100
return percent
def find():
column = "A"
print("\n")
print("These are all available sheets: ", wb.sheetnames)
print("\n")
name = input("What sheet are we working out of> ")
results = []
ws = setSheet(name)
for i in range(1, ws.max_row):
temp = str(column + str(i))
x = ws[temp]
start = ws[x].coordinate
y = str(column + str(i + 1))
next = ws[y].coordinate
if(similar(getVal(start,ws), getVal(next,ws)) > 40):
results.append(getVal(start))
return results
This is some nasty looking code so I do apologize in advance. The expected results should just be a list of strings that are "similar".
I have what might be a simple task and I tried several solutions but can't seem to figure it out.
I have a dict of sets containing gene names and corresponding positions as sets like:
gene_nr_snp = {'gene1: {3,9}, gene2: {2,3,1}, gene3: {1}}
I want to return a dict with the gene name and the corresponding summed value.
I tried the following:
gene_values = {}
for gene, snp in gene_nr_snp.items():
for i in snp: # iterate the values in each set
snp_total = 0
snp_total += i
gene_values[gene].add(snp_total)
This is returning the same set of values
You can use a dict comprehension and the sum() function:
gene_values = {gene: sum(snp) for gene, snp in gene_nr_snp.items()}
Your attempt fails because you set the snp_total variable to 0 for every value in snp, thus failing to sum anything. You then seem to treat gene_values[gene] as a set but the dictionary starts empty, so you'll get a KeyError. A working version would be:
gene_values = {}
for gene, snp in gene_nr_snp.items():
snp_total = 0
for i in snp: # iterate the values in each set
snp_total += i
gene_values[gene] = snp_total
but the sum() function makes the inner loop rather more verbose than needed; the whole loop body could be replaced by gene_values[gene] = sum(snp).
I have two lists one is a subject list, it can vary from 2 to 4 subjects at max. Second list is reporting list which provides information,whether we need a report for the subject.
Possible values for reporting list are:
All_Subjects which means we require report for all subjects.
No_Subject which means we dont require report for any subject
Lastly the format, SubjectName_(All|NO)_Report which means if for a particular subject we want a report or not.
-subject_list = ["Subject", "Chemistry", "Physics" , "Mathematics" , "Bio"] #sequence always remains same.
reporting_list can be ["All_Subjects", "No_Subjects", "Chemistry_No_Report","Chemistry_All_Report"] #sequence does not matter
Function report_required returns a list whether we want a report or not, and returns a list. If list has all "None" values it means no report required.
For example: I have:
reporting_list = ["Chemistry_No_Report", "Mathematics_All_Report]
subject_list = ["Subject", "Chemistry", "Physics" , "Mathematics"]
My subject_list always starts with a value Subject, which I ignore when returning mapped values
my return value should be ["No", None, "Yes"]
My current function below works, is there a more efficient way of mapping out a third list based on two list values.
def reportRequired( reporting_list , subject_list):
report_list = [None]*4
for value in reporting_list:
# subject_list starts with a header value "Subject", thats why iterating from index 1
if value.startswith("All"):
for idx in range(1, len(subject_list)):
report_list[idx-1] = "Yes"
if value.startswith("No"):
for idx in range(1, len(subject_list)):
report_list[idx-1] = "No"
if value.split("_")[1].lower() == "no":
for idx in range(1, len(subject_list)):
if value.split("_")[0].strip() == subject_list[idx]:
report_list[idx-1] = "No"
if value.split("_")[1].lower() == "all":
for idx in range(1, len(subject_list)):
if value.split("_")[0].strip() == subject_list[idx]:
report_list[idx-1] = "Yes"
return report_list
Build a dictionary that maps a subject name to an index and use that to access an element of the report_list. This way, you avoid quadratic complexity of the 3rd and 4rth case.
For the 1st and 2nd case: prepare a list filled with Yes-es and a list filled with No-s. Then you can use them, regardless how often that case appears in the reporting_list.
Note: You can use ['Yes']*4, as you already do in the initialization of the report_list.
Overall complexity is "almost" linear (assuming O(1) dictionary access...)
Edit: If a subject can appear multiple times in the subject list, this doesn't work.
But you can build a dictionary in which you store the answer for every subject, and in the second phase, walk through the subject list and output the answer for every subject.