I am new to python and am looking to analyze the S&P500 by sector. I have assigned symbols to all 11 sectors in the S&P with the first two looking like:
Financials = ['AFL', 'AIG', .... 'ZION']
Energy = ['APA', 'BKR', ... 'SLB']
I then create a new list (of lists) which might look like:
sectors_to_analyze = [Financials, Energy] or [Materials, ConsumerStaples]
My analysis is working perfectly, but I want to retrieve the names "Financials" and "Energy" to attach to the data produced and I cannot figure out how to do it other than make the name part of the list (Financials = ['Financials','AFL', 'AIG', .... 'ZION']
Can someone please point me in the right direction? Thank you.
Perhaps you could use a dictionary
sectors = {
'Financials':['AFL', ...],
# rest of your lists
}
Then you can iterate over the whole dict and access both names and data associated with those names
for key, value in sectors.items():
print(f'Sector name: {key}, List: {value}')
I think you want to use a dictionary instead of a "list of lists" (also called a two dimensional list). You could then loop over the dictionary almost the same way. Here's some example code:
Financials = ['AFL', 'AIG', 'ZION']
Energy = ['APA', 'BKR', 'SLB']
sectors = {"Finacials": Financials, "Energy": Energy}
# in this loop, sector is the sector's name, and symbols is the sector's
# list
for sector in sectors:
symbols = sectors[sector]
# ...
# do some analysis
# ...
Related
I am currently writing a lambda function (python 3.9) with the objective of finding students who are celebrating holiday in their respective country. To start off, I have a student.json file and a folder containing all the countries' holiday in the holiday_folder in my S3 bucket:
(main directory)
student.json
holiday_folder
(within holiday_folder)
holiday_us.json
holiday_cn.json
holiday_my.json
holiday_th.json
the list goes on...
The student.json file has the following content:
{'student': [{'name':'Adam', 'location':'US'}, {'name':'Ben', 'location':'SG'}...]}
While all the holiday json files are structured similarly as follow:
{ "holiday_us":[ { "date":"20220101", "desc":"New Year's Day" }, {"date"....}]}
To achieve my objective, I:
json.loads student.json content into variable student
Create a list called countries and run a for loop to append only the "location" values of all students in student variable
Create another list called holiday_calendar to store transformed location values a.k.a. transforming ['US', 'SG', ...] into ['holiday_us', 'holiday_sg' ...] so that I can convert these list values into variable names to load holiday dates
Run a conditional for loop on student. The loop will look into the holiday list that's only relevant to the specific student and return value accordingly. I wont be going deep on this as this is not the point of the question.
countries = []
holiday_calendar = []
for i in range(len(student)):
if student[i]['location'] not in countries:
countries.append(student[i]['location'])
holiday_calendar.append('holiday_' + student[i]['location'].lower())
print(holiday_calendar) # output: ['holiday_us', 'holiday_sg'...]
for i in range(0, len(holiday_calendar)):
print(holiday_calendar[i])
locals[holiday_calendar[i]] = json.loads(s3.Object('my_s3_bucket', 'holiday_folder' + holiday_calendar[i] + '.json')).get()['Body'].read())
My issue is that I cannot convert the holiday_calendar list values into variables. When I ran the above code, I got this error:
"errorMessage": "'builtin_function_or_method' object does not support item assignment",
"errorType": "TypeError"
I am pretty sure the error came from the globals() line as I have ran all the other parts of the code and they all worked flawlessly. I have also tried exec() and locals() as per suggested in a lot of SO topics but none worked. Any suggestions?
I have created a class(City) with several parameters-- name, latitude, longitude, country_name, city_population, population_ratio. population_ratio is created by dividing the City's population by the population of its country (extracted from another class(Country)). The City class has three registries:
_registry_instances = list()
_registry = dict() ##key = name, value = instance
_registry_names = list() #list of city names
I need to make a class method that returns a tuple of the name of the city with the highest population_ratio and the city name. This is what I have:
def cityWithHighestPopulationRatio(City):
## create empty list
cityList = list()
## create nested lists of population ratios and names
## I've tried a variety of ways to code this including for loops and if statements
cityList.append([City._registry[population_ratio], City._registry[name]])
## assign city with largest ratio to a tuple
return (max(cityList))
No matter how I go about it, I keep getting the error: "NameError: name 'population_ratio' is not defined"
Does anyone have any ideas how to make this work?
Thank you in advance!
I figured it out!!
def cityWithHighestPopulationRatio(City):
cityList = list()
## create nested lists of population ratios and names
for x in City._registry_instances:
cityList.append([x.population_ratio, x.name])
## assign city with largest ratio to a tuple
return (max(cityList))
High fives all around! Good work everyone!
I'm having some trouble figuring out the best implementation
I have data in file in this format:
|serial #|machine_name|machine_owner|
If a machine_owner has multiple machines, I'd like the machines displayed in a comma separated list in the field. so that.
|1234|Fred Flinstone|mach1|
|5678|Barney Rubble|mach2|
|1313|Barney Rubble|mach3|
|3838|Barney Rubble|mach4|
|1212|Betty Rubble|mach5|
Looks like this:
|Fred Flinstone|mach1|
|Barney Rubble|mach2,mach3,mach4|
|Betty Rubble|mach5|
Any hints on how to approach this would be appreciated.
You can use dict as temporary container to group by name and then print it in desired format:
import re
s = """|1234|Fred Flinstone|mach1|
|5678|Barney Rubble|mach2|
|1313|Barney Rubble||mach3|
|3838|Barney Rubble||mach4|
|1212|Betty Rubble|mach5|"""
results = {}
for line in s.splitlines():
_, name, mach = re.split(r"\|+", line.strip("|"))
if name in results:
results[name].append(mach)
else:
results[name] = [mach]
for name, mach in results.items():
print(f"|{name}|{','.join(mach)}|")
You need to store all the machines names in a list. And every time you want to append a machine name, you run a function to make sure that the name is not already in the list, so that it will not put it again in the list.
After storing them in an array called data. Iterate over the names. And use this function:
data[i] .append( [ ] )
To add a list after each machine name stored in the i'th place.
Once your done, iterate over the names and find them in in the file, then append the owner.
All of this can be done in 2 steps.
My list comprehension returns the error "List index out of range" if the value does not exist in the list.
Objective:
Check a three-letter code for a country given by a variable (country) and transform the code into a two-letter code by looking up a list of tuples (COUNTRIES)
Constant:
# Two letter code, three letter code, full name
COUNTRIES = [
('US', 'USA', 'United States'),
('DE', 'DEU', 'Germany'),
....
]
Code:
country = 'EUR'
# Check if code in country has 3 characters (I have multiple checks for two letter codes too) and is not None
if len(country) == 3 and country is not None:
country = [code2 for code2, code3, name in COUNTRIES if code3 == country][0]
If I only include a list with three letter codes USA and DEU, the code works fine. If I add the fictitious code "EUR", which is not a valid country code in the variable "country", than I get the List index out of range error.
How can I return None instead of breaking the program? The variable country will be used later on again.
I don't think List comprehensions are a good choice here. They are good when you want to turn one list into another list, which you don't really want to do here. A better approach would be a regular for loop with a return here.
However, my personal approach would be to transform the list lookups into dict lookups instead:
COUNTRIES_LUT = {}
for code2, code3, country in COUNTRIES:
COUNTRIES_LUT[code2] = country
COUNTRIES_LUT[code3] = country
At the end of that, you can just use COUNTRIES_LUT[your_str] as expected.
If you generate this lookuptable at the start, this also has the bonus of being faster, since you don't need to loop through every element of the list every time.
I have written a code that looks through a transaction list and sees if a company is buying or selling and converts the value to USD and then returns it. the problem is my original list looks like this:
[['Acer', 481242.74], ['Beko', 966071.86], ['Cemex', 187242.16], ['Datsun', 748502.91], ['Equifax', 146517.59], ['Gerdau', 898579.89], ['Haribo', 265333.85]]
and when i run my code it iterates through about 100 transactions, and after each transaction returns a sample like the following:
['Acer', 21439.6892]
now my problem is i want to update the value in the original list with this new value so that it would still have the company name but the two values behind it would be added and the new value would appear in the original list for the next iteration. so 481242.74+21439.6892=502682.4292 so the new original list would look like the following with the acer value updated
[['Acer',502682.4292 ], ['Beko', 966071.86], ['Cemex', 187242.16], ['Datsun', 748502.91], ['Equifax', 146517.59], ['Gerdau', 898579.89], ['Haribo', 265333.85]]
You should use a dictionary instead to order your companies
d ={'Acer': 481242.74,'Beko': 966071.86, 'Cemex': 187242.16, 'Datsun': 748502.91, 'Equifax': 146517.59, 'Gerdau': 898579.89, 'Haribo': 265333.85}
for k,v in d.items():
d[k] = v+sample[1]
Use a dictionary instead of a list.
#Make the master data structure a Dictionary
myDict = {'Acer':float(481242.74) }
print myDict
#iterate over the newList and update the master dictionary
newList = [['Acer', float(502682.4292)]]
for Company, NewValue in newList:
myDict[Company]=(myDict[Company]+NewValue)
print myDict