python 我想将两个列表合并在一起

xoefb8l8  于 2023-02-21  发布在  Python
关注(0)|答案(3)|浏览(125)
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

ecfsfe2w

ecfsfe2w1#

这是否更接近您的预期?
遍历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('\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])

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

    print(k)
z31licg0

z31licg02#

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])

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

    print(k)
yvt65v4c

yvt65v4c3#

我建议做一些简化。例如,如果你对re所做的只是去掉后面的换行符,那么有一个更好的方法。在那之后,我认为你真的需要考虑列表理解来构建最终的用户数据。
退房:

import requests

## ---------------------------
## Read usernames out of a text file.
## ---------------------------
with open("u.txt", "r") as file_in:
    post_data = {
        "usernames": [username.strip() for username in file_in][:100],
        "excludeBannedUsers": True
    }
## ---------------------------

post_url = "https://users.roblox.com/v1/usernames/users"
api_response = requests.post(post_url, post_data)

if not api_response.ok():
    print("something went wrong")
    exit()

## ---------------------------
## Build a list of dictionaries based on the api data
## ---------------------------
user_data = [
    {user["id"]: user["name"]}
    for user
    in api_response.json()["data"]
]
## ---------------------------

## ---------------------------
## Print the results
## ---------------------------
import json
for user in user_data:
    print(json.dumps(user, indent=4))
## ---------------------------

相关问题