English 中文(简体)
估计在分部分之后的非零值时,收到0.0
原标题:Receiving 0.0 when expecting a non-zero value after division
  • 时间:2010-10-02 06:23:29
  •  标签:
  • python

Hai... i am facing a problem while inserting values to an array. the programming language using is python. the problem is, i need to insert a value to array after performing a division. but every the value in array is always 0.0 . i am attaching the code here....

print len(extract_list1)
ratio1=range(len(extract_list1))
i=0
for word in extract_list1:
 ratio1[i]=float(i/len(extract_list1))
 print extract_list1[i],ratio1[i]
 i+=1
ratio2=range(len(extract_list2))
i=0
for word in extract_list2:
 ratio2[i]=float(i/len(extract_list2))
 print extract_list2[i],ratio2[i]
 i+=1
最佳回答

我最好的猜测是,你进行分类,然后转换为滚动,产生<代码>0.0>/代码”的价值,而这正是名单上的 st。 您希望转换为,直至>。 穿过该墙,发现

  1. where you are inserting the value into the list.
  2. Where that value is generated.

之后,将产生这一价值的法典放在后面。 如果把这一价值列入清单(而且你似乎表示是),那么,这与名单和你如何创造价值毫无关系。


如Roger Pate在评论中指出的,你可以把“未来——进口司<>代码”作为你的源代码第1行(或如果你使用其中一条编码后第1行),并将自动将所有司改为浮动点。 然后,当你明确要求进行分类时,你可以使用<代码>/。


审视你的法典,问题在于

float(i/len(extract_list2))

这应当

float(i)/len(extract_list2))

此外,由于你正在多度使用<代码>extract_list2和extract_list1,因此最好能创造变数来储存这些变量。

L1 = len(extract_list1)
L2 = len(extract_list2)

只要你不改变名单的长度,你就可以参考变量。 这将节省你们的一些职能,使你的守则更加简明易懂。

问题回答

暂无回答




相关问题
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 ]="...