Obtaining values from columns in python

2024/11/19 18:21:56

I want to obtain the top 3 cities and items based on their sales, but the only thing I can do now is return the all cities and items with their respective sales. Without using dict, can I obtain my desired output? Or if I use dict, how do I obtain the desired output?

purchases.txt

2012-01-01  09:00   San Jose    Men's Clothing  214.05  Amex
2012-01-01  09:00   Fort Worth  Women's Clothing    153.57  Visa
2012-01-01  09:00   San Diego   Music   66.08   Cash
2012-01-01  09:00   Pittsburgh  Pet Supplies    493.51  Discover
2012-01-01  09:00   Omaha   Children's Clothing 235.63  MasterCard
2012-01-01  09:00   Stockton    Men's Clothing  247.18  MasterCard
2012-01-01  09:00   Austin  Cameras 379.6   Visa
2012-01-01  09:00   New York    Consumer Electronics    296.8   Cash
2012-01-01  09:00   Corpus Christi  Toys    25.38   Discover
2012-01-01  09:00   Fort Worth  Toys    213.88  Visa

test.py

    f = open ("purchases.txt")def separator():str = ("="*48)print (str)return;city_seen = set()item_seen = set()citysaleslist = []itemsaleslist= []for line in open(sys.argv[1]):sales=float(line.split()[-2]) strsales=line.split()[-2]city=line.split('\t')[2] item=line.split('\t')[3]if city not in city_seen: # if city is not a duplicate, add to city_seen setcity_seen.add(city)#Pressing tab for the bottom 2 lines will remove duplicate but combining the sales for the duplicates is impossible here.citysales="{0:<29}{1:>18}".format(city,strsales)citysaleslist.append(citysales)if item not in item_seen: # if item is not a duplicate, add to item_seen setitem_seen.add(item)#Pressing tab for the bottom 2 lines will remove duplicate but combining the sales for the duplicates is impossible here.itemsales = "{0:<29}{1:>18}".format(item,strsales)itemsaleslist.append(itemsales)print("Top Three Cities \n")separator()for i in citysaleslist:print(i)separator()print("Bottom Three Cities \n")separator()separator()print("Top Three Item Categories")separator()for i in itemsaleslist:print(i)separator()print("\nBottom Three Item Categories")separator()separator()      

My output:

Top Three Cities ================================================
San Jose                                 214.05
Fort Worth                               153.57
San Diego                                 66.08
Pittsburgh                               493.51
Omaha                                    235.63
Stockton                                 247.18
Austin                                    379.6
New York                                  296.8
Corpus Christi                            25.38
Fort Worth                               213.88
================================================
Bottom Three Cities ================================================
================================================Top Three Item Categories
================================================
Men's Clothing                           214.05
Women's Clothing                         153.57
Music                                     66.08
Pet Supplies                             493.51
Children's Clothing                      235.63
Men's Clothing                           247.18
Cameras                                   379.6
Consumer Electronics                      296.8
Toys                                      25.38
Toys                                     213.88
================================================Bottom Three Item Categories
================================================
================================================

Desired output:

Top Three Cities ================================================
Pittsburgh                               493.51
Austin                                   379.60
Fort Worth                               367.45
================================================
Bottom Three Cities ================================================
Omaha                                     235.63
San Jose                                  214.05
San Diego                                  66.08
================================================Top Three Item Categories
================================================
Pet Supplies                             493.51
Men's Clothing                           461.23
Cameras                                   379.6
================================================Bottom Three Item Categories
================================================
Toys                                      239.26
Children's Clothing                       235.63
Women's Clothing                          153.57
================================================
Answer

You need to sort your data. Once you sort your data, you can print out the data as you desire.

#brief example (but not a direct solution!)
aList = [ ["citya","22"]   \["cityc","44"]   \["cityb","55"]   \ ]
aSortedList = sorted(alist, key=lamda x:x[1])
#now pick how you want to get your information from the sorted list
#hint you have already read the information. 

But you do NOT have your data linked directly. So either create a new list with all the information OR track the information as you go.

Part of the issue is that you are trying to do everything immediately as you read the file. The data read has NOT been sifted through. No sorting, only read. Finding minimums and maximums do NOT need to be sorted, but then you'll need to TRACK the information.

if (new_value > max1):max3 = max2max2 = max1max1 = new_valueif (new_value > max2 and new_value < max1):...

and you'll have to loop through it all.

Tracking is good if the information is given all at once. But if the information changes, referencing the data might be easier later.

https://en.xdnf.cn/q/119916.html

Related Q&A

Is there a really efficient (FAST) way to read large text files in python?

I am looking to open and fetch data from a large text file in python as fast as possible (It almost has 62603143 lines - size 550MB). As I dont want to stress my computer, I am doing it by following wa…

How to extract all K*K submatrix of matrix with or without NumPy?

This is my input: row=6 col=9 6 9 s b k g s y w g f r g y e q j j a s s m s a s z s l e u s q u e h s s s g s f h s s e s g x d r h g y s s sThis is my code: r=int(input()) c=int(input()) n=min(r,c) k=…

How to scrape multiple result having same tags and class

My code is accurate for single page but when I run this code for multiple records using for loop and if there are some data missing like person then (as I used index no[1] and [2] for person variable ,…

Is there an alternative for sys.exit() in python?

try:x="blaabla"y="nnlfa" if x!=y:sys.exit()else:print("Error!") except Exception:print(Exception)Im not asking about why it is throwing an error. I know that it raises e…

Adding items to Listbox in Python Tkinter

I would like my Listbox widget to be updated upon clicking of a button. However I encountered a logic error. When I click on the button, nothing happens. No errors at all.listOfCompanies: [[1, ], [2, -…

Policy based design in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Exception raised: cannot import name manual_seed from torch

im trying to run the AutoClean project on my device (heres my code): import random from AutoClean import AutoClean import pandas as pddef __init__(self, pipeline, resultat ):self.pipeline = pipelinesel…

Compile a C/C++ Program and store standard output in a File via Python

Lets say I have a C/C++ file named userfile.c. Using Python, how can I invoke the local gcc compiler so that the file is compiled and an executable is made? More specifically, I would like to provide …

How to swap maximums with the minimums? (python)

Is there a method to swap the maximum and the minimum of a list? The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second …

python object attributes and methods

In python all data is object and any object should have attributes and methods. Does somebody know python object without any attributes and methods?>>> len(dir(1)) 64