当前位置: 动力学知识库 > 问答 > 编程问答 >

arrays - Type casting in Python 2.7

问题描述:

How do I cast a float into long in Python 2.7?

I'm doing the same in Python 2.3 like this:

from array import*

data = array('L',[12.34])

print data

which prints out:

array('L',[12L])

How do I do the same in Python 2.7?

网友答案:

Maybe like this?

>>> long(12.34)
12L
网友答案:

These days, it is much more common to see numpy arrays than arrays from the array module, however, your array can be constructed as:

>>> from array import *
>>> array('L',map(long,[12.34]))
array('L', [12L])

with numpy, it could be done as:

>>> import numpy as np
>>> np.array([12.34],dtype=long)

However, this doesn't actually create python longs, it actually creates an array of np.int64 integers (8 byte ints -- Not arbitrary precision like python long).

网友答案:
lst = [1.1,2.2]
data = map(long,lst)
分享给朋友:
您可能感兴趣的文章:
随机阅读: