[英]How can I store the types of elements from a list in a dictionary in Python?
How can I store the types of elements from a list in a dictionary?如何将列表中的元素类型存储在字典中? for example to make the output like this:
例如使 output 像这样:
datatype_statistic([1,'2',3.5, 0.5, None, (1,1)]) = {
'int': 1,
'str': 1,
'float': 2,
'None':1,
'tuple': 1
}
This is what I did till now but I just do not know what is the method that I should apply to acheive the above:这是我到目前为止所做的,但我只是不知道我应该采用什么方法来实现上述目标:
def datatype_statistic(ls):
my_list=len(ls)
print("There are "+str(my_list)+" elements in this list" )
for item in ls:
print(type(item))
datatype_statistic([1,'2',3.5, 0.5, None, (1,1)])
You can use collections.defaultdict
:您可以使用
collections.defaultdict
:
from collections import defaultdict
def datatypes(lst):
ret = defaultdict(int)
for x in lst:
ret[type(x).__name__] += 1
return dict(ret)
print(datatypes([1, '2', 3.5, 0.5, None, (1, 1)]))
Or, without imports:或者,没有进口:
def datatypes(lst):
ret = {}
for x in lst:
ret[type(x).__name__] = ret.get(type(x).__name__, 0) + 1
return ret
print(datatypes([1, '2', 3.5, 0.5, None, (1, 1)]))
Output: {'int': 1, 'str': 1, 'float': 2, 'NoneType': 1, 'tuple': 1}
Output:
{'int': 1, 'str': 1, 'float': 2, 'NoneType': 1, 'tuple': 1}
collections.Counter
is specifically made for this kind of task: collections.Counter
专为此类任务而设计:
from collections import Counter
ls = [1,'2',3.5, 0.5, None, (1,1)]
def datatype_statistic(ls):
type_names = [type(elem).__name__ for elem in ls]
return dict(Counter(type_names))
print(datatype_statistic(ls))
Output: Output:
{'int': 1, 'str': 1, 'float': 2, 'NoneType': 1, 'tuple': 1}
I first use a list comprehension to get the type names from the list elements.我首先使用列表推导从列表元素中获取类型名称。 Then I just apply the
Counter
.然后我只应用
Counter
。 Finally I cast to dict to get the output in the requested form.最后,我转换为 dict 以获取所需形式的 output。
Note that the calls to .__name__
and dict()
are only there to shoe-horn the result to fit the example given for a desired result.请注意,对
.__name__
和dict()
的调用只是为了将结果强加于为所需结果给出的示例。 You need to know yourself if you actually need them.如果您真的需要它们,您需要了解自己。
The type of None
is NoneType
, so that's what I print. None
的类型是NoneType
,所以这就是我打印的内容。 You can special-case this if needed.如果需要,您可以对此进行特殊处理。
jake_list = [1, '2', 3.5, 0.5, None, (1, 1)]
datatype_statistic = dict()
for i in jake_list:
tmp_type = type(i).__name__
datatype_statistic[tmp_type] = datatype_statistic.get(tmp_type, 0) + 1
# {'int': 1, 'str': 1, 'float': 2, 'NoneType': 1, 'tuple': 1}
You would have to employ some form of counter to solve this, which you have not used in your code.您必须使用某种形式的计数器来解决这个问题,而您的代码中没有使用过。
def type_counter_list(list_1):
count_type = dict()
for _ in list_1:
x = type(_)
if _ in count_type.keys():
count_type[x]+=1
else:
count_type[x]=1
return count_type
This is not a code, but I guess you are a beginner.这不是代码,但我猜你是初学者。 This is a blueprint for you to develop your code on.
这是您开发代码的蓝图。
You can achieve this by using some built-in methods in Python such as map
, list.count
, and type
您可以通过使用 Python 中的一些内置方法来实现此目的,例如
map
、 list.count
和type
This is the solution for me:这是我的解决方案:
your_list = [1,'2',3.5, 0.5, None, (1,1)]
x = list(map(lambda j: type(j), your_list))
types_dict = {i.__name__:x.count(i) for i in x} # A dict comprehension
# types_dict value sould be: {'int': 1, 'str': 1, 'float': 2, 'NoneType': 1, 'tuple': 1}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.