Python 枚举还是其他?
Python enumerating or something else?
我从遵循此示例的 json 对象开始:
[
{
"url": "https://www.Whosebug_greatquestions.com",
"pages": 1
},
{
"url": "https://www.Whosebug_not-so-greatquestions.com",
"pages": 3
}
]
我想要完成的是打印与我这样做的页数相等的 url:
url_list = []
for x in json:
for y in range(x['pages']):
url_list.append(x['url'])
我快完成了,这是按照我的要求进行并打印出 url,但我还想根据页面的值对输出进行编号。
所以我的最终结果是这样的:
"https://www.Whosebug_greatquestions.com", 1,
"https://www.Whosebug_not-so-greatquestions.com", 1,
"https://www.Whosebug_not-so-greatquestions.com", 2,
"https://www.Whosebug_not-so-greatquestions.com", 3
我觉得枚举在这里很有用,但我似乎无法理解如何实现它。我所能做的就是对整个列表进行编号,正如您从上面看到的那样,这不是我想要完成的。
提前谢谢你,
布莱恩
这是一种方法。没什么特别的,只是一个 for
循环和一个 range
依赖于 'pages'
键。
>>> data = [
...: {
...: "url": "https://www.Whosebug_greatquestions.com",
...: "pages": 1
...: },
...: {
...: "url": "https://www.Whosebug_not-so-greatquestions.com",
...: "pages": 3
...: }
...: ]
>>>
>>> for d in data:
...: for i in range(1, d['pages'] + 1):
...: print(d['url'], i)
...:
https://www.Whosebug_greatquestions.com 1
https://www.Whosebug_not-so-greatquestions.com 1
https://www.Whosebug_not-so-greatquestions.com 2
https://www.Whosebug_not-so-greatquestions.com 3
编辑 - 构建列表:
>>> result = [(d['url'], i) for d in data for i in range(1, d['pages'] + 1)]
>>> result
[('https://www.Whosebug_greatquestions.com', 1),
('https://www.Whosebug_not-so-greatquestions.com', 1),
('https://www.Whosebug_not-so-greatquestions.com', 2),
('https://www.Whosebug_not-so-greatquestions.com', 3)]
这会很好用:
url_list = []
for x in json:
for y in range(x['pages']):
url_list.append(x['url']+', '+str(y+1))
print(url_list)
输出
['https://www.Whosebug_greatquestions.com, 1', 'https://www.Whosebug_not-so-greatquestions.com, 1', 'https://www.Whosebug_not-so-greatquestions.com, 2', 'https://www.Whosebug_not-so-greatquestions.com, 3']
我从遵循此示例的 json 对象开始:
[
{
"url": "https://www.Whosebug_greatquestions.com",
"pages": 1
},
{
"url": "https://www.Whosebug_not-so-greatquestions.com",
"pages": 3
}
]
我想要完成的是打印与我这样做的页数相等的 url:
url_list = []
for x in json:
for y in range(x['pages']):
url_list.append(x['url'])
我快完成了,这是按照我的要求进行并打印出 url,但我还想根据页面的值对输出进行编号。
所以我的最终结果是这样的:
"https://www.Whosebug_greatquestions.com", 1,
"https://www.Whosebug_not-so-greatquestions.com", 1,
"https://www.Whosebug_not-so-greatquestions.com", 2,
"https://www.Whosebug_not-so-greatquestions.com", 3
我觉得枚举在这里很有用,但我似乎无法理解如何实现它。我所能做的就是对整个列表进行编号,正如您从上面看到的那样,这不是我想要完成的。
提前谢谢你, 布莱恩
这是一种方法。没什么特别的,只是一个 for
循环和一个 range
依赖于 'pages'
键。
>>> data = [
...: {
...: "url": "https://www.Whosebug_greatquestions.com",
...: "pages": 1
...: },
...: {
...: "url": "https://www.Whosebug_not-so-greatquestions.com",
...: "pages": 3
...: }
...: ]
>>>
>>> for d in data:
...: for i in range(1, d['pages'] + 1):
...: print(d['url'], i)
...:
https://www.Whosebug_greatquestions.com 1
https://www.Whosebug_not-so-greatquestions.com 1
https://www.Whosebug_not-so-greatquestions.com 2
https://www.Whosebug_not-so-greatquestions.com 3
编辑 - 构建列表:
>>> result = [(d['url'], i) for d in data for i in range(1, d['pages'] + 1)]
>>> result
[('https://www.Whosebug_greatquestions.com', 1),
('https://www.Whosebug_not-so-greatquestions.com', 1),
('https://www.Whosebug_not-so-greatquestions.com', 2),
('https://www.Whosebug_not-so-greatquestions.com', 3)]
这会很好用:
url_list = []
for x in json:
for y in range(x['pages']):
url_list.append(x['url']+', '+str(y+1))
print(url_list)
输出
['https://www.Whosebug_greatquestions.com, 1', 'https://www.Whosebug_not-so-greatquestions.com, 1', 'https://www.Whosebug_not-so-greatquestions.com, 2', 'https://www.Whosebug_not-so-greatquestions.com, 3']