I have a object in python like below
contributor_detail=contributorId ',' contentFileName ',' productPriority
output =
CSFBW23_1194968,CSFBW23_1194968.pdf,10
CSFBW23_1194969,CSFBW23_1194968.pdf,11
CSFBW23_1194970,CSFBW23_1194968.pdf,13
I want to create a tuple out of it and add multiple object to that tuple .
The tuple that i need is like below
sorted_contributors=[('CSFBW23_1194968','CSFBW23_1194968.pdf', 10), ('CSFBW23_1194968','CSFBW23_1194968.pdf', 11), ('CSFBW23_1194969','CSFBW23_1194968.pdf', 13), ('CSFBW23_1194970','CSFBW23_1194968.pdf', 12), ('CSFBW23_1194971','CSFBW23_1194968.pdf', 9)]
I need tuple in above so that it can be sorted based on 3rd item in tuple which is number .
sorted_contributors.sort(key=itemgetter(1))
Please help to create such format in loop
CodePudding user response:
If I understand you correctly, it looks like you just need to stop making contributor_detail
and string as you are with contributorId ',' contentFileName ',' productPriority
. Assuming those values are strings and an int, you are joining them as a string with ,
. It really isn't very clear what you're after, but I suspect, what you want is:
conributor_detail = contributorId, contentFileName, productPriority
That yields a tuple, from which you can make a list.
from operator import itemgetter
contrib1 = "CSFBW23_1194968", "CSFBW23_1194968.pdf", 10
contrib2 = "CSFBW23_1194969", "CSFBW23_1194968.pdf", 11
contrib3 = "CSFBW23_1194970", "CSFBW23_1194968.pdf", 13
contributors = [contrib1, contrib2, contrib3]
sorted_contributors = sorted(contributors, key=itemgetter(2))
print(sorted_contributors)
Output:
[('CSFBW23_1194968', 'CSFBW23_1194968.pdf', 10), ('CSFBW23_1194969', 'CSFBW23_1194968.pdf', 11), ('CSFBW23_1194970', 'CSFBW23_1194968.pdf', 13)]