简体   繁体   English

在python列表中将字符串递增n次

[英]Increment a string n number of times in python list

I have a list like below in python. 我在python中有如下列表。

a = ['191.10', '10.0']

Now I want to create another list like below 现在,我想创建另一个如下所示的列表

b = ['191.10', '10.0', '10.1', '10.2', '10.3' and so on till '10.255']

Basically Increment the second string with 0.1 基本上将第二个字符串增加0.1

How can I achieve that. 我该如何实现。

Here's one way using a list comprehension: 这是使用列表理解的一种方法:

a = ['191.10', '10.0']

b = a + ['10.'+str(i) for i in range(1, 256)]

Result: 结果:

['191.10', '10.0', '10.1', '10.2', ..., '10.254', '10.255']

The most obvious way to accomplish this is probably with the append() statement. 实现此目的最明显的方法可能是使用append()语句。 Also I'm assuming you meant to write "b = ['191.10, '10.0', '10.001', '10.002', '10.003' and so on till '10.255']" here. 另外,我假设您要在"b = ['191.10, '10.0', '10.001', '10.002', '10.003' and so on till '10.255']"编写"b = ['191.10, '10.0', '10.001', '10.002', '10.003' and so on till '10.255']"

 a = ['191.10', '10.0']

 for n in range(1, 255):
      a.append(10 + (0.001*n))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM