0
import random

A = ['a', 'b', 'c', 'd', 'e']
B = []

count = 1 
while True :
    B.append(random.choice(A))
    print(B)
    repeat = B.count('a')
    if repeat >= 2 :
        print("Waring!!!")
    if count >= 10 :
        break
    count += 1

I wrote upper code. but I want add some options.

  1. I want create B as two-dimensional list having 5 row. if count 6, for example, code can prints B = [[a, b, c, a, b], [e]]
  2. I want to print a Warning message if there is a letter occuring two or more in times in each column. for example, if B = [[a, b, c, a, b], [e]] in count 6, code will prints 'Warning, a, b duplicate in 1 column'. or if B = [[a, b, c, a, b], [e, e, a, d, e]] in count 10, code will prints 'Warning, a, b duplicated in 1 column' and 'Waring, e duplicate in 2 column.'.

I always appreciate your help.

kubatucka
  • 555
  • 5
  • 15
duckchun
  • 7
  • 2
  • In python, the general convention for list of list is that the inner list is interpreted as a line. So the outer list is a list of lines. Numpy and many other packages share that convention. So in your question I assume you want to print a warning if there are duplicates in the same line. – kubatucka Oct 01 '21 at 09:11
  • And please do a quick search before posting a new question. There are hundres of google results and even stackoverflow posts on detecting duplictes : https://stackoverflow.com/questions/1541797/how-do-i-check-if-there-are-duplicates-in-a-flat-list – kubatucka Oct 01 '21 at 09:14
  • @kubatucka never heard of that "convention": in python lines are lines of a file like in `readlines` or `splitlines`. numpy works with arrays and not lists and i never heard of an array of lines. and i don't even see why would numpy be related to the current question – diggusbickus Oct 01 '21 at 09:47
  • @diggusbickus Numpy stores data in row major order. And generally people code that way even in pure python. When creating a a 2d array with list of lists in python, its often [line,line,line ...]. Such that B[i][j] would yield the value on row i column j. https://stackoverflow.com/questions/20341614/numpy-array-row-major-and-column-major. – kubatucka Oct 01 '21 at 10:09
  • @kubatucka is that link for confirming that no one uses the term "line" for list or array? why are you misleading newcomers with terms and "conventions" you're the only one using them? – diggusbickus Oct 01 '21 at 10:16
  • 1
    Maybe I could have said row instead of line. – kubatucka Oct 01 '21 at 10:24

4 Answers4

0
import random
from collections import defaultdict

A = ['a', 'b', 'c', 'd', 'e']

def detect_duplicates(row, row_index):
    duplicates = defaultdict(int)
    for e in row:
        duplicates[e] += 1
    duplicates = dict(filter(lambda x: x[1] > 1, duplicates.items()))
    for e in duplicates:
        print(f'Warning, {e} duplicate in {row_index} column')

def generate_2d(total_size, row_size):
    result = []
    row_count = total_size // row_size
    tail = total_size % row_size

    for i in range(row_count):
        row = random.choices(A, k=row_size)
        detect_duplicates(row, i)
        result.append(row)
    
    if tail:
        row = random.choices(A, k=tail)
        detect_duplicates(tail, row_count+1)
        result.append(tail)
    
    return result

B = generate_2d(total_size=6, row_size=5)
Brandon
  • 708
  • 6
  • 13
0

Maybe you should try something like this

import random

A = ['a', 'b', 'c', 'd', 'e']
B = []
char_count = 10
repeats = []
for count in range(char_count):
    index = count // 5
    if count % 5 == 0:
        B.append([])
        repeats.append([])
    new_char = random.choice(A)
    if new_char in B[index]:
        repeats[index].append(new_char)
    B[index].append(new_char)
print(B)
for line_number in range(len(repeats)):
    print(f'Warning! {", ".join(repeats[line_number])}'
          ' duplicate in line {line_number}')
Sergey
  • 1
0

i made you a commented version if you need some help understanding what the code does

import random
"""
    you want to display one message for all your warnings
    so build your list first and check it when it's built
"""

A = ['a', 'b', 'c', 'd', 'e']
B = []

# needed to fill B with sublists
temp = []
# let for manage the counter in your behalf
# no need to increment or break
for count in range(10):
    temp.append(random.choice(A))
    # each time temp has 5 elements append to B and reset
    if count%5 == 4:
        B.append(temp)
        temp=[]
        
print(B)
    
# enumerate provides the indices of the list you're iterating
# we need it to get the column number
for column, sublist in enumerate(B):
    warning_list=[]
    # instead of just counting 'a's we use for to get all keys of A
    for key in A:
        if sublist.count(key) >= 2 :
            warning_list.append(key)
    if warning_list:
        print(f'Warning, {", ".join(warning_list)} duplicated in column {column+1}')
diggusbickus
  • 1,537
  • 3
  • 7
  • 15
0

You can try simple as this

def ifdup(l):
    for i in l:
        if l.count(i) > 1:
            return i

import random

A = ['a', 'b', 'c', 'd', 'e']
B = []

count = 1
d_size = 5
d_idx = 0
B.append([])

while True:
    B[d_idx].append(random.choice(A))
    print(B)
    
    if ifdup(B[d_idx]):
        print('waring')
        
    if count > 10:
        break;
    count += 1
    
    if (count-1) % d_size == 0:
        d_idx += 1
        B.append([])

Output

[['d']] 
[['d', 'b']] 
[['d', 'b', 'a']]
[['d', 'b', 'a', 'b']] 
waring
[['d', 'b', 'a', 'b', 'a']] 
waring 
[['d', 'b', 'a', 'b', 'a'], ['e']]
[['d', 'b', 'a', 'b', 'a'], ['e', 'e']] 
waring 
[['d', 'b', 'a', 'b','a'], ['e', 'e', 'b']] 
waring