Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to write strings like a<b<c in a text file from the dictionary PDP_data_listt, which looks like this:

PDP_data_listt = {
"['2','3','4']": [
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', -1)]
),
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', -1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', -1)]
),
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', -1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', -1)]
)
],
"['2', '3', '5']": [
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)]
),
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)]
),
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)]
),
(
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)],
[('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)]
)
]
}

Here the key "['2','3','4']" consists nine lists of tuples' lists. These lists describe the comparative relation between the key's elements.

For instance, the first list [('a', 'b', -1), ('a', 'c', -1), ('b', 'c', 1)] shows the relations as a<b, a<c, and b>c. Collectively, we can say that a<c<b or '2'<'4'<'3'. The value -1 shows that the first element a is less than the second element b. The value '1' shows that the first element is greater than the second element. A value '0' shows that both elements are equal. Now the problem is that I want to save the combined relation a<c<b or 2<4<3 in the text file for each key of the dictionary, instead of saving the individual relations.

How could I save the combined relation in a text file in python?

What I have tried:

I have already saving the individual relations using the following code.

for PDP_fragment in PDP_data_listt:  # for each fragment a different text file

    text_file = open(str(name) + '/PDP_ordering/' + (str(file_n)+' cars '+ str(PDP_fragment) + '.txt'),
                     'w')  # the writing of a file
    text_file.write(str(PDP_fragment) + '= [a, b, c, etc.]' + '\n\n\n')
    for matrix in PDP_data_listt[PDP_fragment]:  # add every matrix of the fragment to the file
        if PDP_m=='static':
            text_file.write( 'at '+str('timestamp')'+'\n')
            for d_in in range(len(matrix)):
                text_file.write('d' + str(d_in + 1) + ': ' + str(matrix[d_in]) + '\n\n')  # the matrix is printed
            text_file.write('\n\n') # the matrix is printed


Currently, I am writing only the individual relations but couldn't find a way to write the combined relation.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900