How do I get data in tuples and tuples in lists?

2024/10/9 4:22:08

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

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.

printing this result gives:

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

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

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?

Thanks in advance!

Answer

Something like this?

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

Output:

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

Explanation:

[(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
https://en.xdnf.cn/q/118632.html

Related Q&A

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…

How to add and subtract in python

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering whats wrong with it?numberstoadd = input(…

Python: deferToThread XMLRPC Server - Twisted - Cherrypy?

This question is related to others I have asked on here, mainly regarding sorting huge sets of data in memory.Basically this is what I want / have:Twisted XMLRPC server running. This server keeps seve…

How do I make a linear gradient with Python Turtle?

Im currently trying to replicate this image: https://i.sstatic.net/fymWE.jpg Im trying to make that gradient in the background but I have zero clue how to do it and theres basically nothing on the inte…

Python - Converting an array to a list causes values to change

>>> import numpy as np >>> a=np.arange(0,2,0.2) >>> a array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8]) >>> a=a.tolist() >>> a [0.0, 0.2, …

Understand Python Function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to download linkedin (save as pdf option) using python

Image what i want to download.Image is of LinkedIn profile page of my friend i want to click on that save-as-pdf option for many users.can that be downloaded using python code? for different users? o…

My tkinter entry box is printing .!entry instead of what is entered

from tkinter import * def _name_():businessname=entry_bnprint(businessname) edit_bar=Tk() name=Label(edit_bar,text="Name:").grid(row=0) entry_bn=Entry(edit_bar) entry_bn.grid(row=0,column=1) …

How to get an average from a row then make a list out of it [duplicate]

This question already has answers here:Reading a CSV file, calculating averages and printing said averages(2 answers)Closed 6 years ago.If I have a csv data that gives two row values of:years grades 20…