dictionary

How do I generate an adjacency matrix of a graph from a dictionary in python?

不打扰是莪最后的温柔 提交于 2021-02-06 09:32:04
问题 I have the following dictionary: g = { 'A': ['A', 'B', 'C'], 'B': ['A', 'C', 'E'], 'C': ['A', 'B', 'D'], 'D': ['C','E'], 'E': ['B','D'] } It implements a graph, each list contains the neighbors of the graph vertices (dictionary keys are the vertices itself). I'm in trouble, I can not think of a way to get a graph adjacency matrix from their lists of neighbors, might be easy but I am new to python, I hope someone can help me! I am using Python 3.5 I need to generate the following matrix: 回答1:

How to make dictionary read-only in Python

左心房为你撑大大i 提交于 2021-02-05 20:52:33
问题 I have a class like: class A: def __init__(self): self.data = {} and at some moment I want to prohibit self.data fields modification. I've read in PEP-416 rejection notice that there are a lot of ways to do it. So I'd like to find what they are. I tried this: a = A() a.data = types.MappingProxyType(a.data) That should work but first, its python3.3+ and second, when I do this "prohibition" multiple times I get this: >>> a.data = types.MappingProxyType(a.data) >>> a.data = types

How to make dictionary read-only in Python

杀马特。学长 韩版系。学妹 提交于 2021-02-05 20:52:18
问题 I have a class like: class A: def __init__(self): self.data = {} and at some moment I want to prohibit self.data fields modification. I've read in PEP-416 rejection notice that there are a lot of ways to do it. So I'd like to find what they are. I tried this: a = A() a.data = types.MappingProxyType(a.data) That should work but first, its python3.3+ and second, when I do this "prohibition" multiple times I get this: >>> a.data = types.MappingProxyType(a.data) >>> a.data = types

differentiate between two values with same key in a nested dictionary [closed]

ⅰ亾dé卋堺 提交于 2021-02-05 12:34:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 days ago . Improve this question I have a nested dictionary in Python with the following structure: d= { 'name': 'test', 'IBM': 146.48, 'MSFT': 44.11, 'CSCO': 25.54, 'micro': {'name': 'test', 'age': 1} } In my code I want to change the value of the first key name . When I do: for k, v in d.items(): d[k] =

differentiate between two values with same key in a nested dictionary [closed]

耗尽温柔 提交于 2021-02-05 12:33:25
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 days ago . Improve this question I have a nested dictionary in Python with the following structure: d= { 'name': 'test', 'IBM': 146.48, 'MSFT': 44.11, 'CSCO': 25.54, 'micro': {'name': 'test', 'age': 1} } In my code I want to change the value of the first key name . When I do: for k, v in d.items(): d[k] =

Is there a way to work with files from a dictionary with similar names in a loop?

佐手、 提交于 2021-02-05 12:32:00
问题 So, I was wondering if there could be a way to use files from a dictionary with similar names in a loop, I have this dictionaries: dcm = {} for filename in os.listdir('./GMATfiles'): if fnmatch.fnmatch(filename,'DCM_hydra*.txt'): dcm[filename[:11]] = os.path.normpath(''.join(['./GMATfiles', '/', filename])) #print(dcm) #OUT_INPUT out={} for filename in os.listdir('./GMATfiles'): if fnmatch.fnmatch(filename,'Out_hydra*.txt'): out[filename[:11]] = os.path.normpath(''.join(['./GMATfiles', '/',

How to convert a list into a dictionary in python?

徘徊边缘 提交于 2021-02-05 12:31:45
问题 I have a list of objects: ['fb_ads_sm', 'Active', '18 hr ago', '23', 'sm_sg13', 'Pending', '12 hr ago', '0', ...] How do I convert this into a dictionary where the keys are the 1st and 5th elements and the values are the 2nd and 6th elements? dictionary = {'fb_ads_sm': 'Active', 'sm_sg13': 'Pending', ...} 回答1: You can try to use dict comprehension with list slicing a = ['fb_ads_sm', 'Active', '18 hr ago', '23', 'sm_sg13', 'Pending', '12 hr ago'] {k: v for k, v in zip(a[::4], a[1::4])} OR dict

Is there a way to work with files from a dictionary with similar names in a loop?

痴心易碎 提交于 2021-02-05 12:26:31
问题 So, I was wondering if there could be a way to use files from a dictionary with similar names in a loop, I have this dictionaries: dcm = {} for filename in os.listdir('./GMATfiles'): if fnmatch.fnmatch(filename,'DCM_hydra*.txt'): dcm[filename[:11]] = os.path.normpath(''.join(['./GMATfiles', '/', filename])) #print(dcm) #OUT_INPUT out={} for filename in os.listdir('./GMATfiles'): if fnmatch.fnmatch(filename,'Out_hydra*.txt'): out[filename[:11]] = os.path.normpath(''.join(['./GMATfiles', '/',

How can write a program in scheme to find factors of a list of numbers

匆匆过客 提交于 2021-02-05 12:25:33
问题 This is the code for a single integer, how can it extends to list of function? (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (display (factors 1111111)) (newline) 回答1: You can use for-each to iterate over a list. (define (factors n) (define (*factors d) (cond ((> d n) (list)) ((= (modulo n d) 0) (cons d (*factors (+ d 1)))) (else (*factors (+ d 1))))) (*factors 1)) (define arbitarily

How to make a map sort by value C++

这一生的挚爱 提交于 2021-02-05 12:23:07
问题 I was trying to make a map sort by value using a custom comparator but I couldn't figure out why I kept getting the error of "no matching call to compareByVal" Here's what I had in my main.cpp: #include <map> #include <iostream> struct compareByVal { bool operator[](const std::pair<int,int> & a, const std::pair<int,int> & b) return a.second < b.second; } int main() { std::map<int,int,compareByVal> hash; hash[1] = 5; hash[2] = 2; hash[3] = 10; std::cout << hash.begin()->first << std::endl; }