如何获取元组中的数据和列表中的元组? [英] How do I get data in tuples and tuples in lists?

查看:490
本文介绍了如何获取元组中的数据和列表中的元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清楚汽车进入虚构的曼哈顿的路线. 我已经定义了起始位置,即(1,2)(在二维网格中).

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).

manhattan=[[1, 2, 3, 4, 5],
        [6, 7, 8, 9, 10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20]]

car_in_manhattan=8
car_unmerged = ([(index, row.index(car_in_manhattan)) for index, row in enumerate(manhattan) if car_in_manhattan in row])
car=list(itertools.chain(*car_unmerged))

我这样做是因为我不想在列表中找到列表

i am doing this because I do not want a list in a list

route1=car

路线中的第一个位置是汽车的起点. 我启动一个for循环,查看哪个人离我的汽车最近.此人需要领取,该位置应附加到路线列表中.

The first position in the route is where the car starts. I start a for loop that looks at which person is closest to my car. This person needs to be picked up, this location should be appended to the list for the route.

打印此结果将得到:

[1, 2, (.,.), (.,.), (.,.), (.,.), (.,.)] 

括号中的点已正确填充,在此我故意将其留为空白".

the dots in the brackets are correctly filled, I have intentionally left them 'blank' here.

问题是,我不知道汽车的这个起始位置(1,2)像其他所有位置一样,会在括号之间.如果我是正确的,这与元组有关?

the problem is that I do not know how this starting position for the car, (1,2) can also be between brackets, just like all the other locations. This has to do with tuples, if I am correct?

提前谢谢!

推荐答案

像这样吗?

a= [1, 2, (3,4), (5,6)] 
b=[(a[0],a[1])] + a[2:]
print b

输出:

[(1, 2), (3, 4), (5, 6)]

说明:

[(a[0],a[1])] + a[2:]

(a[0],a[1])   --> Create a tuples from 1st and 2nd element of list a
[(a[0],a[1])] --> Enclose it in a list.
a[2:]   --------> Slice list a, extract all elements starting from index position 2

[(a[0],a[1])] + a[2:] --> add both the list

这篇关于如何获取元组中的数据和列表中的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆