简体   繁体   中英

Copying from one file to another file and pairing in python

I have a file (say, file.txt) which looks like:

A,B,C,32
D,E,F,65
G,H,I,76
J,K,L,98
M,N,O,55
J,K,L,98
S,T,U,46
G,H,I,76

Now as there are 8 rows so I want to make 4 pairs. For the 1st time when it will take 2 rows, it will take the row with the highest 4th column value and the 2nd row can be anyone from the remaining 7 rows but self pairing is not allowed . That means [(J,K,L,98),(J,K,L,98)] can't be one pair. Suppose in the first iteration it has taken [(J,K,L,98),(S,T,U,46)] as a pair, so in the next iteration there will be rest of the 6 rows participating.

Below is how I was trying:

from random import choice   
file_name = 'file.txt'
fp = open(file_name)
val = []
for line in fp.readlines():
    val.append(line.replace("\n","").split(","))
    
val = sorted(val, key=lambda x: x[-1], reverse=True)

item_list = []
for i in val:
    i = list(map(str, i))
    s = ','.join(i).replace("\n","")
    item_list.append(s)
print(*item_list,sep='\n')

list1=[]   
for i in val:
    list1.append(i[-1])
print(list1)
p=max(list1)
print(max(list1))

final_list=[]
for i in val:
    if(i[-1]==p):
        final_list.append(i) 
    elif(i[-1]==choice(list1)):
        final_list.append(i)
        
        
print(final_list)

Please help me out.

My interpretation of the problem is that you need to take a row from the file (table) with the highest numeric value in the 4th token. You then want to take another row at random and make that into a pair (a list of tuples). Key rule is that no pair can have equal tuples. I propose this:

from random import choice

# handles each row from the file creating an appropriately structured tuple
def mfunc(s):
    t = s.strip().split(',')
    t[-1] = int(t[-1])
    return tuple(t)

# selects a random tuple from the given list ensuring that the selection cannot match the reference tuple
def gar(lst, ref=None):
    rv = choice([e for e in lst if e != ref]) if ref else choice(lst)
    lst.remove(rv)
    return rv

with open('file.txt') as infile:
    rows = sorted(list(map(mfunc, infile.readlines())), key=lambda x: x[-1])
    output = []
    t = rows[-1] # get row with highest value
    rows.pop(-1) # then remove it
    output.append([t, gar(rows, t)])
    while len(rows) > 1:
        a = gar(rows)
        output.append([a, gar(rows, a)])
    print(output)

Output (example):

[[('J', 'K', 'L', 98), ('M', 'N', 'O', 55)], [('S', 'T', 'U', 46), ('A', 'B', 'C', 32)], [('G', 'H', 'I', 76), ('D', 'E', 'F', 65)], [('G', 'H', 'I', 76), ('J', 'K', 'L', 98)]]

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