gpt4 book ai didi

python - python 将列表中的元素排序为3个单独的列表

转载 作者:行者123 更新时间:2023-11-30 22:08:17 29 4
gpt4 key购买 nike

让我从代码开始,解释一下目标和我得到的结果。

temp1 = ['3.8', 'Weiss, Earl', '139 RATINGS', '2.3', 'Jeppson, Catherine', '114 RATINGS', '3.3', 'Kiani-Aslani, Rajabali', '88 RATINGS', '2.6', 'Lundblad, Heidemarie', '82 RATINGS', '2.4', 'Stone, Ronald', '75 RATINGS', '3.7', 'Vedd, Rishma', '66 RATINGS', '3.3', 'Foster, Robert', '60 RATINGS', '4.9', 'Basmadzhyan, Babken', '59 RATINGS', '4.3', 'Grodsky, Marilyn', '57 RATINGS', '2.4', 'Dorsey, Norris', '53 RATINGS', '2.6', 'Zvinakis, Kristina', '51 RATINGS', '3.2', 'MacKlin, James', '50 RATINGS', '2.8', 'Liu, David', '48 RATINGS', '3.2', 'Doron, Michael', '48 RATINGS', '2.1', 'Rogoff, Donald', '45 RATINGS', '3.1', 'Sangeladji, Mohammad', '43 RATINGS', '4.0', 'Fountaine, Howard', '42 RATINGS', '4.6', 'Stout, Gary', '41 RATINGS', '3.4', 'Gray, Glen', '34 RATINGS', '3.0', 'Wilson, Barbara', '31 RATINGS', '4.0', 'Yoon, Sung-Wook', '31 RATINGS', '4.5', 'Her, Young-Won', '31 RATINGS', '3.0', 'Kiddoo, Robert', '30 RATINGS', '3.0', 'Chiu, J', '27 RATINGS', '3.3', 'Barker, Robert', '25 RATINGS', '3.7', 'Qureshi, Mahmood', '23 RATINGS', '3.7', 'Primes, David', '22 RATINGS', '2.6', 'Chen, Raymond', '20 RATINGS', '3.3', 'Jones, Christopher', '20 RATINGS', '3.2', 'Zhan, Jun', '20 RATINGS', '4.6', 'Bell, Janice', '15 RATINGS', '3.8', 'Alhashim, Dhia D', '12 RATINGS', '2.9', 'Ansari, Shahid', '11 RATINGS', '4.5', 'Rousselet, Robin (rob)', '9 RATINGS', '2.4', 'Lucero, Terrence', '8 RATINGS', '1.0', 'Perez, Marlene', '7 RATINGS', '1.3', 'Crespo, Patricia', '7 RATINGS', '4.8', 'Knight, Ridgeway', '7 RATINGS', '2.5', 'Julius, Ed', '6 RATINGS', '2.9', 'Reinstein, Todd', '6 RATINGS']

所以我的目标是将这个巨大的列表分为 3 个不同的分类列表,

教授姓名、教授评分和评分数量

我已经使用以下 if 语句开发了以下 for 循环,并且尝试使用它至少有一个不起作用,让我在下面的代码中向您展示

counter = 1
for index in temp1:
if counter % 1 == 0:
pro_rating.append(index)
if counter % 2 == 0:
pro_name.append(index)
if counter % 3 == 0:
pro_amount_rating.append(index)
counter = 0
counter += 1

print("All Professor ratings: ", pro_rating)
print("All professor names: ", pro_name)
print("Amount of times professor rated: ", pro_amount_rating)

现在,在附加名称 (pro_names) 和评级数量 (pro_amount_ rating) 时,一切都很顺利,但 pro_ rating 总是写出完整的列表。

我完全理解为什么会发生这种情况,因为一旦计数器达到 3,我就会重置计数器,并且计数器在最后加 1,使第一个 if 语句始终为 true。

我正在考虑放置一个标志或第二个参数来解决这个问题,但我似乎无法弄清楚,我知道我可以轻松地创建另一个 forloop 来解决这个问题,但我想把它们全部得到在这个单个 for 循环中完成。

如果有人有任何想法,我将不胜感激!

输出:

All Professor ratings:  ['3.8', 'Weiss, Earl', '139 RATINGS', '2.3', 'Jeppson, Catherine', '114 RATINGS', '3.3', 'Kiani-Aslani, Rajabali', '88 RATINGS', '2.6', 'Lundblad, Heidemarie', '82 RATINGS', '2.4', 'Stone, Ronald', '75 RATINGS', '3.7', 'Vedd, Rishma', '66 RATINGS', '3.3', 'Foster, Robert', '60 RATINGS', '4.9', 'Basmadzhyan, Babken', '59 RATINGS', '4.3', 'Grodsky, Marilyn', '57 RATINGS', '2.4', 'Dorsey, Norris', '53 RATINGS', '2.6', 'Zvinakis, Kristina', '51 RATINGS', '3.2', 'MacKlin, James', '50 RATINGS', '2.8', 'Liu, David', '48 RATINGS', '3.2', 'Doron, Michael', '48 RATINGS', '2.1', 'Rogoff, Donald', '45 RATINGS', '3.1', 'Sangeladji, Mohammad', '43 RATINGS', '4.0', 'Fountaine, Howard', '42 RATINGS', '4.6', 'Stout, Gary', '41 RATINGS', '3.4', 'Gray, Glen', '34 RATINGS', '3.0', 'Wilson, Barbara', '31 RATINGS', '4.0', 'Yoon, Sung-Wook', '31 RATINGS', '4.5', 'Her, Young-Won', '31 RATINGS', '3.0', 'Kiddoo, Robert', '30 RATINGS', '3.0', 'Chiu, J', '27 RATINGS', '3.3', 'Barker, Robert', '25 RATINGS', '3.7', 'Qureshi, Mahmood', '23 RATINGS', '3.7', 'Primes, David', '22 RATINGS', '2.6', 'Chen, Raymond', '20 RATINGS', '3.3', 'Jones, Christopher', '20 RATINGS', '3.2', 'Zhan, Jun', '20 RATINGS', '4.6', 'Bell, Janice', '15 RATINGS', '3.8', 'Alhashim, Dhia D', '12 RATINGS', '2.9', 'Ansari, Shahid', '11 RATINGS', '4.5', 'Rousselet, Robin (rob)', '9 RATINGS', '2.4', 'Lucero, Terrence', '8 RATINGS', '1.0', 'Perez, Marlene', '7 RATINGS', '1.3', 'Crespo, Patricia', '7 RATINGS', '4.8', 'Knight, Ridgeway', '7 RATINGS', '2.5', 'Julius, Ed', '6 RATINGS', '2.9', 'Reinstein, Todd', '6 RATINGS']
All professor names: ['Weiss, Earl', 'Jeppson, Catherine', 'Kiani-Aslani, Rajabali', 'Lundblad, Heidemarie', 'Stone, Ronald', 'Vedd, Rishma', 'Foster, Robert', 'Basmadzhyan, Babken', 'Grodsky, Marilyn', 'Dorsey, Norris', 'Zvinakis, Kristina', 'MacKlin, James', 'Liu, David', 'Doron, Michael', 'Rogoff, Donald', 'Sangeladji, Mohammad', 'Fountaine, Howard', 'Stout, Gary', 'Gray, Glen', 'Wilson, Barbara', 'Yoon, Sung-Wook', 'Her, Young-Won', 'Kiddoo, Robert', 'Chiu, J', 'Barker, Robert', 'Qureshi, Mahmood', 'Primes, David', 'Chen, Raymond', 'Jones, Christopher', 'Zhan, Jun', 'Bell, Janice', 'Alhashim, Dhia D', 'Ansari, Shahid', 'Rousselet, Robin (rob)', 'Lucero, Terrence', 'Perez, Marlene', 'Crespo, Patricia', 'Knight, Ridgeway', 'Julius, Ed', 'Reinstein, Todd']
Amount of times professor rated: ['139 RATINGS', '114 RATINGS', '88 RATINGS', '82 RATINGS', '75 RATINGS', '66 RATINGS', '60 RATINGS', '59 RATINGS', '57 RATINGS', '53 RATINGS', '51 RATINGS', '50 RATINGS', '48 RATINGS', '48 RATINGS', '45 RATINGS', '43 RATINGS', '42 RATINGS', '41 RATINGS', '34 RATINGS', '31 RATINGS', '31 RATINGS', '31 RATINGS', '30 RATINGS', '27 RATINGS', '25 RATINGS', '23 RATINGS', '22 RATINGS', '20 RATINGS', '20 RATINGS', '20 RATINGS', '15 RATINGS', '12 RATINGS', '11 RATINGS', '9 RATINGS', '8 RATINGS', '7 RATINGS', '7 RATINGS', '7 RATINGS', '6 RATINGS', '6 RATINGS']

已解决:谢谢迈克尔的解决方案,我显然想太多了,使用一个简单的counter == 1, 2...解决了问题,而不是使用模数再次感谢

最佳答案

您可以使用列表切片以更少的代码实现您想要的效果。

pro_rating        = temp1[0::3]
pro_name = temp1[1::3]
pro_amount_rating = temp1[2::3]

这会将第一个元素排序为评级,第二个元素排序为名称,第三个元素排序为金额,每个第三个元素重复一次。

关于python - python 将列表中的元素排序为3个单独的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213678/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com