简体   繁体   中英

Python, How to concatenate list as index?

a=[1,2,3,4]

b=[5,6,7,8]

Desired outcome from some sort of concatenation.

c=[[1,2,3,4],[5,6,7,8]]

I need this to iterable as to concatenate a different b to c numerous times whilst retaining [[,,,],[,,,],[,,,],.......]

In python you can append anything you want into a list. So for your example, we will start with an empty list c and append a and b . This method will continue to work for an arbitrary number of lists:

a = [1,2,3,4]
b = [5,6,7,8]
c = []

# Now we append
c.append(a)
c.append(b)

If we wanted to do this manually, or as a once only for fixed numbers of lists, we could just defined c as the list containing a and b like so:

c = [a, b]

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