不是列表的列表

2024-04-16 18:42:47 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python3.7中生成{'s': 'http://webprotege.stanford.edu/INFOMDM', 'p': 'type', 'o': 'term1_course'}列表?“:”应替换为“,”。你知道吗

提前谢谢。你知道吗


Tags: term1http列表typeedustanfordcourseinfomdm
3条回答

下面的解决方案将dict中的每个keyvalue转换为一个列表。你知道吗

d = {'s': 'http://webprotege.stanford.edu/INFOMDM', 'p': 'type', 'o': 'term1_course'}
mylist = []
for k, v in d.items():
    mylist.append(k)
    mylist.append(v)

print(mylist)

收益率: ['s', 'http://webprotege.stanford.edu/INFOMDM', 'p', 'type', 'o', 'term1_course']

相当于你要求用,替换:

你现在有一本字典。字典是键值对的集合,:左边的东西是键,右边的东西是值。你知道吗

要创建包含字典的键和值的列表,可以执行以下操作:

dict = {
  's': 'http://webprotege.stanford.edu/INFOMDM', 
  'p': 'type', 
  'o': 'term1_course'
}

list = []

for key in dict.keys():
    value = dict[key]
    list.append(key)
    list.append(value)

在此之后,list将是['s', 'http://webprotege.stanford.edu/INFOMDM', 'p', 'type', 'o', 'term1_course']

Iterate over the dict and extend to a list as follows:

mydict = {'s': 'http://webprotege.stanford.edu/INFOMDM', 'p': 'type', 'o': 'term1_course'}
mylist = []
for x in mydict.items():
    mylist.extend(x)

Output ['o', 'term1_course', 's', 'http://webprotege.stanford.edu/INFOMDM', 'p', 'type']

相关问题 更多 >