Switch with multiple conditions - visual-studio-2012

I have a table where I would like to add some colour to the cells based on the total
Name Total
Alex 12
Harry 6
Using the following switch statement the 'Total' cell does turn Light Green.
=switch(Fields!Name.value ="Alex" and Fields!Total.value >=11, "LightGreen", Fields!Name.value = "Alex" and Fields!Total.value >=8 and Fields!Total.value <=10, "Orange", Fields!Name.value = "Alex" and Fields!Total.value <=7,"Red")
However I would now like to make the 'Total' field for 'Harry' to turn Red. I have tried the following but this has not worked
=switch(Fields!Name.value ="Alex" and Fields!Total.value >=11, "LightGreen", Fields!Name.value = "Alex" and Fields!Total.value >=8 and Fields!Total.value <=10, "Orange", Fields!Name.value = "Alex" and Fields!Total.value <=7,"Red") or switch(Fields!Name.value ="Harry" and Fields!Total.value >=11, "LightGreen", Fields!Name.value = "Harry" and Fields!Total.value >=8 and Fields!Total.value <=10, "Orange", Fields!Name.value = "Harry" and Fields!Total.value <=7,"Red")

Ok managed to answer my own question. I used 'Or' in the second switch statement, swapping this with a comma solved the problem

Related

How to split elements of a list at left white space and assign the left and right to different variables?

"myList" is a list of lists.I want to split the second element in the internal list so that I can assign to variables to reformat it.
For instance:
I want to split '100 Free' to '100' and 'Free'
I want to split '0 Not Free' to '0' and 'Not Free'
I want to split '45 Partly Free' to '45' and 'Partly Free'
myList = [
['Finland', '100 Free', '40', '60'],
['Syria', '0 Not Free', '-3', '3'],
['Mozambique', '45 Partly Free', '14', '31']
]
Desired reformatted Output:
Country, Score , Status, pR, cL
Finland, 100, Free, 40, 60
Syria, 0, Not Free, -3, 3
Mozambique, 45, Partly Free, 14, 31
I splited the 2nd one as you said, it was like:
[['100', 'Free'], ['0', 'Not', 'Free'], ['45', 'Partly', 'Free']]
The first one had 2 indexes and the second and the third one was 3 indexes. so I used the try, except.
//The code starts here.
myList = [['Finland', '100 Free', '40', '60'], ['Syria', '0 Not Free', '-3', '3'],
['Mozambique', '45 Partly Free', '14', '31']]
print("Country, Score, Status, pR, cL")
for i in range(0, len(myList)):
print(myList[i][0], ",", myList[i][1].split()[0], ",", myList[i][1].split()[1], end = " ")
try:
print(myList[i][1].split()[2], ",", myList[i][2], ",", myList[i][3])
except:
print(",", myList[i][2], ",", myList[i][3])

Massage csv dataframe into dictionary style

I have a pandas dataframe named tshirt_orders from an API call looking like this:
Alice, small, red
Alice, small, green
Bob, small, blue
Bob, small, orange
Cesar, medium, yellow
David, large, purple
How can I get this into a dictionary style format where I first go by size and have sub keys under for name and another sublist for color so that I can address it when iterating over by using tshirt_orders?
Like this:
size:
small:
Name:
Alice:
Color:
red
green
Bob:
Color:
blue
orange
medium:
Name:
Cesar:
Color:
yellow
large:
Name:
David:
Color:
purple
What would be the best solution to change this? It is in a pandas dataframe but changing that isn't a problem should there be better solutions.
The close is write DataFrame to yaml.
First create nested dictionaries in dict comprehension:
print (df)
A B C
0 Alice small red
1 Alice small green
2 Bob small blue
3 Bob small orange
4 Cesar medium yellow
5 David large purple
d = {k:v.groupby('A', sort=False)['C'].apply(list).to_dict()
for k, v in df.groupby('B', sort=False)}
print (d)
{'small': {'Alice': ['red', 'green'],
'Bob': ['blue', 'orange']},
'medium': {'Cesar': ['yellow']},
'large': {'David': ['purple']}}
Add size to dict for key and then write to yaml file:
import yaml
with open('result.yml', 'w') as yaml_file:
yaml.dump({'size': d}, yaml_file, default_flow_style=False, sort_keys=False)
size:
small:
Alice:
- red
- green
Bob:
- blue
- orange
medium:
Cesar:
- yellow
large:
David:
- purple
Or create json:
import json
with open("result.json", "w") as twitter_data_file:
json.dump({'size': d}, twitter_data_file, indent=4)
{
"size": {
"small": {
"Alice": [
"red",
"green"
],
"Bob": [
"blue",
"orange"
]
},
"medium": {
"Cesar": [
"yellow"
]
},
"large": {
"David": [
"purple"
]
}
}
}
EDIT:
df = df.assign(A1='Name', B1='size', C1='Color')
df1 = df.groupby(['B1','B','A1','A','C1'], sort=False)['C'].apply(list).reset_index()
#https://stackoverflow.com/a/19900276
def recur_dictify(frame):
if len(frame.columns) == 1:
if frame.values.size == 1: return frame.values[0][0]
return frame.values.squeeze()
grouped = frame.groupby(frame.columns[0], sort=False)
d = {k: recur_dictify(g.iloc[:,1:]) for k,g in grouped}
return d
d = recur_dictify(df1)
print (d)
{'size': {'small': {'Name': {'Alice': {'Color': ['red', 'green']},
'Bob': {'Color': ['blue', 'orange']}}},
'medium': {'Name': {'Cesar': {'Color': ['yellow']}}},
'large': {'Name': {'David': {'Color': ['purple']}}}}}
import yaml
with open('result.yml', 'w') as yaml_file:
yaml.dump(d, yaml_file, default_flow_style=False, sort_keys=False)

How to see if elements in two different lists of tuples match

I have two lists of tuples that I am working with to eventually help me automate processes. I am looking to see how I can properly identify if there are matches in those lists of tuples.
I've already tried to flatten the lists of tuples to make them easier to work with and I'm thinking I want to identify if elements match with boolean statements but I'm not sure how to get the program to run through both lists of tuples.
list1 = [[('1306264304', 'Coca-Cola Zero Sugar Cola, Cans', '1'), ('1176982083', "Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", '1'), ('-975890652', "Ito En Teas\\' Tea, Lemongrass Green, Plastic Bottles", '1'), ('-1152939818', "Ito En Teas\\' Tea, Pure Green, Plastic Bottles", '1'), ('19102859', 'LaCroix Sparkling Water, Coconut, Cans', '1'), ('-546157568', 'LaCroix Sparkling Water, Grapefruit, Cans', '1')]]
and
list2 = [[((beverages)'Coca-Cola Zero Sugar Cola, Cans', 4), ("Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", 3), ("Ito En Teas\\' Tea, Pure Green, Plastic Bottles", 5)]]
If a tuple element in list1 matches with a tuple element in list2, I need it to return true. For example: if 'Coca-Cola Zero Sugar Cola, Cans' appears in both lists, I need to get it to identify that match. I am actually a little lost on how I should code this. I know I need a loop but that's about it. Your help is greatly appreciated.
This should do the trick. The function is pretty well commented within, but essentially, we loop through one array and check each item against the item in the second array. If there is a match, we return true. If not, we return false.
# Create function
def find_match(arr1, arr2):
# Loop through first array
for item in arr1:
# Given the current item, check it against items in other array
for arr2_item in arr2:
# Print what you are comparing
print("array 1 item: ", item, "array 2 item: ", arr2_item)
# If there is a match
if item == arr2_item:
# Return true
return True
# If not
else:
# Keep going
continue
# If you havent returned by this point, it means there is no match
return False
# Main function
def main():
# Example dummy list one
list1 = ['a', 'b', 'c', 'd']
# Example dummy list two
list2 = ['d', 'e', 'f', 'g']
# Call the function
did_it_match = find_match(list1, list2)
# Print the result
print(did_it_match)
# Call main
main()
Note - your arrays you posted don't seem to be formatted correctly so I couldn't use those.
**
** UPDATE PER OP SPECIFIED GUIDELINES **
**
The following code is edited from the other code sample posted but is a working solution with the arrays that OP posted.
def solution(list1, list2):
for index, item in enumerate(list1[0]):
for index, item2 in enumerate(list2[0]):
for item3 in item:
for item4 in item2:
if item3==item4:
return True
return False
def main():
list1 = [[('1306264304', 'Coca-Cola Zero Sugar Cola, Cans', '1'), ('1176982083', "Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", '1'), ('-975890652', "Ito En Teas\\' Tea, Lemongrass Green, Plastic Bottles", '1'), ('-1152939818', "Ito En Teas\\' Tea, Pure Green, Plastic Bottles", '1'), ('19102859', 'LaCroix Sparkling Water, Coconut, Cans', '1'), ('-546157568', 'LaCroix Sparkling Water, Grapefruit, Cans', '1')]]
list2 = [[('Coca-Cola Zero Sugar Cola, Cans', 4), ("Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", 3), ("Ito En Teas\\' Tea, Pure Green, Plastic Bottles", 5)]]
does_match = solution(list1, list2)
print(does_match)
main()
This returns True
Here's what you're looking for:
Since we can assume you have a list inside a list that has tuples inside it, this should do the trick:
def function():
for index, item in enumerate(list1[0]):
for index, item2 in enumerate(list2[0]):
for item3 in item:
for item4 in item2:
if item3==item4:
return True

Comparing like words between two dictionaries

I am using python 3.x,
I have 2 dictionaries (both very large but will substitute here). The values of the dictionaries contain more than one word:
dict_a = {'key1': 'Large left panel', 'key2': 'Orange bear rug', 'key3': 'Luxo jr. lamp'}
dict_a
{'key1': 'Large left panel',
'key2': 'Orange bear rug',
'key3': 'Luxo jr. lamp'}
dict_b = {'keyX': 'titanium panel', 'keyY': 'orange Ball and chain', 'keyZ': 'large bear musket'}
dict_b
{'keyX': 'titanium panel',
'keyY': 'orange Ball and chain',
'keyZ': 'large bear musket'}
I am looking for a way to compare the individual words contained in the values of dict_a to the words contained in the values of dict_b and return a dictionary or data-frame that contains the word, and the keys from dict_a and dict_b it is associated with:
My desired output (not formatted any certain way):
bear: key2 (from dict_a), keyZ(from dict_b)
Luxo: key3
orange: key2 (from dict_a), keyY (from dict_b)
I've got code that works for looking up a specific word in a single dictionary but it's not sufficient for what I need to accomplish here:
def search(myDict, lookup):
aDict = {}
for key, value in myDict.items():
for v in value:
if lookup in v:
aDict[key] = value
return aDict
print (key, value)
dicts = {'a': {'key1': 'Large left panel', 'key2': 'Orange bear rug',
'key3': 'Luxo jr. lamp'},
'b': {'keyX': 'titanium panel', 'keyY': 'orange Ball and chain',
'keyZ': 'large bear musket'} }
from collections import defaultdict
index = defaultdict(list)
for dname, d in dicts.items():
for key, words in d.items():
for word in words.lower().split(): # lower() to make Orange/orange match
index[word].append((dname, key))
index now contains:
{'and' : [('b', 'keyY')],
'ball' : [('b', 'keyY')],
'bear' : [('a', 'key2'), ('b', 'keyZ')],
'chain' : [('b', 'keyY')],
'jr.' : [('a', 'key3')],
'lamp' : [('a', 'key3')],
'large' : [('a', 'key1'), ('b', 'keyZ')],
'left' : [('a', 'key1')],
'luxo' : [('a', 'key3')],
'musket' : [('b', 'keyZ')],
'orange' : [('a', 'key2'), ('b', 'keyY')],
'panel' : [('a', 'key1'), ('b', 'keyX')],
'rug' : [('a', 'key2')],
'titanium': [('b', 'keyX')] }
Update to comments
Since your actual dictionary is a mapping from string to list (and not string to string) change your loops to
for dname, d in dicts.items():
for key, wordlist in d.items(): # changed "words" to "wordlist"
for words in wordlist: # added extra loop to iterate over wordlist
for word in words.split(): # removed .lower() since text is always uppercase
index[word].append((dname, key))
Since your lists have only one item you could just do
for dname, d in dicts.items():
for key, wordlist in d.items():
for word in wordlist[0].split(): # assumes single item list
index[word].append((dname, key))
If you have words that you don't want to be added to your index you can skip adding them to the index:
words_to_skip = {'-', ';', '/', 'AND', 'TO', 'UP', 'WITH', ''}
Then filter them out with
if word in words_to_skip:
continue
I noticed that you have some words surrounded by parenthesis (such as (342) and (221)). If you want to get rid the parenthesis do
if word[0] == '(' and word[-1] == ')':
word = word[1:-1]
Putting this all together we get
words_to_skip = {'-', ';', '/', 'AND', 'TO', 'UP', 'WITH', ''}
for dname, d in dicts.items():
for key, wordlist in d.items():
for word in wordlist[0].split(): # assumes single item list
if word[0] == '(' and word[-1] == ')':
word = word[1:-1] # remove outer parenthesis
if word in words_to_skip: # skip unwanted words
continue
index[word].append((dname, key))
I think you can do what you want pretty easily. This code produces output in the format {word: {key: name_of_dict_the_key_is_in}}:
def search(**dicts):
result = {}
for name, dct in dicts.items():
for key, value in dct.items():
for word in value.split():
result.setdefault(word, {})[key] = name
return result
You call it with the input dictionaries as keyword arguments. The keyword you use for each dictionary will be the string used to describe it in the output dictionary, so use something like search(dict_a=dict_a, dict_b=dict_b).
If your dictionaries might have some of the same keys, this code might not work right, since the keys could collide if they have the same words in their values. You could make the outer dict contain a list of (key, name) tuples, instead of an inner dictionary, I suppose. Just change the assignment line to result.setdefault(word, []).append((key, name)). That would be less handy to search in though.

Groovy sorting string asc

How to sort string names in array on ascending order .
I tried sort method but it fails to sort on name basis .
def words = ["orange", "blue", "apple", "violet", "green"]
I need to achieve like this :
["apple", "blue", "green", "orange", "violet" ]
thanks in advance.
["orange", "blue", "apple", "violet", "green"].sort()
def words = ["orange", "blue", "apple", "violet", "green"]
["orange", "blue", "apple", "violet", "green"].sort({ a, b -> a[0] <=> b[0] } as Comparator )
You can also change the indexes based on the requirement

Resources