我将采用4列的csv:品牌,价格,重量和类型.
类型有橙色,苹果,梨,李子.
参数:我需要选择最可能的重量,但选择1个橙子,2个梨子,3个苹果和1个李子,不超过20美元的预算.我不能重复相同水果的品牌(比如选择同一品牌的苹果3次等).
我可以通过Python打开并读取csv文件,但我不确定如何从csv文件创建字典或元组列表?
为了更清楚,这里是数据的概念.
Brand, Price, Weight, Type
brand1, 6.05, 3.2, orange
brand2, 8.05, 5.2, orange
brand3, 6.54, 4.2, orange
brand1, 6.05, 3.2, pear
brand2, 7.05, 3.6, pear
brand3, 7.45, 3.9, pear
brand1, 5.45, 2.7, apple
brand2, 6.05, 3.2, apple
brand3, 6.43, 3.5, apple
brand4, 7.05, 3.9, apple
brand1, 8.05, 4.2, plum
brand2, 3.05, 2.2, plum
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的一切:
import csv
test_file = 'testallpos.csv'
csv_file = csv.DictReader(open(test_file, 'rb'), ["brand"], ["price"], ["weight"], ["type"])
Run Code Online (Sandbox Code Playgroud) 我目前有这个代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a;
cout << "Enter a number for a: "; //Prompts the user for a and b inputs
cin >> a;
int b;
cout << "Enter a number for b: ";
cin >> b;
cout << "A is " << a << "\tB is " << b << end1;
cout <<"Sum of a and b is equal to " << a << "+" << b << "and the result …Run Code Online (Sandbox Code Playgroud)