从python中的函数返回不同的数据类型

时间:2022-06-16 09:51:17

How do we return a sparse matrix and an array data from a function. My sparse matrix is

我们如何从函数返回稀疏矩阵和数组数据。我的稀疏矩阵是

sparse_mat

sparse_mat

<540x5550 sparse matrix of type '' with 9068 stored elements in COOrdinate format>

<540x5550类型为''的稀疏矩阵,其中包含9068个以COOrdinate格式存储的元素>

and the other data is session_id:

其他数据是session_id:

array([['192.168.113.111_timesofindia.indiatimes.com_1', 'User'],
     ['192.168.113.111_timesofindia.indiatimes.com_2', 'User'],
      dtype='|S46')

I want to return both these data from a function.Thanks

我想从函数中返回这两个数据。谢谢

3 个解决方案

#1


3  

You can return more than one value from a Python function by returning a tuple:

您可以通过返回元组从Python函数返回多个值:

return sparse_mat, session_id

The caller can get the returned value by something like:

调用者可以通过以下方式获取返回值:

m, id = your_function()

Oh, and it doesn't make any difference if there are two different data types. You can return two values of the same type too of course.

哦,如果有两种不同的数据类型,它没有任何区别。当然,您也可以返回两个相同类型的值。

#2


3  

Use a tuple:

使用元组:

 def foo(x, y):
   sum = x + y
   return (x, y, sum)

See? I returned 3 items. And what is more, if I know the airity of the tuple, then I can use an extractor to capture them:

看到?我退回了3件物品。更重要的是,如果我知道元组的空气,那么我可以使用一个提取器来捕获它们:

 (m, n, total) = foo(4, 5)

wherein m is assigned 4, n is assigned 5 and total is assigned 9.

其中m被指定为4,n被指定为5,总数被指定为9。

#3


2  

Examples

例子

def return2():
    a = {1:1}
    b = 'b'
    return a,b

def anotherReturn2():
    a = {1:1}
    b = 'b'
    return {'a' : a, 'b' : b}

a_1, b_1 = return2()
a_2 = return2()[0]
b_2 = return2()[1]
print a_1, b_1, a_2, b_2

a_1, b_1 = anotherReturn2()
a_2 = anotherReturn2()['a']
b_2 = anotherReturn2()['b']
print a_1, b_1, a_2, b_2

>>> 
{1: 1} b {1: 1} b
a b {1: 1} b
>>> 

#1


3  

You can return more than one value from a Python function by returning a tuple:

您可以通过返回元组从Python函数返回多个值:

return sparse_mat, session_id

The caller can get the returned value by something like:

调用者可以通过以下方式获取返回值:

m, id = your_function()

Oh, and it doesn't make any difference if there are two different data types. You can return two values of the same type too of course.

哦,如果有两种不同的数据类型,它没有任何区别。当然,您也可以返回两个相同类型的值。

#2


3  

Use a tuple:

使用元组:

 def foo(x, y):
   sum = x + y
   return (x, y, sum)

See? I returned 3 items. And what is more, if I know the airity of the tuple, then I can use an extractor to capture them:

看到?我退回了3件物品。更重要的是,如果我知道元组的空气,那么我可以使用一个提取器来捕获它们:

 (m, n, total) = foo(4, 5)

wherein m is assigned 4, n is assigned 5 and total is assigned 9.

其中m被指定为4,n被指定为5,总数被指定为9。

#3


2  

Examples

例子

def return2():
    a = {1:1}
    b = 'b'
    return a,b

def anotherReturn2():
    a = {1:1}
    b = 'b'
    return {'a' : a, 'b' : b}

a_1, b_1 = return2()
a_2 = return2()[0]
b_2 = return2()[1]
print a_1, b_1, a_2, b_2

a_1, b_1 = anotherReturn2()
a_2 = anotherReturn2()['a']
b_2 = anotherReturn2()['b']
print a_1, b_1, a_2, b_2

>>> 
{1: 1} b {1: 1} b
a b {1: 1} b
>>>