list-manipulation

Does Linq/.NET3.5 support a 'zip' method?

十年热恋 提交于 2019-12-01 02:15:03
问题 In other languages (ruby, python, ...) I can use zip(list1, list2) which works like this: If list1 is {1,2,3,4} and list2 is {a,b,c} then zip(list1, list2) would return: {(1,a), (2,b), (3,c), (d,null)} Is such a method available in .NET's Linq extensions? 回答1: .NET 4 gives us a Zip method but it is not available in .NET 3.5. If you are curious, Eric Lippert provides an implementation of Zip that you may find useful. 回答2: neither implementation will fill in the missing values (or check that

How to remove every occurrence of sub-list from list

社会主义新天地 提交于 2019-12-01 02:03:10
I have two lists: big_list = [2, 1, 2, 3, 1, 2, 4] sub_list = [1, 2] I want to remove all sub_list occurrences in big_list. result should be [2, 3, 4] For strings you could use this: '2123124'.replace('12', '') But AFAIK this does not work for lists. This is not a duplicate of Removing a sublist from a list since I want to remove all sub-lists from the big-list. In the other question the result should be [5,6,7,1,2,3,4] . Update: For simplicity I took integers in this example. But list items could be arbitrary objects. Update2: if big_list = [1, 2, 1, 2, 1] and sub_list = [1, 2, 1] , I want

In Perl, how can I get the Cartesian product of multiple sets?

心已入冬 提交于 2019-11-29 17:25:55
问题 I want to do permutation in Perl. For example I have three arrays: ["big", "tiny", "small"] and then I have ["red", "yellow", "green"] and also ["apple", "pear", "banana"] . How do I get: ["big", "red", "apple"] ["big", "red", "pear"] ..etc.. ["small", "green", "banana"] I understand this is called permutation. But I am not sure how to do it. Also I don't know how many arrays I can have. There may be three or four, so I don't want to do nested loop. 回答1: That's actually not permutation but

Some built-in to pad a list in python

倾然丶 夕夏残阳落幕 提交于 2019-11-27 06:51:25
I have a list of size < N and I want to pad it up to the size N with a value. Certainly, I can use something like the following, but I feel that there should be something I missed: >>> N = 5 >>> a = [1] >>> map(lambda x, y: y if x is None else x, a, ['']*N) [1, '', '', '', ''] John La Rooy a += [''] * (N - len(a)) or if you don't want to change a in place new_a = a + [''] * (N - len(a)) you can always create a subclass of list and call the method whatever you please class MyList(list): def ljust(self, n, fillvalue=''): return self + [fillvalue] * (n - len(self)) a = MyList(['1']) b = a.ljust(5

How can I get the concatenation of two lists in Python without modifying either one? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-27 05:50:45
This question already has an answer here: How do I concatenate two lists in Python? 25 answers In Python, the only way I can find to concatenate two lists is list.extend , which modifies the first list. Is there any concatenation function that returns its result without modifying its arguments? NPE Yes: list1+list2 . This gives a new list that is the concatenation of list1 and list2 . Depending on how you're going to use it once it's created itertools.chain might be your best bet: >>> import itertools >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = itertools.chain(a, b) This creates a generator

How can I get the concatenation of two lists in Python without modifying either one? [duplicate]

混江龙づ霸主 提交于 2019-11-26 11:46:13
问题 This question already has an answer here: How do I concatenate two lists in Python? 26 answers In Python, the only way I can find to concatenate two lists is list.extend , which modifies the first list. Is there any concatenation function that returns its result without modifying its arguments? 回答1: Yes: list1+list2 . This gives a new list that is the concatenation of list1 and list2 . 回答2: Depending on how you're going to use it once it's created itertools.chain might be your best bet: >>>

Some built-in to pad a list in python

时间秒杀一切 提交于 2019-11-26 09:25:46
问题 I have a list of size < N and I want to pad it up to the size N with a value. Certainly, I can use something like the following, but I feel that there should be something I missed: >>> N = 5 >>> a = [1] >>> map(lambda x, y: y if x is None else x, a, [\'\']*N) [1, \'\', \'\', \'\', \'\'] 回答1: a += [''] * (N - len(a)) or if you don't want to change a in place new_a = a + [''] * (N - len(a)) you can always create a subclass of list and call the method whatever you please class MyList(list): def