iterator

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

泪湿孤枕 提交于 2020-06-07 06:37:26
问题 I need to collect an iterator over a slice of &str s into a collection of &str s. The problem is that the iterator yields &&str s. I tried to map from &word to word , and while it works, I don't know if it is considered good or if there are better options available. The problem: use std::collections::HashSet; fn main() { let words = &["hello", "world", "I'm", "a", "Rustacean!"]; let hashset = words.iter().collect::<HashSet<&str>>(); } Playground error[E0277]: a collection of type `std:

How to iterate pages to scrape web news

落爺英雄遲暮 提交于 2020-06-01 05:12:27
问题 I've been trying to figure out how to iterate pages to scrape multiple news articles. This is the page I want to scrape: (and its following pages) https://www.startribune.com/search/?page=1&q=China%20COVID-19&refresh=true I tried out the below code, but it doesn't return a correct result: def scrap(url): user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko'} urls = [f"{url}{x}" for x in range(1,10)] params = { 'q': 'China%20COVID-19' } for

Printing Min1 and Min2 using Python

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-30 08:05:20
问题 What am I missing in this code here so that it sets min1 and min2 to the two smallest numbers? def test() : # do not change this line! list = [4, 5, 1, 9, -2, 0, 3, -5] # do not change this line! min1 = list[0] min2 = list[1] #missing code here print(min1, min2) return (min1, min2) # do not change this line! # do not write any code below here test() # do not change this line! # do not remove this line! 回答1: List does not sort elements in it implicitly. You have sort the list with sort()

Efficiently chunk large vector into a vector of vectors

我的梦境 提交于 2020-05-29 15:43:10
问题 I want to chunk a large vector into a vector of vectors. I know about chunks() , but am not sure of the best way to go from the iterator to a 2D Vec . I have found the following to work, but is there a better way to write this? let v: Vec<i32> = vec![1, 1, 1, 2, 2, 2, 3, 3, 3]; let v_chunked: Vec<Vec<i32>> = v.chunks(3).map(|x| x.to_vec()).collect(); println!("{:?}", v_chunked); // [[1, 1, 1], [2, 2, 2], [3, 3, 3]] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist

How to mock uuid generation in a test case?

匆匆过客 提交于 2020-05-28 20:51:51
问题 I have a test case in which in the setUp, I create an object for which I want to mock the function uuid4 in the module uuid . TEST_UUIDS = ['uuid_{}'.format(i) for i in range(10000)] UUID_POOL = iter(TEST_UUIDS) def generate_uuid() -> str: return next(UUID_POOL) class MyTestCase(TestCase): def setUp(self, **kwargs): self._create_my_object @patch.object(uuid, 'uuid4', generate_uuid) def _create_my_object(self): # Create an object using a uuid My problem now is that when I run write two test

What is the equivalent of boost::make_transform_iterator in the standard library?

你离开我真会死。 提交于 2020-05-26 10:34:46
问题 When dealing with a const vector, the following doesn't work: const std::vector<std::string> v; v.push_back("test"); // error: v cannot be modified Instead, you have to initialize the vector on the same line where it is constructed. However, even with this restriction, boost::make_transform_iterator makes it easy to do something with another vector's elements before pushing them into v. In this example, convert is a unary function that returns a transformed version of an input element: auto

Is iterating over the same frozenset guaranteed always to produce the items in the same order? (Python 3)

此生再无相见时 提交于 2020-05-23 10:39:20
问题 For example, when I execute frozen = frozenset(('kay', 'snow queen')) , then tuple(frozen) , I get ('kay', 'snow queen') . (When / how) is it possible, if ever, for iter(frozen) to produce the items in a different order? (When / how) will tuple(frozen) return ('snow queen', 'kay') ? I am using Python 3 almost all of the time, but I would also be curious about Python 2. 回答1: By default, the hash values of str objects are salted with an unpredictable random value. Although they remain constant

How do I use __getitem__ and __iter__ and return values from a dictionary?

泄露秘密 提交于 2020-05-22 16:56:35
问题 I have an object with a dictionary that I want to access via __getitem__ as well as iterate over (values only, keys don't matter) but am not sure how to do it. For example: Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10) >>> class Library(object): ... def __init__(self): ... self.books = { 'title' : object, 'title2' : object, 'title3' : object, } ... def __getitem__(self, i): ... return self.books[i] ... >>> library = Library() >>> library['title'] <type 'object'> >>> for book in library: ..

How do I use __getitem__ and __iter__ and return values from a dictionary?

冷暖自知 提交于 2020-05-22 16:54:05
问题 I have an object with a dictionary that I want to access via __getitem__ as well as iterate over (values only, keys don't matter) but am not sure how to do it. For example: Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10) >>> class Library(object): ... def __init__(self): ... self.books = { 'title' : object, 'title2' : object, 'title3' : object, } ... def __getitem__(self, i): ... return self.books[i] ... >>> library = Library() >>> library['title'] <type 'object'> >>> for book in library: ..

algorithm to iterate through fixed size positive integer lists with the same sum

北战南征 提交于 2020-05-17 08:11:08
问题 I am looking for a fast and memory efficient algorithm to iterate through all possible lists of positive integers of the same size (S) with a given sum (N). for example if S = 3 and N = 4 the result would be (i have a really inefficient algo): [0, 0, 4] [0, 1, 3] [0, 2, 2] [0, 3, 1] [0, 4, 0] [1, 0, 3] [1, 1, 2] [1, 2, 1] [1, 3, 0] [2, 0, 2] [2, 1, 1] [2, 2, 0] [3, 0, 1] [3, 1, 0] [4, 0, 0] Not necessarily in that order. Another way to look at it is how to cut the number N into S pieces. The