Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

python - Converting a nested list into list shows unhashable type

How to convert a nested list into a separate list?

rec = [["A",["B","C","D"]],["B",["D","E","F"]],["C",["G","H","I"]]]

Desired output:

desired_output = [["A","B","C","D"],["B","D","E","F"],["C","G","H","I"]]

I've tried the below code, however, it throws a Type Error: - unhashable type: 'list'

data = list(set([tuple(row) for row in rec]))

Any help would be greatly appreciated.

Thanks in advance.

question from:https://stackoverflow.com/questions/65874146/converting-a-nested-list-into-list-shows-unhashable-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This would work in your case where there is a single element followed by a list at each entry:

[[elem] + lst for elem, lst in rec]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...