set

Python selecting elements in a list by indices

一个人想着一个人 提交于 2020-08-10 04:59:24
问题 I have a list in python that I want to get the a set of indexes out of and save as a subset of the original list: templist = [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]] and I want this: sublist=[[1, 4, 7, 16, 19,20]] as an example. I have no way of knowing ahead of time what the contents of the list elements will be . All I have is the indices that will always be the same. Is there a single line way of doing this? 回答1: Assuming you know what the indices to be selected are, it would

Python selecting elements in a list by indices

元气小坏坏 提交于 2020-08-10 04:55:27
问题 I have a list in python that I want to get the a set of indexes out of and save as a subset of the original list: templist = [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]] and I want this: sublist=[[1, 4, 7, 16, 19,20]] as an example. I have no way of knowing ahead of time what the contents of the list elements will be . All I have is the indices that will always be the same. Is there a single line way of doing this? 回答1: Assuming you know what the indices to be selected are, it would

Python: Calculate difference between all elements in a set of integers

て烟熏妆下的殇ゞ 提交于 2020-07-21 04:40:10
问题 I want to calculate absolute difference between all elements in a set of integers. I am trying to do abs(x-y) where x and y are two elements in the set. I want to do that for all combinations and save the resulting list in a new set. 回答1: I want to calculate absolute difference between all elements in a set of integers (...) and save the resulting list in a new set. You can use itertools.combinations: s = { 1, 4, 7, 9 } { abs(i - j) for i,j in combinations(s, 2) } => set([8, 2, 3, 5, 6])

Python: Calculate difference between all elements in a set of integers

时间秒杀一切 提交于 2020-07-21 04:40:09
问题 I want to calculate absolute difference between all elements in a set of integers. I am trying to do abs(x-y) where x and y are two elements in the set. I want to do that for all combinations and save the resulting list in a new set. 回答1: I want to calculate absolute difference between all elements in a set of integers (...) and save the resulting list in a new set. You can use itertools.combinations: s = { 1, 4, 7, 9 } { abs(i - j) for i,j in combinations(s, 2) } => set([8, 2, 3, 5, 6])

How to sort a set in python? [duplicate]

一世执手 提交于 2020-07-18 22:31:57
问题 This question already has an answer here : Sorting a set of values [closed] (1 answer) Closed last year . I am trying to sort a set in python, by adding elements to it. I tried using the sorted() method, but it is converting my set into a list. I need to sort my elements and print it as a set itself. How can I do that? I tried using the sorted() method and obtained a list. Tried to put the list into a set. But, didn't receive the required result a={} a=set() a.add(1) a.add(-1) a.add(0) a.add

How to sort a set in python? [duplicate]

风格不统一 提交于 2020-07-18 22:31:23
问题 This question already has an answer here : Sorting a set of values [closed] (1 answer) Closed last year . I am trying to sort a set in python, by adding elements to it. I tried using the sorted() method, but it is converting my set into a list. I need to sort my elements and print it as a set itself. How can I do that? I tried using the sorted() method and obtained a list. Tried to put the list into a set. But, didn't receive the required result a={} a=set() a.add(1) a.add(-1) a.add(0) a.add

How to clone or copy a set in Python?

こ雲淡風輕ζ 提交于 2020-07-15 10:06:13
问题 For copying a list: shallow_copy_of_list = old_list[:] . For copying a dict: shallow_copy_of_dict = dict(old_dict) . But for a set , I was worried that a similar thing wouldn't work, because saying new_set = set(old_set) would give a set of a set? But it does work. So I'm posting the question and answer here for reference. In case anyone else has the same confusion. 回答1: Both of these will give a duplicate of a set: shallow_copy_of_set = set(old_set) Or: shallow_copy_of_set = old_set.copy()

How can I sort an ES6 `Set`?

Deadly 提交于 2020-07-02 06:14:14
问题 new Set(['b', 'a', 'c']).sort() throws TypeError: set.sort is not a function . How can I sort a Set to ensure a particular iteration order? 回答1: A set is not an ordered abstract data structure. A Set however always has the same iteration order - element insertion order [1], so when you iterate it (by an iterating method, by calling Symbol.iterator , or by a for.. of loop) you can always expect that. You can always convert the set to an array and sort that. Array.from(new Set(["b","a","c"]))

Why python set displays in “same” order if sets are unordered?

旧街凉风 提交于 2020-07-01 14:38:41
问题 I'm taking a first look at the python language from Python wikibook. For sets the following is mentioned - We can also have a loop move over each of the items in a set. However, since sets are unordered, it is undefined which order the iteration will follow. and the code example given is s = set("blerg") for letter in s: print letter Output : r b e l g When I run the program I get the results in the same order, no matter how many times I run. If sets are unordered and order of iteration is

Find sets that contain at least one element from other sets

可紊 提交于 2020-06-29 05:07:45
问题 Suppose we are given n sets and want to construct all minimal sets that have at least one element in common with each of the input sets. A set S is called minimal, if there is no admissible set S' that is a subset of S . An example: In: s1 = {1, 2, 3}; s2 = {3, 4, 5}; s3 = {5, 6} Out: [{1, 4, 6}, {1, 5}, {2, 4, 6}, {2, 5}, {3, 5}, {3, 6}] My idea was to iteratively add one set after the other to the solution: result = f(s1, f(s2, f(s3, ...))) whereby f is a merge function that could look as