简体   繁体   English

如果列表中的 0 值相同,有没有办法组合列表的第一个索引?

[英]Is there a way to combine the 1st index of a list if the 0 values in the list are the same?

import csv
import pandas as pd
from matplotlib import pyplot as plt

# Declaring workout data to df variable
df = pd.read_csv('workout.csv')

# Declaring name variable and storing name data into it
name = df['name']

# Calculating the mass moved
moved_mass_data = df['sets'] * df['reps'] * df['weight']

# storing mass moved data into the df data frame
df['mass moved'] = moved_mass_data

# turning moved mass data frame into a list
moved_mass = moved_mass_data.to_list()

# turning exercise name data frame into a list
exercise_name = name.to_list()

# zip names and mass moved together
exercise_moved_mass = zip(exercise_name, moved_mass)

# converting the zip into a list
exercise_moved_mass_list = list(exercise_moved_mass)

# exercise_moved_mass_list prints
[('Facepull', 480), ('Lat Pull Down', 600), ('Dumbbell Bench', 720), ('Dumbbell Bench', 1200), ('Dumbbell Bench', 2880), ('Dips', 3780), ('Incline Fly', 720), ('Incline Fly', 800), ('Incline Fly', 1200), ('Incline Fly', 800)]

#How could I make a function that would turn list to this
[('Facepull', 480), ('Lat Pull Down', 600), ('Dumbbell Bench', 4,800), ('Dips',3780), ('Incline Fly', 2800)]

In the expected output the Incline Fly should be ('Incline Fly',3520), if yes,在预期的 output 中,Incline Fly 应该是('Incline Fly',3520),如果是,

This code should work->这段代码应该可以工作->

b = {}
for i in exercise_moved_mass_list:
  try:
    b[i[0]]+=i[1]
  except:
    b[i[0]]=i[1]

c = [(x, y) for x, y in b.items()]

Output: Output:

[('Facepull', 480),
 ('Lat Pull Down', 600),
 ('Dumbbell Bench', 4800),
 ('Dips', 3780),
 ('Incline Fly', 3520)]

Another method using your dataframe df :使用 dataframe df的另一种方法:

>>> df[["name", "mass moved"]].groupby("name").sum().to_records().tolist()

[('Dips', 3780),
 ('Dumbbell Bench', 4800),
 ('Facepull', 480),
 ('Incline Fly', 3520),
 ('Lat Pull Down', 600)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较列表列表中的第一个元素并写入重复值的数量 - Compare 1st element in list of lists and write number of repeated values 从python dicts列表中取2个键值并制作新的list / tuple / array / dictionary,每个索引包含来自第一个列出的dict的2个键值 - Take 2 key values from list of python dicts & make new list/tuple/array/dictionary with each index containing 2 key values from 1st listed dict 将内部 0 和 1 索引附加到第二索引二维列表列表中的所有元素 - python - Append inner 0 & 1st index to all elements in 2nd index two-dimensional List of Lists - python 列表中的第一个元素和python中相同列表的其他元素的组合 - Making combinations of 1st element in list and other elements of the same list in python 如何将列表列表中与第一个列表中的单词匹配的单词替换为与第二个列表中的第一个单词具有相同位置的单词? - How can I replace a word in my list of lists that matches a word from 1st list into a word that has the same position as the 1st one in the 2nd list? 如何将列表的第一个值更改为浮点型 - How to change the 1st value of a list to a float 将元组返回到字符串中,为什么它只返回元组列表的第一个索引? - return tuple into strings and why is it only returning the 1st index of list of tuple? 如何迭代数据框名称列表以获取列表中每个数据框第一行的第一个值 - How to iterate of a list of dataframe's name to get the 1st value of 1st row of every dataframe in the list for 循环 - 一个列表中的第一个值到第二个列表中的第一个值 - for loop - 1st value in one list to 1st value second list 在列表中获取相同的第一个元素并将其分配为python中列表的第一个元素 - Taking identical 1st element of a list and assigning it as 1st element of the list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM