我怎样才能增加与循环数组? [英] How can I increment array with loop?

查看:130
本文介绍了我怎样才能增加与循环数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

listOne = ['33.325556', '59.8149016457', '51.1289412359']
listTwo = ['2.5929778', '1.57945488999', '8.57262235411']

我用LEN告诉我有多少项目在我的名单:

I use len to tell me how many items are in my list:

itemsInListOne = int(len(listOne))
itemsInListTwo = int(len(listTwo))

那么,这就是我会被卡住。我想通过使用whil​​e循环,并增加每次循环数组列表循环。这里是我的尝试:

Then, this is where I get stuck. I want to loop through the list using a while loop and increment the array with each loop. Here is what I tried:

iterations = 0

while iterations < itemsInListOne:
        listOne = listOne[0] + 1
        print iterations

while循环显然是错误的,但你可以看到我想要做的事。我想结束了:

The while loop is clearly wrong, but you can see what i'm trying to do. I want to end up with:

listOne[0]
listOne[1]
listOne[2]

先谢谢了。

编辑:为了更好地解释这一点。

To better explain this.

我有一个名单,我想通过列表循环,并自动创建一个在每次迭代增加一个阵列,例如:

I've got a list and I want to loop through the list and automatically create an array which increments with each iteration, for example:

listOne[0] 
+1
listOne[1] 
+ 1 
listOne[2]

希望这会更有意义。

Hope this makes more sense.

推荐答案

您的意思是要做到:

while iterations < itemsInListOne:
    listOne[iterations] = float(listOne[iterations]) + 1

请注意,你需要加入之前将其转换为浮动

Note that you needed to convert it to a float before adding to it.

另外请注意,这可能是与列表COM prehension更容易实现:

Also note that this could be done more easily with a list comprehension:

listOne = [float(x) + 1 for x in listOne]

这篇关于我怎样才能增加与循环数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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