populate a list / list of lists with no repeated data from a file

python list
python list of lists example
sort list python
append nested list python
list comprehension || python
python remove duplicates from list of lists
how to store values in list in python
how to remove duplicate data in python list

So I need to take a load of names from a file and create a list it currently works but the names should no repeat and I can figure this out I tried messing around with random.sample but as it is in a loop this becomes pointless.

  def open_cards(number_of_cards):
    cards = []
    txtfile="mytextfile.txt"
    file = open(txtfile).read().splitlines()
    for x in range (0, int(number_of_cards) * 2):
        listy = []
        xmen_name = random.choice(file)
        power = random.randint(0, 100)
        intelligence = random.randint(0, 50)
        weakness = random.randint(0, 10)
        spirit = random.randint(0, 10)
        listy.insert(0, xmen_name)
        listy.insert(1, power)
        listy.insert(2, intelligence)
        listy.insert(3, weakness)
        listy.insert(4, spirit)
        cards.append(listy)
    print("this is the card list prior to shuffle")
    print(cards)

If en entry should not be reated this sounds like a job for a dict. So:

cards = {}
for x in range (0, int(number_of_cards) * 2):
    # code
    cards[xmen_name] = (power, intelligence, weakness, spirit)

I would say use a tuple, I don't think XMen stats should be modified :)

Traceback (most recent call last): File "qchemOutputSearch.py", line Just make a new list to populate, if the item for your list is not yet in the new list input it, else just move on to the next item in your original list. set will automatically remove duplicates. list will cast it back to a list. for unhashable lists. In versions <= v1.9.2, each item for rbindlist should have the same number of columns as the first non empty item. rbind.data.table gained a fill argument to fill missing columns with NA in v1.9.2, which allowed for rbind () binding unequal number of columns.

You can use set to remove the duplicate values in list

Listy = [power, intelligence, weakness, spirit, weakness, power]
unique_listy = set(Listy)
print(unique_listy)

Output

{power, intelligence, weakness, spirit}

['Hi'] will create a list with single value, then we can multiply this list by 20. It will repeat the contents of list 20 times. So, lists content will be now  This does not work so for example it is a card game. the user selects how many cards they want for example 6 the code pulls 12 xmen names from the txt file and adds 1 to a new list then repeats creating the cards at the moment duplicates can be created which is bad.

I would use random.shuffle

Shuffling names first, then iterate

(I don't get why you create number_of_cards * 2 cards and not just number_of_cards)

def open_cards(number_of_cards):
    cards = []
    txtfile="mytextfile.txt"
    with open(txtfile) as f:
        names = list(f)
    random.shuffle(names)
    for i in range(number_of_cards * 2):
        listy = []
        xmen_name = names[i]
        power = random.randint(0, 100)
        intelligence = random.randint(0, 50)
        weakness = random.randint(0, 10)
        spirit = random.randint(0, 10)
        listy.append(xmen_name)
        listy.append(power)
        listy.append(intelligence)
        listy.append(weakness)
        listy.append(spirit)
        cards.append(listy)
    print("this is the card list prior to shuffle")
    print(cards)

The last thing you need is to populate the list upon first arriving at the client. two lists against any data source, instead of just the XML file in this example. You'd have duplicate populateListB methods injected into the page, which would not  When working with case-sensitive data such as passwords, user names or file names, you may need to get a list of case-sensitive distinct values. For this, use the following array formula, where A2:A10 is the source list, and B1 is the cell above the first cell of the distinct list: Array formula to get case-sensitive distinct values (requires

Finally, a list with no elements is called an empty list, and is denoted []. We have already seen that we can assign list values to variables or pass lists as (most recent call last): File "<interactive input>", line 1, in <module> IndexError: list index out of range Similarly, the * operator repeats a list a given number of times:. Select H2:H7 (your list source). Click the Formulas tab. In the Defined Names group, click the Create From Selection option. In the resulting dialog, check Top Row (if necessary), and Excel will

is this : Each member will send in a list of journals wanted to complete files, lists are in, it will be the endeavor to use the lists of duplicates to fill out the list of This clearing house is not for the sale or purchase of any books or journals, but​  For value lists with ODBC data, the No access privilege and Limited custom privilege are not supported. To prohibit a user from seeing ODBC data in a value list, you must enforce row-level security in the external SQL database. See Editing value list privileges.

in a list of journals wanted to complete files,»also a list of duplicates on hand. lists are in, it will be the endeavor to use the lists of duplicates to fill out the list of This clearing house is not for the sale or purchase of any books or journals,  In the File menu (along the left edge), click or tap New, and then click or tap SharePoint. Choose your SharePoint site from the Recent sites list or enter your site's url directly into the text box. Click or tap GO. Choose the main list from your SharePoint site, in this example, Assets. Click or tap the Connect button in the lower-right corner.

Comments
  • It is not a good solution as you probably won't have int(number_of_cards) * 2 cards at the end
  • the code distributes the cards to the use so say 6 for player and 6 for cpu if I use this it would mean player had 5 and cpu 6 for example.
  • Why don't you make file = open(txtfile).read().splitlines() a "set" and then pick from that so u will have unique choice every time
  • This does not work so for example it is a card game. the user selects how many cards they want for example 6 the code pulls 12 xmen names from the txt file and adds 1 to a new list then repeats creating the cards at the moment duplicates can be created which is bad.
  • Well you lost me there.
  • I think you doing the wrong way, you should create a card deck with unique xmens before the game starts.
  • Not really you create cards when user is picking cards from the deck. You should create cards before.