私信  •  关注

Sayandip Dutta

Sayandip Dutta 最近创建的主题
Sayandip Dutta 最近回复了
6 年前
回复了 Sayandip Dutta 创建的主题 » Python张量矩阵乘法

你可以试试这个:

>>> import numpy as np
>>> a = np.arange(1,9).reshape(2,2,2)
>>> a
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
>>> b = np.arange(1,5).reshape(2,2)
>>> b
array([[1, 2],
       [3, 4]])
>>> (a * b[None,:,:].T).sum(axis = 0)
array([[11, 14],
       [37, 44]])

>>> b[None,:,:]
array([[[1, 2],
        [3, 4]]])
>>> b[None,:,:].T
array([[[1],
        [3]],

       [[2],
        [4]]])
6 年前
回复了 Sayandip Dutta 创建的主题 » 如何用python计算列表中的单词列表

它只给你一个计数是因为你只在寻找一个。试试这个:

fruits = ['apple', 'banana', 'grape', 'kiwi', 'banana', 'apple',
          'apple', 'watermelon', 'kiwi', 'banana', 'apple']
words = ["apple", "banana", "kiwi"]

def count_the_fruits(fruits, words):
    # This is a dict comprehension
    counts = {word: fruits.count(word) for word in words}
    return counts

print(count_the_fruits(fruits, words))

输出:

{'apple': 4, 'banana': 3, 'kiwi': 2}
6 年前
回复了 Sayandip Dutta 创建的主题 » Python中的Pascal三角(11的幂)

你很接近,你可以 str.center 每行:

inc = int(input('Input number of rows: '))
n = 0
row = []
while n < inc:
    m = 11 ** n
    row.append(m)
    n += 1
for i in range(0, len(row)):
    row[i] = ' '.join(list(str(row[i])))
for i in range(0, len(row)):
    row[i] = row[i].center(len(row[-1]),' ')
result = '\n'.join(row)
print(result)

输出:

Input number of rows: 4
   1   
  1 1  
 1 2 1 
1 3 3 1

较短的版本是:

inc = int(input('Input number of rows: '))
max_len = 2 * len(str(11**inc)) - 1
row = (' '.join([*str(11**p)]).center(max_len,' ') for p in range(inc))
print(*row, sep='\n')

输出:

输入行数:4
1个
11个
1 2 1号
1 3 3 1
6 年前
回复了 Sayandip Dutta 创建的主题 » 如果值是多数组,则使用键将值附加到python字典值

这是同一个旧的 deep copy shallow copy 事情。 试试这个:

>>> a = []
>>> b = [] 
>>> c = [] 
>>> d = [] 
>>> e = [] 
>>> f = []
>>> classifiers = [a,b,c,d,e,f]
>>> import copy
>>> cvfoldacc = {k:copy.deepcopy(classifiers) for k in range(2,11)}
>>> cvfoldacc[2][0].append(8)
>>> cvfoldacc 
{2: [[8], [], [], [], [], []],
 3: [[], [], [], [], [], []],
 4: [[], [], [], [], [], []],
 5: [[], [], [], [], [], []],
 6: [[], [], [], [], [], []],
 7: [[], [], [], [], [], []],
 8: [[], [], [], [], [], []],
 9: [[], [], [], [], [], []],
 10: [[], [], [], [], [], []]}

你在做:

a = []
b = [] 
c = [] 
d = [] 
e = [] 
f = []
classifiers = [a,b,c,d,e,f]
cvfoldacc = dict.fromkeys(range(2,11), classifiers) 

现在,正在创建的字典在每个键中都有相同的列表,不仅它们看起来相同,而且它们是完全相同的对象,具有相同的 identity (CPython中的内存位置)。让我们看看:

>>> id(cvfoldacc[2])
171760008
>>> id(cvfoldacc[3])
171760008

其他价值观也一样。 所以 dict.fromkeys() 为字典中的所有键指定相同的值,而不创建其副本。

现在,另一种方法,你做一个浅显的复制,这个问题就解决了:

>>> cvfoldacc = {a:classifiers.copy() for a in range(2,11)}
>>> id(cvfoldacc[2])
171840616
>>> id(cvfoldacc[3])
171847688

解决了对吧?

>>> cvfoldacc[2][0].append(8)
{2: [[8], [], [], [], [], []],
 3: [[8], [], [], [], [], []],
 4: [[8], [], [], [], [], []],
 5: [[8], [], [], [], [], []],
 6: [[8], [], [], [], [], []],
 7: [[8], [], [], [], [], []],
 8: [[8], [], [], [], [], []],
 9: [[8], [], [], [], [], []],
 10: [[8], [], [], [], [], []]}

显然不是!!让我们现在看得更深一点。让我们看看 id 列表中的列表:

>>> id(cvfoldacc[2][0])
171810120
>>> id(cvfoldacc[3][0])
171810120

尽管如此 list.copy() 创建了outerlist的副本,内部列表是相同的。所以本质上你是在 list: a 它存在于 keys 所以一切都在改变。

Deepcopy 递归地复制了所有对象,避免了这个问题。