我想皈依
list_int=['(2,3)','(3,4)']
到整数坐标点:
[(2, 3), (4, 8)]
我试过这段代码
list_int=['(2,3)','(3,4)']
for x in list_int:
con= int(x0 for x in list_int)
print con
我得到了这个错误
TypeError: int()参数必须是字符串或数字,而不是“生成器”
我是Python新手。
我正在为现有的C++库编写一个包装器,它利用了列表,其中T是一个自定义结构。有人建议我使用向量而不是列表,但我试图避免修改库。
为了更好地探索这个场景,我制作了一个简单的应用程序,使用list作为注册到python转换的代理(只读是可以的)。
我的当前实现编译得很好,python很好地导入它,可以创建对象,但是当我调用数据成员时,它会出错。
python shell输出:
In [1]: import my_list
In [2]: x = my_list.Bob()
In [3]: x.foos
----------------------------------------------
也许我对函数定义的cdef理解不正确。例如,假设我想要编写一个函数来将Python列表转换为C数组:
%%cython
cimport cython
from libc.stdlib cimport malloc, free
cdef int* list_to_array(a_list):
""" Converts a Python list into a C array """
cdef int *c_array
cdef int count, n
c_array = <int *>malloc(le
我的任务是编写一个脚本,让用户输入一个数字列表,并让该脚本将该列表中的每个数字相加,以获得总和。我能够写脚本,但是当我输入一个超过一个数字的整数时,我遇到了问题。以下是我的代码和工作脚本:
#!/usr/bin/python
#Sum of integers in a list
#Takes input and removes non-number characters
nList = input("please enter a list of integers separated by commas:\n")
n = ""
numList1 = list
就像在C++中,我如何让用户输入到一个范围内?
下面的是在c++中接受用户输入的代码
#include<iostream>
using namespace std;
int main() {
int array[50];
cin>>n;
//ask a list
for(i = 0; i<n; i++){
cin>>array[i];
}
}
我怎样才能像上面那样在Python 3中询问用户输入?(我不想把输入放在不同的行)
我是Python的新手(我才14岁),我想为一个随机整数列表创建一个气泡分类器。以下是我的代码:
list = input("Please put in a random set of integers, in any order you like (unlimited range), separated by spaces:")
list = list.split()
indexcounter = 0
def sorter(list, indexcounter):
for v in list:
int(v)
while indexcoun
我有一个dataframe,df,其中我希望使用Python对列中的值进行舍入,从floats到int。
数据
location a site b site c site
aa 4.9000 1.72222 0.29999
bb 5.9000 6.72222 0.46999
备注我不想在c站点0.29999上打滚
期望的
location a site b site c site
aa 5 2 1
bb 6
函数def create_list()接受列表中所需的元素数。然后,用户将元素插入到列表中。目标是将整数和浮点数的字符串转换为整数,然后存储到列表中。不能转换的输入被存储为字符串。(如将用户的值命名并插入到列表中)。问题是浮点数仍然保存为字符串。
# create a list with a dynamic size
def create_list(num):
num = int(num)
my_list = []
for i in range(num):
inpt = input(f"enter element {i+1}:")
我正在尝试将我的Cython代码中的所有Python调用转换为纯C,以便能够释放GIL并进行并行化。
我曾经使用从2Dnumpy数组初始化的列表大小为2的列表,所以我这样做了:
cdef double[:,:,:] init=np.random.uniform((10,4,2),dtype=np.float32)
cdef int i
cdef int N=init.shape[0]
for i in range(N):
a=init[i].tolist()
#I then get this list of list
#a=[[1.,1.],[1.,1.],[1.,1.]]
我试图使用input()和Python3.7中的bisect模块的bisect()和insort()函数将元素插入到线性列表中。为了只接受整数输入,我尝试添加一个try- only子句(如对:的答复中所建议的那样):
import bisect
m=[0,1,2,3,4,5,6,7,8,9]
print("The list is:", m)
item=input("Enter element to be inserted:")
try:
item=int(item)
except ValueError:
print("Invalid!
我编写了python代码来编写这样的文件:
with codecs.open("wrtieToThisFile.txt",'w','utf-8') as outputFile:
for k,v in list1:
outputFile.write(k + "\n")
list1是类型(char,int)这里的问题是,当我执行此操作时,文件不会像预期的那样被"\n“分隔。知道这里有什么问题吗?我想是因为
with
任何帮助都是非常感谢的。提前谢谢。(我使用Pyth
我使用boost::python::extract<>将boost::python::list中的项转换为浮点数。我的问题是python中的整数- extract<float>似乎将int->float视为有效的转换,但我只想要真正的float对象。有没有办法迫使extract<>变得更加保守?
extract<float> value(o);
if (value.check()) {
// This is true both for floats and ints
a = value();
}
我正在尝试将列表的python列表转换为cython多维数组。该列表包含300,000个元素,每个元素是一个由10个整数组成的列表。这个例子是随机创建的。我尝试的方式很好,只要我的cython多维数组不大,那么大约是210000。当然,我的实际项目更复杂,但我相信,如果我让这个例子开始工作,剩下的就会更多地相同。
我有一个包含以下内容的cython文件"array_cy.pyx“:
cpdef doublearray(list list1):
cdef int[200000][10] a
cdef int i
cdef int y
cdef int j
如何使用正确的python语法回答这个foor循环问题:
def int_all_2(str_list):
'''(list of str) -> NoneType
Replace every str element of str_list with its corresponding
int version.
For example,
>>> sl = ['100', '222', '2', '34']
>>> in
我使用的似乎就是PyArg_ParseTuple的usgae,但是代码仍然不能工作。我使用的是python 2.7
这是我正在编写的Python扩展的C代码:
static PyObject* tpp(PyObject* self, PyObject* args)
{
PyObject* obj;
PyObject* seq;
int i, len;
PyObject* item;
int arrayValue, temp;
if (!PyArg_ParseTuple(args, "O", &obj)){
printf("Item is not a
我正在使用Python遍历一个巨大的未知数据类型列表(大多数是整数)。因此,为了标准化数据类型,我通常将变量转换为int类型。所以我的问题是,在大型列表的情况下,将int转换为int是否需要额外的时间?更正式地考虑以下代码:
# data is a huge list which consists of int, float etc.
modifieddata=[]
for i in data:
modifieddata.append(int(i)+12)
这里,如果我已经是一个int类型,那么python到底做了什么?它是浪费时间将其转换回int,还是忽略int()语句?
在Python中,您可以为传递给另一个函数(reference)的可调用对象指定参数和返回值。例如: def foo(
bar: Callable[[str],int], # a function that takes a string as an argument and returns an int
baz: List[str] # a list of strings
) -> List[int]: # returns a list of ints
return [bar(s) for s in baz]
foo(len,["hello&
这似乎是一个琐碎的任务,但我仍然没有找到解决办法。
当使用enthought.traits的API并处理它们的数据类型(例如整数Int)时,我如何将这些值键入到HasTraits类中的原生python对象中。例如:
from traits.api import HasTraits, Int, List
class TraitsClass(HasTraits):
test = Int(10)
channel = List(range(0,test)) # this fails as range expects integers
我在类中尝试了以下两种方法,都会产生错误。
test
“我正在尝试触发这个异常,这样当我输入一个字符串而不是int时,我可以看看python是否可以处理。” 我尝试将ValueError语句更改为不同类型的异常,如TypeError,而不是Value Error。我还检查了语法问题。 try:
u_list.append(userInput)
if userInput % 2 == 0:
list_sum += userInput
except ValueError: #this is supposed to be thrown when I put
# a string character instea