list

How to reverse a list of lists in python? [duplicate]

纵然是瞬间 提交于 2021-02-16 15:08:52
问题 This question already has answers here : How can I reverse a list in Python? (33 answers) Closed 5 years ago . I was wondering how to reverse a list of lists in python. For example, Original: list = [[1,2,3],[4,5,6],[7,8,9]] Output: new_list = [[7,8,9],[4,5,6],[1,2,3]] Right now, I am trying this: new_list = reversed(list) However, this doesn't seem to work. 回答1: In [24]: L = [[1,2,3],[4,5,6],[7,8,9]] In [25]: L[::-1] Out[25]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]] 来源: https://stackoverflow.com

How to reverse a list of lists in python? [duplicate]

喜夏-厌秋 提交于 2021-02-16 15:06:33
问题 This question already has answers here : How can I reverse a list in Python? (33 answers) Closed 5 years ago . I was wondering how to reverse a list of lists in python. For example, Original: list = [[1,2,3],[4,5,6],[7,8,9]] Output: new_list = [[7,8,9],[4,5,6],[1,2,3]] Right now, I am trying this: new_list = reversed(list) However, this doesn't seem to work. 回答1: In [24]: L = [[1,2,3],[4,5,6],[7,8,9]] In [25]: L[::-1] Out[25]: [[7, 8, 9], [4, 5, 6], [1, 2, 3]] 来源: https://stackoverflow.com

How to read filenames in a folder and access them in an alphabetical and increasing number order?

馋奶兔 提交于 2021-02-16 14:49:09
问题 I would like to ask how to efficiently handle accessing of filenames in a folder in the right order (alphabetical and increasing in number). For example, I have the following files in a folder: apple1.dat, apple2.dat, apple10.dat, banana1.dat, banana2.dat, banana10.dat. I would like to read the contents of the files such that apple1.dat will be read first and banana10.dat will be read last. Thanks. This is what I did so far. from glob import glob files=glob('*.dat') for list in files # I read

How to split consecutive elements in a list into sublists

无人久伴 提交于 2021-02-16 14:28:26
问题 I have the following list: indices_to_remove: [0,1,2,3,..,600,800,801,802,....,1200,1600,1601,1602,...,1800] I have basically 3 subsets of consecutive indices: 0-600 800-1200 1600-1800 I would like to create 3 different small lists that will include only consecutive numbers. Expected outcome: indices_to_remove_1 : [0,1,2,3,....,600] indices_to_remove_2 : [800,801,802,....,1200] indices_to_remove_3 : [1600,1601,1602,....., 1800] P.S: The numbers are arbitrary and random; moreover, I may

“TypeError: 'type' object is not subscriptable” in a function signature

陌路散爱 提交于 2021-02-16 14:00:48
问题 Why am I receiving this error when I run this code? Traceback (most recent call last): File "main.py", line 13, in <module> def twoSum(self, nums: list[int], target: int) -> list[int]: TypeError: 'type' object is not subscriptable nums = [4,5,6,7,8,9] target = 13 def twoSum(self, nums: list[int], target: int) -> list[int]: dictionary = {} answer = [] for i in range(len(nums)): secondNumber = target-nums[i] if(secondNumber in dictionary.keys()): secondIndex = nums.index(secondNumber) if(i !=

Highest and lowest value of nested lists using recursion

无人久伴 提交于 2021-02-16 13:53:46
问题 I want to take in a list with nested lists. Then print the highest value of index 0 or 2 in the list and the lowest value of index 0 or 2, through using recursion. This is what I got so far: lst = [1, 5, [7, 10, []]] def high_low(my_list): new_lst = [] if not my_list: print max(new_lst) print min(new_lst) elif isinstance(my_list[0], int): return new_lst.append(my_list[0]) + high_low(my_list[2:]) elif isinstance(my_list[0], list): return new_lst.append(max(my_list[0])) + high_low(my_list[2:])

Highest and lowest value of nested lists using recursion

走远了吗. 提交于 2021-02-16 13:53:12
问题 I want to take in a list with nested lists. Then print the highest value of index 0 or 2 in the list and the lowest value of index 0 or 2, through using recursion. This is what I got so far: lst = [1, 5, [7, 10, []]] def high_low(my_list): new_lst = [] if not my_list: print max(new_lst) print min(new_lst) elif isinstance(my_list[0], int): return new_lst.append(my_list[0]) + high_low(my_list[2:]) elif isinstance(my_list[0], list): return new_lst.append(max(my_list[0])) + high_low(my_list[2:])

C# Set Data Annotation on List<string> [duplicate]

不打扰是莪最后的温柔 提交于 2021-02-16 13:34:26
问题 This question already has answers here : Required Attribute on Generic List Property (4 answers) Closed 7 years ago . I have this piece of code: [Required] public List<string> myStringList { get; set; } Unfortunatelly, it doesn't work, tha validator totally ignores it. Besides, this works fine: [Required] public string myString { get; set; } and DateTimes work fine as well. Obviously, the problem doesn't lie on my validator, but on the annotation. So the question is, how should I set the Data

C# Set Data Annotation on List<string> [duplicate]

那年仲夏 提交于 2021-02-16 13:34:01
问题 This question already has answers here : Required Attribute on Generic List Property (4 answers) Closed 7 years ago . I have this piece of code: [Required] public List<string> myStringList { get; set; } Unfortunatelly, it doesn't work, tha validator totally ignores it. Besides, this works fine: [Required] public string myString { get; set; } and DateTimes work fine as well. Obviously, the problem doesn't lie on my validator, but on the annotation. So the question is, how should I set the Data

How to remove the last occurrence of an item from a list?

这一生的挚爱 提交于 2021-02-16 13:27:36
问题 The list.remove() function serves to remove the first time an item appears in a list. Is there a built-in function to remove the last time? For instance if I have a list, say: X = ['sf', 'cc', 'ch', 'sc', 'sh', 'ch'] and I want to remove the last 'ch' from the list, is there a better method than what I'm currently doing, which is: X.reverse() X.remove('ch') X.reverse() I will soon also have to worry about cases where the item being removed is potentially not in the list. So methods that do