Список Python изменится, прежде чем я скажу им [закрыто]Python

Программы на Python
Ответить
Anonymous
 Список Python изменится, прежде чем я скажу им [закрыто]

Сообщение Anonymous »

У меня есть список, и я хочу добавить в него элементы после его обработки
def f(x):
some processing including api calls and executing some command line subprocesses.
return f(x)

A = [{"a1":a1, "a2":a2 .... },{"b1":b1 ....},...,{"n1:n1...}]

def Z(A):
X = []
for a in A:
x = f(a)
X.append(x)

return X

Z(A)

Однако тогда он просто возвращает мне список f(n) из len(A) раз

[f (n), f(n), f(n),....]

Я ожидаю, что [f(a), f(b) ,...f(n)]
добавление операторов печати:
def f(x):
some processing including api calls and executing some command line subprocesses.
return f(x)

A = [{"a1":a1, "a2":a2 .... },{"b1":b1 ....},...,{"n1:n1...}]

def Z(A):
X = []
for a in A:
print(X)
x = f(a)
print(X)
X.append(x)
print(X)
return X

Z(A)

для первой итерации я получаю ожидаемый результат.

[]


[]


[f(a)]

однако для следующей итерации я получить

[f(a)]


[f(b)]


[f(b),f(b)]

и третий

[f(b),f(b)]


[f(c),f( в)]


[f(c),f(c),f(c)]
и т. д. и т. д.
Я ожидал список
Почему Python копирует до того, как я об этом скажу.
Полный код довольно длинный:
import berserk
import pandas as pd
from subprocess import Popen, PIPE
client = berserk.Client()
prevelance = 400
initialmovesindex=2
initialuci = ["e2e4"]
e2e4amount = 3546806334
cutoff = e2e4amount/prevelance

testnodes = [{'uci': 'e7e5', 'san': 'e5', 'averageRating': 1497, 'white': 740631862, 'draws': 55281626, 'black': 656024328, 'game': None, 'movepath': 'e2e4 e7e5', 'total': 1451937816},
{'uci': 'c7c5', 'san': 'c5', 'averageRating': 1738, 'white': 304672991, 'draws': 25142786, 'black': 307273681, 'game': None, 'movepath': 'e2e4 c7c5', 'total': 637089458},
{'uci': 'd7d5', 'san': 'd5', 'averageRating': 1593, 'white': 182838137, 'draws': 14490805, 'black': 178037660, 'game': None, 'movepath': 'e2e4 d7d5', 'total': 375366602},
{'uci': 'e7e6', 'san': 'e6', 'averageRating': 1682, 'white': 173594349, 'draws': 14206159, 'black': 173658709, 'game': None, 'movepath': 'e2e4 e7e6', 'total': 361459217},
{'uci': 'c7c6', 'san': 'c6', 'averageRating': 1718, 'white': 116560750, 'draws': 10250312, 'black': 119858154, 'game': None, 'movepath': 'e2e4 c7c6', 'total': 246669216},
{'uci': 'd7d6', 'san': 'd6', 'averageRating': 1686, 'white': 64571700, 'draws': 5039404, 'black': 61973697, 'game': None, 'movepath': 'e2e4 d7d6', 'total': 131584801},
{'uci': 'g7g6', 'san': 'g6', 'averageRating': 1685, 'white': 58485806, 'draws': 4469212, 'black': 57602500, 'game': None, 'movepath': 'e2e4 g7g6', 'total': 120557518},
{'uci': 'b7b6', 'san': 'b6', 'averageRating': 1622, 'white': 36366304, 'draws': 2489351, 'black': 33996613, 'game': None, 'movepath': 'e2e4 b7b6', 'total': 72852268},
{'uci': 'g8f6', 'san': 'Nf6', 'averageRating': 1691, 'white': 34712390, 'draws': 2784049, 'black': 34355510, 'game': None, 'movepath': 'e2e4 g8f6', 'total': 71851949},
{'uci': 'b8c6', 'san': 'Nc6', 'averageRating': 1577, 'white': 27031281, 'draws': 1990743, 'black': 24932503, 'game': None, 'movepath': 'e2e4 b8c6', 'total': 53954527},
{'uci': 'a7a6', 'san': 'a6', 'averageRating': 1607, 'white': 4699541, 'draws': 326685, 'black': 4260966, 'game': None, 'movepath': 'e2e4 a7a6', 'total': 9287192}]
emptynd = {'uci': '0000', 'san': '0000', 'averageRating': 0000, 'white': 0000, 'draws': 0000, 'black': 0000, 'game': None,"movepath": ""}

def generateBestResponse(mv: dict, start=False,white=True)->dict:
moves = mv["movepath"]
stri = f"position startpos moves {moves}\n"
p = Popen("./stockfishavx2/stockfish-macos-x86-64-avx2",stdout=PIPE, stdin=PIPE)
p.stdin.write(bytes(stri, "utf8"))
p.stdin.write (b"go depth 14\n")
p.stdin.flush()
data = p.communicate()

p.terminate()
d=data[0].decode("utf-8").splitlines()
bm = list(filter(lambda x: x.startswith('bestmove'), d))
if (len(bm)>1):
print("error bm > 1")
return emptynd
else:
bm_str = str(bm[0])
bm_moves_str = bm_str.split()
bm = bm_moves_str[1]

#get data for played move
bmmovepath = moves + " " + bm
bmmovelist = bmmovepath.split()

node = emptynd

respo = client.opening_explorer.get_lichess_games(play=bmmovelist,moves=0,top_games=0,recent_games=0)

node["white"] = respo['white']
node["black"] = respo['black']
node["draws"] = respo['draws']
node["uci"] = bm
node["movepath"] = bmmovepath

if (mv["total"]

Подробнее здесь: https://stackoverflow.com/questions/792 ... ll-them-to
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»