Home > Software engineering >  How to find duplicate elements by each column in a two-dimensional list?
How to find duplicate elements by each column in a two-dimensional list?

Time:10-02

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.

CodePudding user response:

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)

CodePudding user response:

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}')

CodePudding user response:

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}')

CodePudding user response:

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
  • Related