list

How do I change column names in list of data frames inside a function?

我怕爱的太早我们不能终老 提交于 2021-02-16 18:51:25
问题 I know that the answer to "how to change names in a list of data frames" has been answered multiple times. However, I'm stuck trying to generate a function that can take any list as an argument and change all of the column names of all of the data frames in the list. I am working with a large number of .csv files, all of which will have the same 3 column names. I'm importing the files in groups as follows: # Get a group of drying data data files, remove 1st column files <- list.files('Mang

How to reverse dictionary items and list keys grouped by common values [duplicate]

拟墨画扇 提交于 2021-02-16 18:21:48
问题 This question already has answers here : Dictionary comprehension for swapping keys/values in a dict with multiple equal values (3 answers) Closed 10 months ago . I have a dictionary that I want to group by the common values: init_dict = {'00001': 'string1', '00002': 'string2', '00003': 'string1', '00004': 'string3', '00005': 'string2'} I want to create a new dictionary that groups the values and lists the keys like this: new_dict = {'string1': ['00001', '00003'], 'string2':['00002', '00004']

Extract elements by name from a nested list

感情迁移 提交于 2021-02-16 18:14:49
问题 For a named, nested list, what is the best way to extract a specific element? If I have a list with known fields (eg, from a yaml file), I want to extract an element (list or otherwise) without having to search through the names and indices or trying to keep track of the levels in the str output. For example, I know that lm returns a nested list which contains qr info. fit <- lm(mpg ~ wt, mtcars) fit$qr$qraux # [1] 1.176777 1.046354 But if I don't know the order, I just want to specify the

Python: Bitwise-like list operations

余生长醉 提交于 2021-02-16 17:00:36
问题 I'm trying to elementwise & and elementwise | 2 lists of 8 lists of 6 binary digits, and it is working very oddly. c1 and c2 start as tuples of length 8 with elements that are tuples of length 6, and res starts out as a list version of c1. anding: for x in range(8): for y in range(6): res[x][y] = (c1[:][x][y])*(c2[:][x][y]) oring: for x in range(8): for y in range(6): res[x][y] = int(c1[:][x][y] or c2[:][x][y]) An example: c1: ((1, 0, 0, 0, 1, 1), (1, 1, 0, 0, 0, 1), (1, 1, 1, 0, 0, 0), (0, 1

How to make a list in Python distinct based on a property of the class in the list?

一世执手 提交于 2021-02-16 16:07:18
问题 I have a list of instances from the same class, and I want to make my list distinct based on a property in the class. What is the most pythonic way to achieve this? Here is some sample code: #!/usr/bin/python #-*- coding:utf-8 -*- class MyClass(object): def __init__(self, classId, tag): self.classId = classId self.tag = tag myList = [] myInstance1 = MyClass(1, "ABC") myInstance2 = MyClass(2, "DEF") myInstance3 = MyClass(3, "DEF") myList.append(myInstance1) myList.append(myInstance3) # note

How to make a list in Python distinct based on a property of the class in the list?

一曲冷凌霜 提交于 2021-02-16 16:06:56
问题 I have a list of instances from the same class, and I want to make my list distinct based on a property in the class. What is the most pythonic way to achieve this? Here is some sample code: #!/usr/bin/python #-*- coding:utf-8 -*- class MyClass(object): def __init__(self, classId, tag): self.classId = classId self.tag = tag myList = [] myInstance1 = MyClass(1, "ABC") myInstance2 = MyClass(2, "DEF") myInstance3 = MyClass(3, "DEF") myList.append(myInstance1) myList.append(myInstance3) # note

Remove elements from Dictionary<Key, Item>

 ̄綄美尐妖づ 提交于 2021-02-16 15:51:25
问题 I have a Dictionary, where items are (for example): "A", 4 "B", 44 "bye", 56 "C", 99 "D", 46 "6672", 0 And I have a List: "A" "C" "D" I want to remove from my dictionary all the elements whose keys are not in my list, and at the end my dictionary will be: "A", 4 "C", 99 "D", 46 How can I do? 回答1: It's simpler to construct new Dictionary to contain elements that are in the list: List<string> keysToInclude = new List<string> {"A", "B", "C"}; var newDict = myDictionary .Where(kvp=>keysToInclude

'Repeat' in Haskell?

白昼怎懂夜的黑 提交于 2021-02-16 15:49:45
问题 I'm very new to Haskell, and I have a simple question. What function can I use with a and b that will result in a, b times. Example: a = 4 | b = 3 Would return: [4, 4, 4] Thanks! 回答1: replicate : replicate 3 4 will be: [4,4,4] When you know what's the type of the function you need (in this case it was quite obvious that the function you needed had a type similar to Int -> a -> [a] ) you can use Hoogle in order to find it. 回答2: You could also use recursion (although the solutions above should

Matplotlib: Histogram from a list of frequencies

落花浮王杯 提交于 2021-02-16 15:43:25
问题 I have a list x = [90, 100, 121, 123, 88]. These values in the list are frequencies of occurrence of an event in 5 different trials. I am looking to create a histogram from this list. I simply tried: plt.hist(x) plt.show() I get this: I need something like this: Note: Very New to Python. And still learning when and how to use Stackoverflow. 回答1: Since the list x contains the frequencies then use a bar plot: x = [90, 100, 121, 123, 88] plt.bar(range(1,6), x) plt.ylabel('frequency') plt.show()

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

 ̄綄美尐妖づ 提交于 2021-02-16 15:10:44
问题 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