Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表和字符串的元组_Python_List_Tuples - Fatal编程技术网

Python 列表和字符串的元组

Python 列表和字符串的元组,python,list,tuples,Python,List,Tuples,因此,给定一周中某几天人们偏好列表的随机列表,每个子列表中的7个位置中,第一个位置值6分,第二个5分,第三个4分,第四个4分,第三个2分,第二个1分,最后一个没有任何偏好分: days_week=['mon','tues','wed','thurs','fri','sat','sun'] random_day_list=[['sat','sun','wed','thurs','mon','tues','fri'],['mon','fri','sat','sun','wed','

因此,给定一周中某几天人们偏好列表的随机列表,每个子列表中的7个位置中,第一个位置值6分,第二个5分,第三个4分,第四个4分,第三个2分,第二个1分,最后一个没有任何偏好分:

    days_week=['mon','tues','wed','thurs','fri','sat','sun']

    random_day_list=[['sat','sun','wed','thurs','mon','tues','fri'],['mon','fri','sat','sun','wed','thurs','tues'],['tues','fri','sun','sat','wed','thurs','mon']]
我正在做的函数应该输出一个最高得分日的元组,以及一个以天/周为顺序的每天总分列表。上述结果将是:

    ('sat',[8,7,8,5,10,14])
周六得14分,所以这一天是最受欢迎的一天

到目前为止,我想到了:

   day_dictionary={'mon':0,'tues':0,'wed':0,'thurs':0,'fri':0,'sat':0,'sun':0}
   for i in random_day_list:
         # getting into the sublist
         for j in i:
              if j in day_dictionary:
                 # stuck here:  whatever position j is in (0,1,2,3,4,5,6) if its 
                   0 add 6, 1 add 5, and so on, to day_dictionary[j]

除了数学,我什么都不能导入。我怀疑我需要什么。你需要一些方法来跟踪你的职位。该函数为您实现了这一点

试试这个:

day_dictionary={'mon':0,'tues':0,'wed':0,'thurs':0,'fri':0,'sat':0,'sun':0}
for i in random_day_list:
     for position, day in enumerate(i):
         if day in day_dictionary:
             day_dictionary[day] += (6 - position)

(作为对未来的提醒,最好使用,而不是手动将所有内容初始化为0。但是,由于您无法导入任何内容,您的方式将很好。)

这是一本python书籍中的问题,让我感到困惑,所以我想您的家庭作业可能没有分配,但我只是在做