小编Cli*_*ley的帖子

列出动态字典python

以下是我的文本文件中的数据集.

2.1,3.5,1.4,0.2,Iris
4.9,3.0,1.4,0.2,Ilia
3.7,3.2,1.3,0.2,Iridium
Run Code Online (Sandbox Code Playgroud)

有一个名为的列表:

list_of_keys 
Run Code Online (Sandbox Code Playgroud)

它在列表中包含以下值

['S_Length','S_Width','P_Length','P_Width','Predicate']
Run Code Online (Sandbox Code Playgroud)

所以,问题是,我想创建一个字典列表来保存我的所有数据(从文本文件)使用list_of_keys as keys字典,如下所示:

dict = 
      {'S_Length': 2.1, 'S_Width':3.5 , 'P_Length': 1.4, 'P_Width': 0.2, 'Predicate': Iris},
      {'S_Length': 4.9, 'S_Width':3.0 , 'P_Length': 1.4, 'P_Width': 0.2, 'Predicate': Ilia},
      ... so on!
Run Code Online (Sandbox Code Playgroud)

我到现在为止:

# store all data from the text files as list
all_examples = file.readlines()

for outer_index in range(len(all_examples)):
     for inner_index in range(0, len(list_of_keys)+1):
Run Code Online (Sandbox Code Playgroud)

python dictionary loops

6
推荐指数
1
解决办法
655
查看次数

在python中创建一个字典列表

我有一个从文本文件中读入的以下数据集:

all_examples=   ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
Run Code Online (Sandbox Code Playgroud)

我需要创建一个字典列表如下:

lst = [ 
{"A":1, "B":2, "C":4, "D":4 },
{"A":1, "B":1, "C":4, "D":5 } 
]
Run Code Online (Sandbox Code Playgroud)

我尝试使用生成器函数,但很难创建一个列表.

attributes = 'A,B,C'
def get_examples():

    for value in examples:
        yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))
Run Code Online (Sandbox Code Playgroud)

python dictionary

4
推荐指数
1
解决办法
127
查看次数

如何为k-means聚类选择初始质心

我正在致力于在 Python 中实现 k-means 聚类。为数据集选择初始质心的好方法是什么?例如:我有以下数据集:

A,1,1
B,2,1
C,4,4
D,4,5
Run Code Online (Sandbox Code Playgroud)

我需要创建两个不同的集群。我如何从质心开始?

python cluster-analysis data-mining k-means centroid

4
推荐指数
1
解决办法
1万
查看次数

找到决策树的最佳属性

为什么我们要选择"最佳"属性?如果我们从任何其他属性中选择,会有什么不同吗?

machine-learning decision-tree

4
推荐指数
1
解决办法
1253
查看次数

类方法中未解决的引用

我不认为这是因为该功能的范围,但我得到一个

尚未解析的引用,位于get_all_predicates(examples).count(predicate_list [0])

get_entropy_of_attributes(examples, predicate_list)我班上的内部函数Tree

class Tree:

    def get_examples(examples, attributes):
        for value in examples:
            yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))

    def get_all_predicates(examples):
        return [d['Predicate'] for d in examples]

    def get_entropy_of_attributes(examples, predicate_list):
        get_all_predicates(examples).count(predicate_list[0])
        return 0

    examples = list(get_examples(all_examples, name_of_attributes))

    predicate_list = list(set(get_all_predicates(examples)))

    get_entropy_of_attributes(examples, predicate_list)
Run Code Online (Sandbox Code Playgroud)

all_examples是词典name_of_attributes列表,并且是列表,其中包含从文本文件导入的值。

all_examples = [{'P_Length': '1.4', 'P_Width': '0.2', 'Predicate': 'I-setosa', 'Sepal_Width': '3.5', 'S_Length': '5.1'}, ...]

name_of_attributes = ["Check","P-Width"]
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

python scope function

3
推荐指数
2
解决办法
1万
查看次数

从python中的字典列表中查找特定值

我在字典列表中有以下数据:

data = [{'I-versicolor': 0, 'Sepal_Length': '7.9', 'I-setosa': 0, 'I-virginica': 1},
{'I-versicolor': 0, 'I-setosa': 1, 'I-virginica': 0, 'Sepal_Width': '4.2'},
{'I-versicolor': 2, 'Petal_Length': '3.5', 'I-setosa': 0, 'I-virginica': 0},
{'I-versicolor': 1.2, 'Petal_Width': '1.2', 'I-setosa': 0, 'I-virginica': 0}]
Run Code Online (Sandbox Code Playgroud)

为了获得基于键和值的列表,我使用以下内容:

next((item for item in data if item["Sepal_Length"] == "7.9"))
Run Code Online (Sandbox Code Playgroud)

但是,所有字典都不包含密钥Sepal_Length,我得到:

KeyError: 'Sepal_Length'
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

python dictionary python-3.5

3
推荐指数
1
解决办法
3974
查看次数

在字典python中添加键的列表值

我有一个以下字典:

centroid = {'A': [1.0, 1.0], 'B': [2.0, 1.0]}
Run Code Online (Sandbox Code Playgroud)

使用上面的字典我创建了两个不同的字典并将它们附加到列表中:

for key in centroids:
        clusters_list.append(dict(zip(key, centroids.get(key))))
Run Code Online (Sandbox Code Playgroud)

但是,当我检查我的cluster_list时,我得到以下数据:

[{'A': 1.0}, {'B': 2.0}]
Run Code Online (Sandbox Code Playgroud)

而不是 [{'A': [1.0, 1.0]}, {'B': [2.0, 1.0]}].我怎样才能解决这个问题?

python dictionary list

2
推荐指数
1
解决办法
60
查看次数

python 3中字典值的总和

我有以下字典:

dic= {'I-setosa': 8, 'I-versicolor': 2, 'I-virginica': 0}
Run Code Online (Sandbox Code Playgroud)

如何找到密钥的总和?在Python 2我可以做到以下几点:

sum(dic.values())
Run Code Online (Sandbox Code Playgroud)

除此之外,还有其他方法吗?我尝试使用dic.values()并找到该列表的总和,但这太长了。

python dictionary python-3.5

1
推荐指数
1
解决办法
2063
查看次数

不同iPhone的按钮位置不同

我如何才能将按钮保持在所有不同版本的iPhone的相同位置?我有一个背景图像,最重要的是,我有一个按钮。这是用于iPhone 6。

这是为iPhone 5。

无论如何,我可以为按钮提供固定位置吗?

以下是基于故事板的外观。 在此处输入图片说明

storyboard ios autolayout

1
推荐指数
1
解决办法
662
查看次数

使用迭代方法和LINQ查找模型的最常见属性

我有一个以下的Model类:

public class Student
{
     public string Name { get; set; }
     public string Major { get; set; }
     public int Age { get; set; }
}


public string GetPrimaryMajor(List<Student> students)
{ 
     ... 
}
Run Code Online (Sandbox Code Playgroud)

如何使用迭代和LINQ方法实现该方法GetPrimaryMajor()以确定参数中最常出现Majorstudents方法?

c# linq

1
推荐指数
1
解决办法
65
查看次数