简体   繁体   中英

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. 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] . 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. 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:

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

================================================================================
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM