我只是在学Python。我有一本字典,里面有一个键,里面有值中的列表。我需要做一个计数的所有独特的价值,从关键的‘工艺’。我做了一个循环,以获得‘工艺’的钥匙,但不知道如何只返回独特的工艺。实际上,想要列出它们,然后对它们进行计数。以下是我到目前为止所拥有的:
import requests
Data = { } #create the dictionary
response = requests.get("http://api.open-notify.org/astros.json")
Data = response.json()
for i in range(len(Data["people"])):
print(Data["people"][i]["craft"])这是输出:
ISS
ISS
ISS
Shenzhou 13
Shenzhou 13
Shenzhou 13
ISS
ISS
ISS
ISS我想展示的是:
ISS
Shenzhou 13然后数一数,结果是:2
发布于 2022-03-09 23:27:51
这里有些事情应该解决
response = requests.get("http://api.open-notify.org/astros.json")
data = response.json()
unique_crafts = []
for people in data["people"]:
craft = people["craft"]
if craft not in unique_crafts:
unique_crafts.append(craft)
print(unique_crafts)
print(len(unique_crafts))发布于 2022-03-09 23:29:36
把所有的手工艺都放在一套。集合只包含唯一的元素。
crafts = set(person['craft'] for person in Data['people'])
print(*crafts, sep='\n')
print(len(crafts))https://stackoverflow.com/questions/71417106
复制相似问题