从矩阵作为列表的列表:计算每列中集合 x 中的元素数。 (python3)
From matrix as lists of lists: count number of elements from a set x in each column. (python3)
我有一个包含 256 个函数的列表,每个函数有 4 对,如:
[{'a':'b', 'b':'c', 'c':'c', 'd':'a'}, ... ]
并想做以下事情...
- 对域中的每个元素应用函数 1 到 4 次。例如,对于域中的元素 1,'a':
[f[a], f[f[a]], ..., f[f[f[f[a]]]] ]
这会为 1 中的示例 dict/function 生成以下内容。
['b','c','c','c'] #for element a
['c','c','c','c'] #for element b
... etc. #for element c
#for element d
将列表的 matrix/list 查看为列。
4.Then,我想统计col1,2,3,4中的元素个数'a'。
并对每个域元素执行此操作。我希望产生某种形式的东西:
[ [num a's in col 1, num of as in col 2, ...],
[num b's in col 1, num of b's in col 2, ...], etc ]
对于 256 个 function/dict 元素中的每一个。
最终输出应该是这样的:
[ [[0,1,1,0],
[2,1,1,0],
[0,0,0,0],
[2,2,2,4]], .... 256 of these]
这是当前代码和当前输出:
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky():
global dictfuncts
dictfuncts = []
for item in allf:
di = dict(item)
dictfuncts.append(di)
print(dictfuncts)
def functmatnoz():
global matlist
matlist = []
global elems
for f in dictfuncts:
elems = []
for element in domain:
forward = [f[element], f[f[element]], f[f[f[element]]], f[f[f[f[element]]]]]
forward.reverse()
back = forward
elems.append(back)
matlist.append(elems)
print(matlist)
def sigma():
global sigmamat
sigmamat = []
for element in domain:
sig = []
for column in matlist:
size = column.count(element)
sig.append(size)
sigmamat.append(sig)
print(sigmamat)
dictfunky()
functmatnoz()
sigma()
上面的第一位输出如下,主要问题当然是结果字符串 0 而不是我想要的列表计数出现次数:
输出:
>>> dictfunky()
[{'b': 'c', 'd': 'd', 'c': 'd', 'a': 'a'}, {'b': 'c', 'd': 'a', 'c': 'd', 'a': 'b'}]
>>> functmatnoz()
[[['a', 'a', 'a', 'a'], ['d', 'd', 'd', 'c'], ['d', 'd', 'd', 'd'], ['d', 'd', 'd', 'd']], [['a', 'd', 'c', 'b'], ['b', 'a', 'd', 'c'], ['c', 'b', 'a', 'd'], ['d', 'c', 'b', 'a']]]
>>> sigma()
[[0, 0], [0, 0], [0, 0], [0, 0]]
>>>
想要的:
sigma()
[
[[4,0,0,0], # num of a's in each col for funct 1
[0,0,0,0], # num of b's in each col for funct 1
[0,1,0,0], # num of c's in ....
[0,3,4,4]] ,
[[1,1,1,1], # num of a's in each col for funct 2
[1,1,1,1], # num of b's in each col for funct 2
[1,1,1,1], # etc.
[1,1,1,1]]
]
这似乎符合您的要求。我还去掉了函数中的全局变量引用,使它们只是 return 结果 (global variables are bad)。唯一重要的逻辑变化是 sigma()
函数。
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky():
dictfuncts = []
for item in allf:
di = dict(item)
dictfuncts.append(di)
return dictfuncts
def functmatnoz():
matlist = []
for f in dictfuncts:
elems = []
for element in domain:
forward = [f[element], f[f[element]], f[f[f[element]]], f[f[f[f[element]]]]]
forward.reverse()
back = forward
elems.append(back)
matlist.append(elems)
return matlist
def sigma():
sigmamat = []
for mat in matlist:
col = []
for element in domain:
num_elem = []
for results in mat:
count = results.count(element)
num_elem.append(count)
col.append(num_elem)
sigmamat.append(col)
return sigmamat
dictfuncts = dictfunky()
matlist = functmatnoz()
sigmamat = sigma()
print('dictfuncts: {}'.format(dictfuncts))
print(' matlist: {}'.format(matlist))
print(' sigmamat: {}'.format(sigmamat))
输出(为便于阅读而添加的换行符):
dictfuncts: [{'c': 'd', 'b': 'c', 'a': 'a', 'd': 'd'},
{'c': 'd', 'b': 'c', 'a': 'b', 'd': 'a'}]
matlist: [[['a', 'a', 'a', 'a'], ['d', 'd', 'd', 'c'],
['d', 'd', 'd', 'd'], ['d', 'd', 'd', 'd']],
[['a', 'd', 'c', 'b'], ['b', 'a', 'd', 'c'],
['c', 'b', 'a', 'd'], ['d', 'c', 'b', 'a']]]
sigmamat: [[[4, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 3, 4, 4]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]]
更新
以下使用嵌套 list comprehensions 的优化版本以更少的代码生成完全相同的结果,因此您可能也会感兴趣。它还通过将所需的那些作为附加参数传递给它们,从而消除了每个函数中对全局变量的剩余引用。
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky(allf):
return [dict(item) for item in allf]
def functmatnoz(dictfuncts, domain):
return [[[f[f[f[f[element]]]],
f[f[f[element]]],
f[f[element]],
f[element]] for element in domain]
for f in dictfuncts]
def sigma(matlist, domain):
return [[[[results.count(element) for results in mat]
for element in domain]]
for mat in matlist]
dictfuncts = dictfunky(allf)
matlist = functmatnoz(dictfuncts, domain)
sigmamat = sigma(matlist, domain)
print('dictfuncts: {}'.format(dictfuncts))
print(' matlist: {}'.format(matlist))
print(' sigmamat: {}'.format(sigmamat))
我有一个包含 256 个函数的列表,每个函数有 4 对,如:
[{'a':'b', 'b':'c', 'c':'c', 'd':'a'}, ... ]
并想做以下事情...
- 对域中的每个元素应用函数 1 到 4 次。例如,对于域中的元素 1,'a':
[f[a], f[f[a]], ..., f[f[f[f[a]]]] ]
这会为 1 中的示例 dict/function 生成以下内容。
['b','c','c','c'] #for element a
['c','c','c','c'] #for element b
... etc. #for element c
#for element d
将列表的 matrix/list 查看为列。
4.Then,我想统计col1,2,3,4中的元素个数'a'。 并对每个域元素执行此操作。我希望产生某种形式的东西:
[ [num a's in col 1, num of as in col 2, ...], [num b's in col 1, num of b's in col 2, ...], etc ]
对于 256 个 function/dict 元素中的每一个。 最终输出应该是这样的:
[ [[0,1,1,0],
[2,1,1,0],
[0,0,0,0],
[2,2,2,4]], .... 256 of these]
这是当前代码和当前输出:
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky():
global dictfuncts
dictfuncts = []
for item in allf:
di = dict(item)
dictfuncts.append(di)
print(dictfuncts)
def functmatnoz():
global matlist
matlist = []
global elems
for f in dictfuncts:
elems = []
for element in domain:
forward = [f[element], f[f[element]], f[f[f[element]]], f[f[f[f[element]]]]]
forward.reverse()
back = forward
elems.append(back)
matlist.append(elems)
print(matlist)
def sigma():
global sigmamat
sigmamat = []
for element in domain:
sig = []
for column in matlist:
size = column.count(element)
sig.append(size)
sigmamat.append(sig)
print(sigmamat)
dictfunky()
functmatnoz()
sigma()
上面的第一位输出如下,主要问题当然是结果字符串 0 而不是我想要的列表计数出现次数:
输出:
>>> dictfunky()
[{'b': 'c', 'd': 'd', 'c': 'd', 'a': 'a'}, {'b': 'c', 'd': 'a', 'c': 'd', 'a': 'b'}]
>>> functmatnoz()
[[['a', 'a', 'a', 'a'], ['d', 'd', 'd', 'c'], ['d', 'd', 'd', 'd'], ['d', 'd', 'd', 'd']], [['a', 'd', 'c', 'b'], ['b', 'a', 'd', 'c'], ['c', 'b', 'a', 'd'], ['d', 'c', 'b', 'a']]]
>>> sigma()
[[0, 0], [0, 0], [0, 0], [0, 0]]
>>>
想要的:
sigma()
[
[[4,0,0,0], # num of a's in each col for funct 1
[0,0,0,0], # num of b's in each col for funct 1
[0,1,0,0], # num of c's in ....
[0,3,4,4]] ,
[[1,1,1,1], # num of a's in each col for funct 2
[1,1,1,1], # num of b's in each col for funct 2
[1,1,1,1], # etc.
[1,1,1,1]]
]
这似乎符合您的要求。我还去掉了函数中的全局变量引用,使它们只是 return 结果 (global variables are bad)。唯一重要的逻辑变化是 sigma()
函数。
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky():
dictfuncts = []
for item in allf:
di = dict(item)
dictfuncts.append(di)
return dictfuncts
def functmatnoz():
matlist = []
for f in dictfuncts:
elems = []
for element in domain:
forward = [f[element], f[f[element]], f[f[f[element]]], f[f[f[f[element]]]]]
forward.reverse()
back = forward
elems.append(back)
matlist.append(elems)
return matlist
def sigma():
sigmamat = []
for mat in matlist:
col = []
for element in domain:
num_elem = []
for results in mat:
count = results.count(element)
num_elem.append(count)
col.append(num_elem)
sigmamat.append(col)
return sigmamat
dictfuncts = dictfunky()
matlist = functmatnoz()
sigmamat = sigma()
print('dictfuncts: {}'.format(dictfuncts))
print(' matlist: {}'.format(matlist))
print(' sigmamat: {}'.format(sigmamat))
输出(为便于阅读而添加的换行符):
dictfuncts: [{'c': 'd', 'b': 'c', 'a': 'a', 'd': 'd'},
{'c': 'd', 'b': 'c', 'a': 'b', 'd': 'a'}]
matlist: [[['a', 'a', 'a', 'a'], ['d', 'd', 'd', 'c'],
['d', 'd', 'd', 'd'], ['d', 'd', 'd', 'd']],
[['a', 'd', 'c', 'b'], ['b', 'a', 'd', 'c'],
['c', 'b', 'a', 'd'], ['d', 'c', 'b', 'a']]]
sigmamat: [[[4, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 3, 4, 4]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]]
更新
以下使用嵌套 list comprehensions 的优化版本以更少的代码生成完全相同的结果,因此您可能也会感兴趣。它还通过将所需的那些作为附加参数传递给它们,从而消除了每个函数中对全局变量的剩余引用。
domain = ['a','b','c','d']
allf = [{'a':'a','b':'c','c':'d', 'd':'d'}, {'a':'b', 'b':'c', 'c':'d', 'd':'a'}]
def dictfunky(allf):
return [dict(item) for item in allf]
def functmatnoz(dictfuncts, domain):
return [[[f[f[f[f[element]]]],
f[f[f[element]]],
f[f[element]],
f[element]] for element in domain]
for f in dictfuncts]
def sigma(matlist, domain):
return [[[[results.count(element) for results in mat]
for element in domain]]
for mat in matlist]
dictfuncts = dictfunky(allf)
matlist = functmatnoz(dictfuncts, domain)
sigmamat = sigma(matlist, domain)
print('dictfuncts: {}'.format(dictfuncts))
print(' matlist: {}'.format(matlist))
print(' sigmamat: {}'.format(sigmamat))