python


Find all dict keys based on a list, get values, and create new list. Python VLOOKUP attempt


Goal:
Take the first value of the list, find ALL keys that match, create a list of values from those matching keys.
This is pretty much just a python version of excel vlookup or index/match but better since I'll be able to concatenate the matching dict values and do stuff with it like find all the cells that match and concat them to one cell.
Here's what I've done. Yes it does work. Just thought there was an easier way. I've only been dabbling in python on and off this year so please criticize or provide best practices. Also I don't know wtf I'm doing most of the time.
import openpyxl
import re
path = r'C:\Users\aaron\Documents\HustleHardRacing\Attack Performance\TYMMC.xlsx'
wb = openpyxl.load_workbook(path)
sheet = wb.active
def lookup(value_to_lookup, against_col, indice):
def col_values(col_letter): #function for getting column values and putting to list
x = []
for item in sheet[col_letter]:
x.append(item.value)
return x
values = col_values(value_to_lookup) #list
against_col_range = col_values(against_col) # List im searcing in
indice_range = col_values(indice) # index id like to get.
pairs = dict(zip(against_col_range, indice_range)) # dict the list im searching and their associated index.
i = 2
for value in values:
if value is None:
pass
else:
k = [pairs[x] for i, x in enumerate(against_col_range) if x == value]
sheet['K'+str(i)].value = k
i = i+1
return
lookup('A','I','J')
wb.save(path)
os.startfile(path)
My Solution:
I found that using a dict comprehension allowed me to get all the cell values as keys and locate them to their coordinate(dict values). This was considerably more efficient. Thanks to Charlie Clark for the idea/motivation.
# Fundamentally
list_to_check =['spam', 'eggs', 'Castle of aaaarggghhh']
# Associates all cell values in a column 'A' to their coord's
# Pass if cell is None
excel_index = {x.value: x.coordinate for x in sheet['A'] if x.value is not None}
for item in list_to_check;
if item in excel_index.keys();
get_coordinate = excel[item] # get cell coord that matches your list
#do stuff know that you know the coord of the matching cell

Related Links

Tensorboard get blank page
Python Matplotlib Streamplot providing start points
Reduce inner points in numpy coordinate dataset (speed up concave hull)
Blitting several layers pygame
floating and integer power difference in Python
how to skip certain line in text file and keep reading the next line in python?
match key and insert into new column
Python - Storing the values of CSV file in List
Should telegram-code be defined as a message option?
I get translated text when I do a GET request (in Python). How to get English content?
Store two types of value in single Django model field
egg_info failed with error code 1
Open SAP with Python GUI Script win32com.client.Dispatch(“Sapgui.ScriptingCtrl.1”)
Python receive MySQL Decimal datatype as None
Panda .loc or .iloc to select the columns from a dataset
Elasticsearch document doesn't contain _version by default?

Categories

HOME
arrays
dotnetrdf
cntk
vb6
smartphone
decorator
thunderbird-addon
command
codeeffects
webdav
algorithmic-trading
httpclient
i2c
wicket
spinnaker
intellij-plugin
webmethods
eclipse-luna
pyyaml
cython
nsmenuitem
k2
remove-method
jquery-scrollify
qsub
bus-error
vcard
pcre
thinking-sphinx
websauna
clockwork
division
remote-server
color-scheme
xen
rhel.net
office365connectors
equivalence
s3cmd
restful-url
android-preferences
intellilock
react-native-router-flux
jrules
taglib
asmx
nstouchbar
slot
polar-coordinates
pdfkit
pspice
spring-repositories
lemoon
file-diffs
insert-into
wikitext
selendroid
connection-reset
castle-windsor-3
divide-by-zero
cpu-cores
google-plus-one
pisa
ghostdoc
argument-passing
boost-test
funkload
delphi-xe3
asp.net-mvc-scaffolding
ng-hide
swrl
database-permissions
magickwand
subtract
electronic-signature
associative
port-scanning
for-xml-path
communicationexception
aio
revision
weborb
associativity

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App