tuples

How to create a tuple from a vector?

不羁的心 提交于 2020-06-27 07:13:33
问题 Here's an example that splits a string and parses each item, putting it into a tuple whose size is known at compile time. use std::str::FromStr; fn main() { let some_str = "123,321,312"; let num_pair_str = some_str.split(',').collect::<Vec<&str>>(); if num_pair_str.len() == 3 { let num_pair: (i32, i32, i32) = ( i32::from_str(num_pair_str[0]).expect("failed to parse number"), i32::from_str(num_pair_str[1]).expect("failed to parse number"), i32::from_str(num_pair_str[2]).expect("failed to parse

Why is a tuple of tuples of length 1 not actually a tuple unless I add a comma?

筅森魡賤 提交于 2020-06-23 08:16:14
问题 Given a tuple of tuples T : (('a', 'b')) and an individual tuple t1 : ('a','b') why does: t1 in T return False? UPDATE: From Ipython: In [22]: T = (('a','b')) In [23]: t1 = ('a','b') In [24]: t1 in T Out[24]: False And how then to check that a tuple is in another tuple? 回答1: The problem is because T is not a tuple of tuples, it is just a tuple. The comma makes a tuple, not the parentheses. Should be: >>> T = (('a','b'),) >>> t1 = ('a', 'b') >>> t1 in T True In fact, you can loose the outer

Why is a tuple of tuples of length 1 not actually a tuple unless I add a comma?

故事扮演 提交于 2020-06-23 08:15:30
问题 Given a tuple of tuples T : (('a', 'b')) and an individual tuple t1 : ('a','b') why does: t1 in T return False? UPDATE: From Ipython: In [22]: T = (('a','b')) In [23]: t1 = ('a','b') In [24]: t1 in T Out[24]: False And how then to check that a tuple is in another tuple? 回答1: The problem is because T is not a tuple of tuples, it is just a tuple. The comma makes a tuple, not the parentheses. Should be: >>> T = (('a','b'),) >>> t1 = ('a', 'b') >>> t1 in T True In fact, you can loose the outer

How to define JSON array with concrete item definition for every index in OpenAPI?

故事扮演 提交于 2020-06-22 04:13:50
问题 I need to define in OpenAPI a JSON response with an array. The array always contains 2 items and the first one is always a number and second one is always a string. [1, "a"] //valid ["a", 1] //invalid [1] //invalid [1, "a", 2] //invalid I've found out that JSON schema does support that by passing a list of items in items instead of single object (source), but OpenAPI explicitly forbids that and accepts only a single object (source). How can that be expressed in OpenAPI? 回答1: OpenAPI 3.1

How to define JSON array with concrete item definition for every index in OpenAPI?

六月ゝ 毕业季﹏ 提交于 2020-06-22 04:13:47
问题 I need to define in OpenAPI a JSON response with an array. The array always contains 2 items and the first one is always a number and second one is always a string. [1, "a"] //valid ["a", 1] //invalid [1] //invalid [1, "a", 2] //invalid I've found out that JSON schema does support that by passing a list of items in items instead of single object (source), but OpenAPI explicitly forbids that and accepts only a single object (source). How can that be expressed in OpenAPI? 回答1: OpenAPI 3.1

How to define JSON array with concrete item definition for every index in OpenAPI?

守給你的承諾、 提交于 2020-06-22 04:13:18
问题 I need to define in OpenAPI a JSON response with an array. The array always contains 2 items and the first one is always a number and second one is always a string. [1, "a"] //valid ["a", 1] //invalid [1] //invalid [1, "a", 2] //invalid I've found out that JSON schema does support that by passing a list of items in items instead of single object (source), but OpenAPI explicitly forbids that and accepts only a single object (source). How can that be expressed in OpenAPI? 回答1: OpenAPI 3.1

How to get Mypy to realize that sorting two ints gives back two ints

人走茶凉 提交于 2020-06-17 01:57:32
问题 My code is as follows: from typing import Tuple a: Tuple[int, int] = tuple(sorted([1, 3])) Mypy tells me: Incompatible types in assignment (expression has type "Tuple[int, ...]", variable has type "Tuple[int, int]") What am I doing wrong? Why can't Mypy figure out that the sorted tuple will give back exactly two integers? 回答1: The call to sorted produces a List[int] which carries no information about length. As such, producing a tuple from it also has no information about the length. The