在新的编号列表替换旧号码,并创建一个新的数组 [英] Replacing the old numbers in a list with new numbers and create a new array

查看:104
本文介绍了在新的编号列表替换旧号码,并创建一个新的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import xlrd
fileWorkspace = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(fileWorkspace + 'Book1.xlsx')
sh1 = wb1.sheet_by_index(0)

colA,colB,colC,colD = [],[],[],[]
for a in range(3,sh1.nrows):
    colA.append(int(sh1.cell(a,0).value))
    colB.append(float(sh1.cell(a,1).value))
    colC.append(str(sh1.cell(a,2).value))
    colD.append(str(sh1.cell(a,3).value))
print(colA)
print(colB)   # Need to add 10 if indicated
print(colC)
print(colD)   # Adding 10 is indicated by "add10" 
print('\n')

update_colB_values = []
for i in range(3,sh1.nrows):    
    if sh1.cell(i,3).value == "add10":
        add = (sh1.cell(i,1).value) + 10
        update_colB_values.append(add)
print(update_colB_values)

我越来越为有他们身边ADD10的值的所有新值。我如何创建一个用新的替换它们的旁边是有ADD10这些值的数组?

I am getting all the new values for the values that has "add10" beside them. How do I create an array that replaces those values that had "add10" beside them with the new ones?

最初从Excel文件COLB打印拉:

Initially pulled from the excel file ColB prints:

[32.0, 5.0, 12.0, 1.0, 87.0, 9.0]  # This is fine

更新后,我希望有因为10.0已被添加到他们更新列表的列表。所以,新COLB应该是:

After the update, I want to have a list that updates the list because 10.0 has been added to them. So, the new colB should be:

[42.0, 15.0, 12.0, 1.0, 87.0, 19.0]   # new_colB. This is what I want after the update.

整个输出截至目前:

The whole output as of now:

[0, 1, 2, 3, 4, 5]
[32.0, 5.0, 12.0, 1.0, 87.0, 9.0]
['', '', '', '', '', '']
['add10', 'add10', '', '', '', 'add10']


[42.0, 15.0, 19.0] 

Excel文件图片:
Excel文件

推荐答案

现在你只追加值已更改的更新列表。相反,要附加的所有的值,同时更新需要被加入的值。这里有一个表使用现有的列有清单COM prehension:

Right now you are only appending the values to the updated list that have been changed. Instead, you want to append all values, while updating the values that need to be added to. Here's a form using your existing columns with a list comprehension:

colB = [32.0, 5.0, 12.0, 1.0, 87.0, 9.0]
colD = ['add10', 'add10', '', '', '', 'add10']

update_colB_values = [B + 10 if D == 'add10' else B for B, D in zip(colB, colD)]
# [42.0, 15.0, 12.0, 1.0, 87.0, 19.0]

您应该能够取代您的实例和循环这一行。

You should be able to replace your instantiation and for loop with this line.

应该指出的是,内置的拉链是,你可以在你的前面code到利用非常有用的功能。例如,它的基极的行为是TRANSPOSE(移调)二维列表

It should be noted that the built-in zip is a very useful function that you may be able to utilize in your earlier code. For example, it's base behavior is to "tranpose" a 2D list.

matrix = [['a1', 'a2', 'a3'],
          ['b1', 'b2', 'b3'],
          ['c1', 'c2', 'c3']]

for row in zip(*matrix):
    print(list(row))

# ['a1', 'b1', 'c1']
# ['a2', 'b2', 'c2']
# ['a3', 'b3', 'c3']

功能拉链需要的位置参数的任意号码,所有的第i 元素相匹配在一起。一旦某一行中被耗尽,迭代结束,所以 zip_longest (Python 3中)或 izip_longest (Python的2 )从和itertools 如果你想垫是很有用的。

The function zip takes an arbitrary number of positional arguments and matches all the ith elements together. As soon as one of the rows is exhausted, iteration ends, so zip_longest (Python 3) or izip_longest (Python 2) from itertools is useful if you want to pad.

要做到这一点使用您现有的code,你可以添加一个其他语句,以便您包含的元素没有的需要10个添加到他们。

To do this using your existing code, you can add an else statement so that you include the elements that don't need 10 added to them.

update_colB_values = []
for i in range(3,sh1.nrows):    
    if sh1.cell(i,3).value == "add10":
        add = (sh1.cell(i,1).value) + 10
        update_colB_values.append(add)
    else:                                    # add
        value = sh1.cell(i,1).value          # these
        update_colB_values.append(value)     # lines

这篇关于在新的编号列表替换旧号码,并创建一个新的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆