迭代&将索引值转换为整数并在嵌套列表中浮动

时间:2016-06-13 22:06:57

标签: python iteration

我在迭代下面的嵌套列表表时遇到了困难。我理解如何遍历表一次,但为了更深层次并遍历每个嵌套列表,我坚持使用正确的语法。在迭代子列表时,我试图将每个“年龄”和“年经验”转换为整数,执行操作“年龄” - “年经验”,并将值(作为字符串)附加到每个子列表。 / p>

table = [
    ['first_name', 'last_name', 'age', 'years experience', 'salary'], 
    ['James', 'Butt', '29', '8', '887174.4'], 
    ['Josephine', 'Darakjy', '59', '39', '1051267.9'], 
    ['Art', 'Venere', '22', '2', '47104.2'], 
    ['Lenna', 'Paprocki', '33', '7', '343240.2'], 
    ['Donette', 'Foller', '26', '2', '273541.4'], 
    ['Simona', 'Morasca', '35', '15', '960967.0'], 
    ['Mitsue', 'Tollner', '51', '31', '162776.7'], 
    ['Leota', 'Dilliard', '64', '39', '464595.5'], 
    ['Sage', 'Wieser', '27', '9', '819519.7'], 
    ['Kris', 'Marrier', '59', '33', '327505.55000000005'], 
    ['Minna', 'Amigon', '45', '23', '571227.05'], 
    ['Abel', 'Maclead', '46', '23', '247927.25'], 
    ['Kiley', 'Caldarera', '33', '7', '179182.8'], 
    ['Graciela', 'Ruta', '48', '21', '136978.95'], 
    ['Cammy', 'Albares', '29', '9', '1016378.95'], 
    ['Mattie', 'Poquette', '39', '15', '86458.75'], 
    ['Meaghan', 'Garufi', '21', '3', '260256.5'], 
    ['Gladys', 'Rim', '52', '26', '827390.5'], 
    ['Yuki', 'Whobrey', '32', '10', '652737.0'], 
    ['Fletcher', 'Flosi', '59', '37', '954975.15']]

##Exercise 3 (rows as lists): Iterate over each row and append the following values:
#If it is the first row then extend it with the following ['Started Working', 'Salary / Experience']
#Start work age (age - years experience)
#Salary / Experience ratio  = (salary / divided by experience)


for i, v in enumerate(table):
    extension = ['Started Working', 'Salary/Experience']
    if i == 0: 
        v.extend(extension)
    print(i,v) #test to print out the index and nested list values
    #for index, value in enumerate(v):
       # age = 
        #exp = 
        #start_work = age - exp
        #print(index, value) test to print out the index and each value in the nested list

2 个答案:

答案 0 :(得分:0)

将论据start传递给enumerateenumerate(table, 1)

table = [['first_name', 'last_name', 'age', 'years experience', 'salary'], 
         ['James', 'Butt', '29', '8', '887174.4'], 
         ['Josephine', 'Darakjy', '59', '39', '1051267.9'], 
         ['Art', 'Venere', '22', '2', '47104.2']]

table[0].extend(['Started Working', 'Salary/Experience'])
for idx, row in enumerate(table[1:], 1):
    start_work_age = int(row[2]) - int(row[3])
    ratio  = float(row[4]) / int(row[3])
    table[idx].extend([str(start_work_age), str(ratio)])

print(table)
# Output
[['first_name', 'last_name', 'age', 'years experience', 'salary', 'Started Working', 'Salary/Experience'], 
 ['James', 'Butt', '29', '8', '887174.4', '21', '110896.8'], 
 ['Josephine', 'Darakjy', '59', '39', '1051267.9', '20', '26955.5871795'], 
 ['Art', 'Venere', '22', '2', '47104.2', '20', '23552.1']]

答案 1 :(得分:0)

如果您可以将空格转换为years experience中的下划线,则可以使用collections.namedtuple让您的生活更简单:

from collections import namedtuple

table = [
    ['first_name', 'last_name', 'age', 'years_experience', 'salary'],
    ['James', 'Butt', '29', '8', '887174.4'],
    ['Josephine', 'Darakjy', '59', '39', '1051267.9'],
    ['Art', 'Venere', '22', '2', '47104.2'],
# ...
]

workerv1 = namedtuple('workerv1', ','.join(table[0]))

for i,v in enumerate(table):
    worker = workerv1(*v)
    if i == 0:
        swage = 'Started Working'
        sex_ratio = 'S/Ex ratio'
    else:
        swage = int(worker.age) - int(worker.years_experience)
        sex_ratio = float(worker.salary) / float(worker.years_experience)

    print("{w.first_name},{w.last_name},{w.age},{w.years_experience},{w.salary},{0},{1}".format(
        swage, sex_ratio, w=worker))