【问题标题】:I want to combine two lists together我想将两个列表组合在一起
【发布时间】:2023-02-20 23:03:08
【问题描述】:
import re
import requests
t=[]
u = []
n = []
k = []
exclude_banned = True
with open("u.txt", "r") as f:
    h = f.readlines()[0:100]
    for sub in h:
        t.append(re.sub('\n', '', sub))
    print(type(t))
    print(t)
    r = requests.post('https://users.roblox.com/v1/usernames/users', json={'usernames': t, 'excludeBannedUsers': exclude_banned})
    print(r.json())
    y = r.json()['data']
    u.extend([f['id'] for f in y])
    n.extend([f['name'] for f in y])
    k.append(f'{u[0]}:{n[0]}\n{u[1]}:{n[1]}')
    print(k)

我想将列表u 中的所有变量加入列表n,就像它们在列表k 中的加入方式一样。我将如何大规模地做到这一点?

我尝试了列表k 中的方法,但那会非常耗时,现在所有列表的长度总是相同的。 文件中的文本是这个https://gist.githubusercontent.com/deekayen/4148741/raw/98d35708fa344717d8eee15d11987de6c8e26d7d/1-1000.txt

【问题讨论】:

  • 嗨,对不起,我对 python 很不好,您必须向我展示一个工作示例,以便我理解 .extend 方法是什么,谢谢!
  • 这回答了你的问题了吗? How do I concatenate two lists in Python?
  • 如果您分享来自u.txt 的示例、y 中的 json/对象以及所需的结果,将会很有帮助。

标签: python list


【解决方案1】:

这接近您的预期吗?

遍历 un 的索引。连接两个列表中当前索引处的元素并追加到列表k

import re
import requests

t=[]
u = []
n = []
k = []
exclude_banned = True

with open("u.txt", "r") as f:
    h = f.readlines()[0:100]
    for sub in h:
        t.append(re.sub('
', '', sub))
    print(type(t))
    print(t)
    r = requests.post('https://users.roblox.com/v1/usernames/users', json={'usernames': t, 'excludeBannedUsers': exclude_banned})
    print(r.json())
    y = r.json()['data']
    u.extend([f['id'] for f in y])
    n.extend([f['name'] for f in y])

    # Concatenate elements in u and n using a loop
    for i in range(len(u)):
        k.append(f'{u[i]}:{n[i]}')

    print(k)

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2020-01-27
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多