Hi. I have to write a bunch of functions that operate on input arrays to return multiple output arrays. In case helpful the inputs are price bars or economic data points [datetime, ohlc] and the outputs are nx1 arrays [I won't say vectors] of doubles or structs. What is the best way to return multiple arrays in this kind of situation. In Python I returned a tuple of numpy arrays, and the C way would be to pass a pointer to the return destinations, and I guess I could do the same in a D way by passing by ref not value]. I see that I can return a struct containing dynamic arrays, and return it [by value of course]. I have read the forum discussion a while back over how to return multiple values, and tried using tuples. I don't see that I can return a tuple of arrays - am I missing something? Here is a simple case. Can I do better ? Thanks. import std.typecons; import std.stdio; struct RetStruct { double[] a; double[] b; } RetStruct myfunction[double x] { RetStruct s; double[] a,b; a~=x+1.0; a~=x+9.0; b~=x+2.0; b~=x+11.0; s.a=a; s.b=b; return s; } void main[] { writefln["%s",myfunction[99.0]]; }
On Wednesday, 15 October 2014 at 16:48:24 UTC, Laeeth Isharc wrote: > Hi. > > I have to write a bunch of functions that operate on input arrays to return multiple output arrays. > > In case helpful the inputs are price bars or economic data points [datetime, ohlc] and the outputs are nx1 arrays [I won't say vectors] of doubles or structs. > > What is the best way to return multiple arrays in this kind of situation. In Python I returned a tuple of numpy arrays, and the C way would be to pass a pointer to the return destinations, and I guess I could do the same in a D way by passing by ref not value]. > > I see that I can return a struct containing dynamic arrays, and return it [by value of course]. I have read the forum discussion a while back over how to return multiple values, and tried using tuples. I don't see that I can return a tuple of arrays - am I missing something? > > Here is a simple case. Can I do better ? > > Thanks. > > > import std.typecons; > import std.stdio; > > struct RetStruct > { > double[] a; > double[] b; > } > > RetStruct myfunction[double x] > { > RetStruct s; > double[] a,b; > a~=x+1.0; > a~=x+9.0; > b~=x+2.0; > b~=x+11.0; > s.a=a; > s.b=b; > return s; > } > > void main[] > { > writefln["%s",myfunction[99.0]]; > } You could also return an std.typecons.Tuple containing both arrays. //dlang.org/phobos/std_typecons.html#.Tuple
Để làm như vậy, hãy trả về cấu trúc dữ liệu chứa nhiều giá trị, chẳng hạn như danh sách chứa số dặm chạy mỗi tuần
def miles_to_run[minimum_miles]:
week_1 = minimum_miles + 2
week_2 = minimum_miles + 4
week_3 = minimum_miles + 6
return [week_1, week_2, week_3]
print[miles_to_run[2]]
# result: [4, 6, 8]
Cấu trúc dữ liệu trong Python được dùng để lưu trữ các tập hợp dữ liệu, có thể được trả về từ các hàm. Trong bài viết này, chúng ta sẽ khám phá cách trả về nhiều giá trị từ các cấu trúc dữ liệu này. bộ dữ liệu, danh sách và từ điển
bộ dữ liệu
Một bộ là một dãy có thứ tự, bất biến. Điều đó có nghĩa là, một tuple không thể thay đổi
Ví dụ, sử dụng một tuple để lưu trữ thông tin về một người. tên, tuổi và vị trí của họ
nancy = ["nancy", 55, "chicago"]
Đây là cách bạn viết một hàm trả về một bộ
def person[]:
return "bob", 32, "boston"
print[person[]]
# result: ['bob', 32, 'boston']
Lưu ý rằng chúng tôi đã không sử dụng dấu ngoặc đơn trong câu lệnh trả về. Đó là bởi vì bạn có thể trả về một bộ bằng cách tách từng mục bằng dấu phẩy, như trong ví dụ trên
“Thực ra dấu phẩy tạo nên một tuple, không phải dấu ngoặc đơn,” tài liệu chỉ ra. Tuy nhiên, dấu ngoặc đơn được yêu cầu với các bộ dữ liệu trống hoặc để tránh nhầm lẫn
Đây là một ví dụ về hàm sử dụng dấu ngoặc đơn
nancy = ["nancy", 55, "chicago"]
0 để trả về một bộ dữ liệudef person[name, age]:
return [name, age]
print[person["henry", 5]]
#result: ['henry', 5]
Danh sách
Một danh sách là một chuỗi có thứ tự, có thể thay đổi. Điều đó có nghĩa là, một danh sách có thể thay đổi
Bạn có thể sử dụng một danh sách để lưu trữ các thành phố
cities = ["Boston", "Chicago", "Jacksonville"]
Hoặc điểm kiểm tra
________số 8Hãy xem chức năng dưới đây. Nó trả về một danh sách chứa mười số
def ten_numbers[]:
numbers = []
for i in range[1, 11]:
numbers.append[i]
return numbers
print[ten_numbers[]]
#result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Đây là một ví dụ khác. Lần này chúng ta truyền vào một số đối số khi chúng ta gọi hàm
On Wednesday, 15 October 2014 at 16:48:24 UTC, Laeeth Isharc wrote: > Hi. > > I have to write a bunch of functions that operate on input arrays to return multiple output arrays. > > In case helpful the inputs are price bars or economic data points [datetime, ohlc] and the outputs are nx1 arrays [I won't say vectors] of doubles or structs. > > What is the best way to return multiple arrays in this kind of situation. In Python I returned a tuple of numpy arrays, and the C way would be to pass a pointer to the return destinations, and I guess I could do the same in a D way by passing by ref not value]. > > I see that I can return a struct containing dynamic arrays, and return it [by value of course]. I have read the forum discussion a while back over how to return multiple values, and tried using tuples. I don't see that I can return a tuple of arrays - am I missing something? > > Here is a simple case. Can I do better ? > > Thanks. > > > import std.typecons; > import std.stdio; > > struct RetStruct > { > double[] a; > double[] b; > } > > RetStruct myfunction[double x] > { > RetStruct s; > double[] a,b; > a~=x+1.0; > a~=x+9.0; > b~=x+2.0; > b~=x+11.0; > s.a=a; > s.b=b; > return s; > } > > void main[] > { > writefln["%s",myfunction[99.0]]; > } You could also return an std.typecons.Tuple containing both arrays. //dlang.org/phobos/std_typecons.html#.Tuple0
Rất dễ nhầm lẫn bộ dữ liệu và danh sách. Xét cho cùng, cả hai đều là các thùng chứa lưu trữ các đối tượng. Tuy nhiên, hãy nhớ những điểm khác biệt chính này
- Tuples không thể thay đổi
- Danh sách có thể thay đổi
từ điển
Từ điển chứa các cặp khóa-giá trị được đặt trong dấu ngoặc nhọn
nancy = ["nancy", 55, "chicago"]
1. Mỗi “khóa” có một “giá trị” liên quan. ”Xem xét từ điển của nhân viên dưới đây. Mỗi tên nhân viên là một “chìa khóa” và vị trí của họ là “giá trị. ”
On Wednesday, 15 October 2014 at 16:48:24 UTC, Laeeth Isharc wrote: > Hi. > > I have to write a bunch of functions that operate on input arrays to return multiple output arrays. > > In case helpful the inputs are price bars or economic data points [datetime, ohlc] and the outputs are nx1 arrays [I won't say vectors] of doubles or structs. > > What is the best way to return multiple arrays in this kind of situation. In Python I returned a tuple of numpy arrays, and the C way would be to pass a pointer to the return destinations, and I guess I could do the same in a D way by passing by ref not value]. > > I see that I can return a struct containing dynamic arrays, and return it [by value of course]. I have read the forum discussion a while back over how to return multiple values, and tried using tuples. I don't see that I can return a tuple of arrays - am I missing something? > > Here is a simple case. Can I do better ? > > Thanks. > > > import std.typecons; > import std.stdio; > > struct RetStruct > { > double[] a; > double[] b; > } > > RetStruct myfunction[double x] > { > RetStruct s; > double[] a,b; > a~=x+1.0; > a~=x+9.0; > b~=x+2.0; > b~=x+11.0; > s.a=a; > s.b=b; > return s; > } > > void main[] > { > writefln["%s",myfunction[99.0]]; > } You could also return an std.typecons.Tuple containing both arrays. //dlang.org/phobos/std_typecons.html#.Tuple2
Đây là cách bạn viết một hàm trả về một từ điển với một cặp khóa, giá trị
On Wednesday, 15 October 2014 at 16:48:24 UTC, Laeeth Isharc wrote: > Hi. > > I have to write a bunch of functions that operate on input arrays to return multiple output arrays. > > In case helpful the inputs are price bars or economic data points [datetime, ohlc] and the outputs are nx1 arrays [I won't say vectors] of doubles or structs. > > What is the best way to return multiple arrays in this kind of situation. In Python I returned a tuple of numpy arrays, and the C way would be to pass a pointer to the return destinations, and I guess I could do the same in a D way by passing by ref not value]. > > I see that I can return a struct containing dynamic arrays, and return it [by value of course]. I have read the forum discussion a while back over how to return multiple values, and tried using tuples. I don't see that I can return a tuple of arrays - am I missing something? > > Here is a simple case. Can I do better ? > > Thanks. > > > import std.typecons; > import std.stdio; > > struct RetStruct > { > double[] a; > double[] b; > } > > RetStruct myfunction[double x] > { > RetStruct s; > double[] a,b; > a~=x+1.0; > a~=x+9.0; > b~=x+2.0; > b~=x+11.0; > s.a=a; > s.b=b; > return s; > } > > void main[] > { > writefln["%s",myfunction[99.0]]; > } You could also return an std.typecons.Tuple containing both arrays. //dlang.org/phobos/std_typecons.html#.Tuple3
Trong ví dụ trên, “Boston” là khóa và “United States” là giá trị.
Chúng tôi đã bao phủ rất nhiều mặt đất. Điểm mấu chốt là đây. bạn có thể trả về nhiều giá trị từ một hàm Python và có một số cách để làm như vậy
Tôi viết về các kỹ năng lập trình bạn cần phát triển và các khái niệm bạn cần học cũng như những cách tốt nhất để học chúng tại amymhaddad. com
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Lập trình viên và nhà văn. cách học hiệu quả. com. công cụ lập kế hoạch hàng ngày. com
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu