Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python 3.x - How to write multi list in list of tuple

In the func function, I performed some processing on the oldlist[] and then saved the result in a buffer[] list. Now I want to save this buffer[]list to be the first one in a list of tuples. After that, a new values in buffer[] must be saved as a second list in tuples and so on. The output something like this

#list of tuple:
Ltuple=[(1,2,3),(66,53,6),(3,1,5,8,3)]

I do not know how to use list of tuple any advice please?

def func(oldlist):
    buffer = []
    buffer.append(oldlist[0])
    # some processing on the items of oldlist[]  
        buffer.append(oldlist[index])
    print(buffer)
    # Now here I need to append the buffer[] in list of tuple to be the
    #  first list in tuple and in next time the new list insert to be 
    #  the second list in list of tuple
    buffer.clear()


oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func() 

func(oldlist)
oldlist.clear()
question from:https://stackoverflow.com/questions/65875616/how-to-write-multi-list-in-list-of-tuple

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

1 Reply

0 votes
by (71.8m points)

Can you pass Ltuple to the function? If not it has to be made global or you need to return the buffer tuple I have modified the function below to take the list of tuples also.

def func(oldlist, list_of_tuples):
    buffer = []
    buffer.append(oldlist[0])
    # some processing on the items of oldlist[]  
        buffer.append(oldlist[index])
    print(buffer)
    # Now here I need to append the buffer[] in list of tuple to be the
    #  first list in tuple and in next time the new list insert to be 
    #  the second list in list of tuple
    list_of_tuples.append(tuple(buffer))
    buffer.clear()

oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func()

list_of_tuples = []
func(oldlist, list_of_tuples)
oldlist.clear()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...