[英]How to make two Lists correspond and create a deck
So basically, I have a file that has ranks, power and card.所以基本上,我有一个有等级、权力和卡片的文件。 I made those go into their own lists.
我将这些 go 放入他们自己的列表中。 So I have a list for ranks, power and card.
所以我有一个等级、力量和卡片的清单。 I want to make the rank list and the card list correspond so that it outputs the ranks the number of cards it has.
我想让等级列表和卡片列表对应,以便它输出它拥有的卡片数量的等级。 For example I have a list called
name = [Jake, Blake, Sam, Adam and Dino]
and a list called num = [2,3,1,5,4]
.例如,我有一个名为
name = [Jake, Blake, Sam, Adam and Dino]
的列表和一个名为num = [2,3,1,5,4]
的列表。 I want name and num to correspond so Jake and 2, and Blake and 3 etc. But when it outputs I want Jake to show up 2 times and Blake to show up 3 times.我希望 name 和 num 对应,所以 Jake 和 2,以及 Blake 和 3 等。但是当它输出时,我希望 Jake 出现 2 次,Blake 出现 3 次。 This is what I have done so far
这是我到目前为止所做的
numFile = open("ranks.dat", "r")
rankList = []
powerList = []
cardList = []
while True:
text = numFile.readline()
#rstrip removes the newline character read at the end of the line
text = text.rstrip("\n")
if text=="":
break
info = text.split(",")
rankList.append(info[0])
powerList.append(int(info[1]))
cardList.append(int(info[2]))
deck = cardList*(int(rankList))* This is what I tried
numFile.close()
print(80*"=")
print("Level 3 Build Deck")
print(80*"=")
print(deck)*
Here I have the file(Rank, Power, Card):在这里我有文件(等级,力量,卡片):
Admiral,30,1
General,25,1
Colonel,20,2
Major,15,2
Captain,10,2
Lieutenant,7,2
Sergeant,5,4
Corporal,3,6
Private,1,10
You can use zip
easily like so:您可以像这样轻松使用
zip
:
cardList = ["Jake", "Blake", "Sam", "Adam", "Dino"]
rankList = [2, 3, 1, 5, 4]
deck = list(zip(cardList, rankList))
print(deck)
#[('Jake', 2), ('Blake', 3), ('Sam', 1), ('Adam', 5), ('Dino', 4)]
I think you are trying to make a deck with repeat cards using the card list... Here is an idea using your code as a basis.我认为您正在尝试使用卡片列表制作带有重复卡片的套牌......这是一个使用您的代码作为基础的想法。 This will be a list of tuples where each tuple is the (rank, power):
这将是一个元组列表,其中每个元组是(秩,幂):
numFile = open("ranks.dat", "r")
# rankList = []
# powerList = []
# cardList = [] # prolly not needed
card_deck = []
while True:
text = numFile.readline()
#rstrip removes the newline character read at the end of the line
text = text.rstrip("\n")
if text=="":
break
info = text.split(",")
rank = info[0]
power = int(info[1])
card_count = int(info[2])
for i in range(card_count): # a small loop to add card_count duplicates to the deck
card_deck.append( (rank, power) ) # add a tuple of the (rank, power)
numFile.close()
print(80*"=")
print("Level 3 Build Deck")
print(80*"=")
print(card_deck)
Yields:产量:
================================================================================
Level 3 Build Deck
================================================================================
[('Admiral', 30), ('General', 25), ('Colonel', 20), ('Colonel', 20), ('Major', 15), ('Major', 15), ('Captain', 10), ('Captain', 10), ('Lieutenant', 7), ('Lieutenant', 7), ('Sergeant', 5), ('Sergeant', 5), ('Sergeant', 5), ('Sergeant', 5), ('Corporal', 3), ('Corporal', 3), ('Corporal', 3), ('Corporal', 3), ('Corporal', 3), ('Corporal', 3), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1), ('Private', 1)]
[Finished in 0.0s]
To get cards to repeat in list让卡片在列表中重复
deck = []
for i in range(len(rankList)):
rank = rankList[i]
number = cardList[i]
for repeat in range(number):
deck.append(rank)
print(deck)
Complete Code完整代码
numFile = open("ranks.dat", "r")
rankList = []
powerList = []
cardList = []
while True:
text = numFile.readline()
#rstrip removes the newline character read at the end of the line
text = text.rstrip("\n")
if text=="":
break
info = text.split(",")
rankList.append(info[0])
powerList.append(int(info[1]))
cardList.append(int(info[2]))
numFile.close()
print(80*"=")
print("Level 3 Build Deck")
print(80*"=")
deck = []
for i in range(len(rankList)):
rank = rankList[i]
number = cardList[i]
for repeat in range(number):
deck.append(rank)
print(deck)
Output Output
================================================================================
Level 3 Build Deck
================================================================================
['Admiral', 'General', 'Colonel', 'Colonel', 'Major', 'Major', 'Captain', 'Captain', 'Lieutenant', 'Lieutenan
t', 'Sergeant', 'Sergeant', 'Sergeant', 'Sergeant', 'Corporal', 'Corporal', 'Corporal', 'Corporal', 'Corporal
', 'Corporal', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Private', 'Priva
te', 'Private']
Something like this?像这样的东西?
name = ["Jake", "Blake", "Sam", "Adam", "Dino"]
number = [2, 3, 1, 5, 4]
for n, num in zip(name, number):
print(n * num)
Giving给予
JakeJake
BlakeBlakeBlake
Sam
AdamAdamAdamAdamAdam
DinoDinoDinoDino
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.