list

Appending to dict of lists adds value to every key [duplicate]

假装没事ソ 提交于 2021-02-18 21:43:11
问题 This question already has answers here : dict.fromkeys all point to same list (4 answers) Closed 5 years ago . I have a dictionary of empty lists with all keys declared at the beginning: >>> keys = ["k1", "k2", "k3"] >>> d = dict.fromkeys(keys, []) >>> d {'k2': [], 'k3': [], 'k1': []} When I try to add a coordinate pair (the list ["x1", "y1"] ) to one of the key's lists, it instead adds to all the keys' lists: >>> d["k1"].append(["x1", "y1"]) >>> d {'k1': [['x1', 'y1']], 'k2': [['x1', 'y1']],

minimum of list of lists

放肆的年华 提交于 2021-02-18 20:47:32
问题 I have a list of lists like so: [[10564, 15], [10564, 13], [10589, 18], [10637, 39], [10662, 38], [10712, 50], [10737, 15], [10762, 14], [10787, 9], [10812, 12], [10837, 45], [3, 17], [7, 21], [46, 26], [48, 12], [49, 24], [64, 14], [66, 17], [976, 27], [981, 22], [982, 22], [983, 17], [985, 13], [517, 9], [521, 15], [525, 11], [526, 13], [528, 14], [698, 14], [788, 24], [792, 19]] I am trying to find the lowest value for the second element in each list(so compare 15 to 13 to 18 etc not

C# Tuple versus List Considerations

流过昼夜 提交于 2021-02-18 12:36:06
问题 The different attributes of Tuples and Lists; Tuples are heterogeneous and Lists are homogeneous, Tuples are immutable while Lists are mutable, often dictate the use of one type over the other. In other scenarios, however, either data type could be equally appropriate. As such , what are the memory and/or performance implications of Tuples versus Lists that might also guide our decision? Thanks, 回答1: Well, in addition to what you've mentioned there's the rather significant difference that a

python: pointwise list sum

六眼飞鱼酱① 提交于 2021-02-18 11:50:42
问题 Input: two lists (list1, list2) of equal length Output: one list (result) of the same length, such that: result[i] = list1[i] + list2[i] Is there any concise way to do that? Or is this the best: # Python 3 assert len(list1) == len(list2) result = [list1[i] + list2[i] for i in range(len(list1))] 回答1: You can use the builtin zip function, or you can also use do it mapping the two lists over the add operator. Like this: from operator import add map(add, list1,list2) 回答2: IMO the nicest way is

Selection elements of a list based on another 'True'/'False' list

我只是一个虾纸丫 提交于 2021-02-18 07:31:48
问题 I have two lists of the same length. The first one contains strings. The second one - strings that can be either 'True' or 'False' . If the nth element of the second list is 'True' , I want to append the nth element of the first list to another list. So if I have: List1: ('sth1','sth2','sth3','sth4') List2: ('True','False','True','False') The outcome should be List3 : ('sth1','sth3') . How can I intersect two list in that way? 回答1: Use zip: result = [x for x, y in zip(xs, ys) if y == 'True']

What is the best VBA data type`key`=>`value` to save data same as PHP array

让人想犯罪 __ 提交于 2021-02-17 21:31:41
问题 I'm working with VBA and need to save data in type key => value to getting fastest; This data type help me cache responese text from http request, increase query speed. But I don't know what is the best way to do it? I need a data type same as php array with key=>value ! Thank for help! 回答1: Have you looked at dictionary object? It's available as part of the Microsoft Scripting Runtime. A clear example of how to add this is given by this SO answer. Sub DictExample1() Dim dict As Dictionary

What is the best VBA data type`key`=>`value` to save data same as PHP array

六月ゝ 毕业季﹏ 提交于 2021-02-17 21:31:34
问题 I'm working with VBA and need to save data in type key => value to getting fastest; This data type help me cache responese text from http request, increase query speed. But I don't know what is the best way to do it? I need a data type same as php array with key=>value ! Thank for help! 回答1: Have you looked at dictionary object? It's available as part of the Microsoft Scripting Runtime. A clear example of how to add this is given by this SO answer. Sub DictExample1() Dim dict As Dictionary

Using map() for columns in a pandas dataframe

吃可爱长大的小学妹 提交于 2021-02-17 21:19:06
问题 I have some columns in my dataframe for which I just want to keep the date part and remove the time part. I have made a list of these columns: list_of_cols_to_change = ['col1','col2','col3','col4'] I have written a function for doing this. It takes a list of columns and applies dt.date to each column in the list. def datefunc(x): for column in x: df[column] = df[column].dt.date I then call this function passing the list as parameter: datefunc(list_of_cols_to_change ) I want to accomplish this

Beginner Python: AttributeError: 'list' object has no attribute

折月煮酒 提交于 2021-02-17 12:24:26
问题 The error says: AttributeError: 'list' object has no attribute 'cost' I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles: class Bike(object): def __init__(self, name, weight, cost): self.name = name self.weight = weight self.cost = cost bikes = { # Bike designed for children" "Trike": ["Trike", 20, 100], # Bike designed for everyone" "Kruzer": ["Kruzer", 50, 165] } When I try to calculate profit with my for statement, I get the

Beginner Python: AttributeError: 'list' object has no attribute

烈酒焚心 提交于 2021-02-17 12:24:13
问题 The error says: AttributeError: 'list' object has no attribute 'cost' I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles: class Bike(object): def __init__(self, name, weight, cost): self.name = name self.weight = weight self.cost = cost bikes = { # Bike designed for children" "Trike": ["Trike", 20, 100], # Bike designed for everyone" "Kruzer": ["Kruzer", 50, 165] } When I try to calculate profit with my for statement, I get the