English 中文(简体)
压缩案文从阵列起
原标题:Concatenating text string and int from array
  • 时间:2012-01-13 12:11:36
  •  标签:
  • python

权利 我是这方面的一个完整的开端,试图通过各种教程开展工作,其中有许多是巨大的。 然而,他们当中没有任何一种是综合学习方法,其中一种技能建立在另一种技能的基础上。 他们似乎都在一帆风顺。 我试图做的都是用一个阵列的愤怒扼杀。 该守则是:

text =  product_price_ 
numberArray = [1,2,3,4,5,6,7,8,9,10]


for i in numberArray:
  print text + str(numberArray[i])

这些工作给我带来结果:

product_price_2
product_price_3
product_price_4
product_price_5
product_price_6
product_price_7
product_price_8
product_price_9
product_price_10
Traceback (most recent call last):
  File "/Users/me/Documents/Programming/python/eclipse/workspace/concat.py", line 8, in <module>
print text + str(numberArray[i])
IndexError: list index out of range

同我一样,这确实是简单的。 我可以想象,我可以印制一个阵列,但两者都做吗?

任何人能否在我的知识中弥补这一差距?

增 编

最佳回答

<代码>i含有阵列的数值,而不是指数。 因此,如果你想降低价值,就会尝试:

for i in numberArray:
  print text + str(i)
问题回答

Long story short:

text =  product_price 
numberArray = [1,2,3,4,5,6,7,8,9,10]

for num in numberArray:
    print  _ .join((text, str(num)))

Long story long:

- Step 1

你们不应将指数与价值混为一谈。 您举“工作类型”为例,因为您在座标中储存着编号(按序号为list/code>),但是,由于指数计算开始于0<>>,你在后面的封面中找到了<代码>的索引。

这是你的例子。

text =  product_price_ 
numberArray = [1,2,3,4,5,6,7,8,9,10]

for i in numberArray:
    print text + str(i)

- Step 2

您的名单上载有<>条码>的字典,可能更清楚:

>>> text =  product_price  
>>> my_list = [ one ,  two ,  three ]
>>> for price in my_list:
...     print text + price
product_price one
product_price two
product_price three

顺便提一下,从指数中获取价值确实没有必要,因此,您不应做:

>>> text =  product_price  
>>> my_list = [ one ,  two ,  three ]
>>> for i in range(len(my_list)):
...     print text + my_list[i]
product_price one
product_price two
product_price three

- Step 3

最后一个步骤是使用str.join(),在大多数情况下,这将更具有象征意义:

>>> text =  product_price    # without the underscore at the end
>>> numberArray = [1,2,3,4,5,6,7,8,9,10]
>>> for num in numberArray:
...     print  _ .join((text, str(num)))
product_price_1
product_price_2
product_price_3
[...]

您可能希望这一法律能够成为法律。

for i in range(len(numberArray)):
    print text + str( numberArray[i] )

您可使用一个清单,了解:

[text + str(i) for i in numberArray]

甚至更短:

[text + str(i) for i in range(11)]

删除清单(清单,而不是阵列)。

另一种方式是使用<条码>。

map(lambda i: b + str(i), a)

You want to loop on the indeces of numberArray:

for i in xrange(len(numberArray)):
    print text + str(numberArray[i])

之后,即数值0至9,而您的列名指数将不在范围之内。

The function xrange gives you an itearator on the range 0,...,len(numberArray) - 1. Since the length of numberArray is 10 you have an iterator from 0 to 9.

愿你们想要的东西

for i in numberArray:

    print text + str(i)

When using in operator, array elements are cycled through and not indexes. And array indexes start with 0 and not 1. eg

numberArray[0] = 1

numberArray[1] = 2

numberArray[9] = 10

If you insist on using indexes for this example, Your array should change to

<代码>编号Array=[0、1、2、3、4、5、6、7、8、9、10]





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...