所以我有一個字典(dictionary2),其中每個值都是一個串列。我需要創建一個函式,在圖上顯示這些資料,鍵在 x 軸上,這是我管理的。我想創建一個單獨的串列 (count_list),其中包含每個數字在屬于給定鍵的串列中重復自身的頻率的計數(字典的所有值只有 1 個單獨的串列)。最終目標是創建一個散點圖,其中重疊標記更大,我可以通過將這個單獨的串列歸因于散點圖呼叫中的 's' 引數來實作。(請注意,不同的顏色或其他東西也可以正常作業,但無論如何仍然需要 count_list)
我有 1 個不考慮重疊的作業版本(這可能會提供一些背景關系,見下文)。
我目前正在嘗試使用以下代碼制作 count_list(為了實驗方便,我將它放在函式之外):
count_list=[]
for key in dictionary2:
for value in dictionary2:
for M in value:
count= value.count(M)
count_list.append(count)
這將回傳一個 count_list,其中每個鍵都重復相同的數字序列。我意識到我的代碼可能太簡單了,所以我對它不起作用并不感到驚訝。但是,我不確定從哪里開始,也不明白為什么輸出看起來像那樣。
當前的情節是這樣的:
def plot_dict(dataset):
#transforming data to be read correctly into the plot
d = dataset
x= []
y= []
for k, v in d.items():
x.extend(list(itertools.repeat(k, len(v))))
y.extend(v)
plt.figure(figsize=(30,30))
plt.plot(x,y, '.')
plt.title('FASTA plot', fontsize=45)
plt.xlabel('Sequence IDs', fontsize=30)
plt.ylabel('Molecular weight', fontsize=30)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plot_dict(dictionary2)
在此處輸入圖片說明
(我使用的是 jupyterlab 3.0.14。)
這是我第一次在堆疊溢位中發布問題,所以如果我違反了任何禮儀,或者我對問題的解釋有什么不清楚的地方,請告訴我!
uj5u.com熱心網友回復:
我不確定我是否正確理解了您的需求,但它是這樣的嗎?
from typing import Dict
dictionary = {
"key1": [1, 2, 3, 4, 4, 1],
"key2": [1, 2, 2, 2, 1, 5],
"key3": [100, 3, 100, 9],
}
occurrences_dict: Dict[str, Dict[int, int]] = {key: {} for key in dictionary}
for key, numbers_list in dictionary.items():
for number in numbers_list:
occurrences = numbers_list.count(number)
occurrences_dict[key].update({number: occurrences})
print(occurrences_dict)
輸出如下:
{
"key1": {1: 2, 2: 1, 3: 1, 4: 2},
"key2": {1: 2, 2: 3, 5: 1},
"key3": {100: 2, 3: 1, 9: 1},
}
您將獲得一個與原始鍵具有相同鍵的字典,并且在每個鍵中,您有一個字典,其中包含相應串列中每個數字的出現次數
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398297.html
上一篇:比較兩個串列并傳遞給另一個
下一篇:如何從標題串列列構建命名串列?
