gpt4 book ai didi

python - 从循环中的列表中删除元素

转载 作者:行者123 更新时间:2023-12-03 08:19:29 24 4
gpt4 key购买 nike

import stdio
import sys
import random

SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen ", "King", "Ace"]


rank = random . randrange (0 , len( RANKS ))
suit = random . randrange (0 , len( SUITS ))
stdio . writeln ( RANKS [ rank ] + "of" + SUITS [ suit ])

deck = []
for rank in RANKS :
for suit in SUITS :
card = rank + "of" + suit
deck += [ card ]


n = len ( deck )
for i in range ( n ):
r = random . randrange (i , n)
temp = deck [r]
deck [r] = deck [i]
deck [i] = temp


h = []

b = int(sys.argv[1])

k = 1
for l in range(b):
while k <= b:
f = random.randrange(n)
h += [deck[f]]
deck.pop([deck[f]]) # this line is the problem, i wnat to move [deck[f]], from deck but getting a
type eror
k += 1
print(h)

# 这是我的命令提示符

文件“C:\Users\USER\Desktop\app\pokerhands.py”,第 37 行,在
甲板.pop([甲板[f]])
TypeError:“列表”对象不能解释为整数

最佳答案

而不是 deck.pop([deck[f]]) , 试试 deck.pop(f) .
list.pop(index)删除 index 处的元素.您正在尝试使用包含字符串作为索引的列表,而不是整数。

关于python - 从循环中的列表中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61026995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com