[英]Python create dictionary from file
我有此Python代碼未完全讀取我想要的內容。 對於性能最差的汽車,福特應該是數量最多的汽車,但我不斷得到雪佛蘭ssr的結果有什么幫助嗎?
這是我正在使用的文件的格式:
1899 Horsey Horseless
1909 Ford Model T
1911 Overland OctoAuto
1913 Scripps-Booth Bi-Autogo
1920 Briggs and Stratton Flyer
1933 Fuller Dymaxion
1934 Chrysler/Desoto Airflow
1949 Crosley Hotshot
1956 Renault Dauphine
1957 King Midget Model III
這是我的代碼:
import string
def car_dictionary(file_obj):
car_dict = {}
for line in file_obj:
line =line.strip()
line =line.split()
manufacturer = line[1]
if len(line) ==3:
car_tuple = (line[0],''.join(line[2:]))
else:
car_tuple = (line[0])
if manufacturer in car_dict:
car_dict[manufacturer].append(car_tuple)
else:
car_dict[manufacturer] = [car_tuple]
return car_dict
def max_worst_cars(car_dict):
worst_manufacturer =("")
worst_count = 0
for a,b in car_dict.items():
if len(b) > worst_count:
worst_manufacturer = a
worst_count = len(b)
print("Worse Manufacturer is: ", worst_manufacturer)
print("Cars: ")
for y, m in car_dict[worst_manufacturer]:
print(y,m)
file_obj=open("cars.txt", "r")
car_dict = car_dictionary(file_obj)
file_obj.close()
max_worst_cars(car_dict)
除了代碼的錯誤提示(將其粘貼到問題中可能是一個錯誤)之外,它只有一個真正的問題:
car_tuple = (line[0])
這不會構成元組,而只是將line[0]
分配給car_tuple
。 要使其成為元組,您需要添加逗號:
car_tuple = (line[0], )
但是,這不會改變您稍后嘗試拆開元組時遇到的問題,因此您應該僅使用一個空字符串作為第二個元組項:
car_tuple = (line[0], '')
然后,您的代碼將得出正確的結果:
Worse Manufacturer is: Ford
Cars:
1909
1958 Edsel
1971 Pinto
1995 Explorer
2000 Excursion
話雖如此,您可以通過使用其他一些Python技巧來簡化所有操作。 這是我的8行解決方案,外加注釋,以便您了解發生了什么:
# We’re going to use `defaultdict` to handle the whole “when there is
# already an element in the dictionay, append to the list, otherwise
# create a new entry with a single list item” thing. `defaultdict` just
# allows us to append without having to manually initialize new keys.
from collections import defaultdict
# Files should always be opened with the `with` statement, to ensure
# that they are closed correctly afterwards. Since we’re reading, we
# don’t need to specify the open mode ('r' is the default).
with open('cars.txt') as f:
# We initialize our dictionary as a defaultdict, where each item
# is automatically initialized with an empty list.
cars = defaultdict(list)
for line in f:
# We put strip and split in a single line. Since we always
# get at least two values from your file format, we can just
# unpack the values directly. We collect additional unpacked
# values (so index 2 or above) in a list called `rest` (the
# star symbol does that). That list may be empty, or not.
year, manufacturer, *rest = line.strip().split()
# We just append (because it’s a defaultdict, we don’t need
# to initialize it) to the list of the manufacturer a tuple
# with the unpacked year, and the joined values of the rest.
cars[manufacturer].append((year, ' '.join(rest)))
# Now that we collected our cars dictionary, we want to find the
# manufacturer that has the highest number of cars. Since `cars` maps
# the key manufacturer to the a list of cars (car tuples actually), we
# essentially want to get the dictionary item with the maximum length
# in the item value. We use the built-in `max` function with a custom
# key function for this.
# `cars.items()` returns a sequence of key/value tuples of the
# dictionary. We want to get the maximum value of those key/value
# tuples, and as a metric for “maximum value” we use a function that
# takes this tuple `(manufacturer, listOfCarTuples)` and returns the
# length of that `listOfCarTuples`. So in the key function, `x` is that
# tuple, so `x[1]` is the list of car tuples. So the length of that list
# is the metric of the current dictionary item which we want to get the
# maximum from.
worst = max(cars.items(), key=lambda x: len(x[1]))
# `worst` is now `(manufacturer, listOfCarTuples)` where
# `len(listOfCarTuples)` has the maximum value.
print(worst)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.