Create a new list according to item value

2024/11/16 10:49:50

I have a list like below.

  ['T46', 'T43', 'R45', 'R44', 'B46', 'B43', 'L45', 'L44', 'C46', 'C45']

where I want to group according to int value:

  [id][' ',' ',' ',' ',' ']   # AREA PATTERN [Top, Right, Bottom, Left, Center][46]['1','0','1','0','1']   #Top,Bottom,Center[45]['0','1','0','1','1']   #Right,Left,Center[43]['1','0','1','0','0']   #Top,Bottom[44]['0','1','0','1','0']   #Right,Left

Is this possible? What I tried so far is:

  id_area = []for a in area:id = a[1:3]areas = a[:1]if any(str(id) in s for s in area):id_area = #lost
Answer

I think this is what you are looking for?

In [1]: lst =  ['T46','T43','R45','R44','B46','B43','L45','L44', 'C46', 'C45']In [2]: [1 if x.endswith("46") else 0 for x in lst]
Out[2]: [1, 0, 0, 0, 1, 0, 0, 0, 1, 0]In [3]: [1 if x.endswith("43") else 0 for x in lst]
Out[3]: [0, 1, 0, 0, 0, 1, 0, 0, 0, 0]
https://en.xdnf.cn/q/119537.html

Related Q&A

Concatenating many time and date columns

I have many date and time column pairs (around 15 each) that share the same prefix, ie. SH or DEL. The columns are all of dtype object, ie. string. All the columns belong to the same Dataframe. Here is…

Python with ICS files and CSV [duplicate]

This question already has answers here:Parsing files (ics/ icalendar) using Python(6 answers)Closed 5 years ago.one friend ask me some help on a personnal project, but I have to admit my skills in Pyth…

loading my data in numpy genfromtxt get errors

I have my data file contain 7500 lines with :Y1C 1.53 -0.06 0.58 0.52 0.42 0.16 0.79 -0.6 -0.3 -0.78 -0.14 0.38 0.34 0.23 0.26 -1.8 -0.1 -0.17 0.3…

Install dlib with cuda support ubuntu 18.04

I have CUDA 9.0 and CUDNN 7.1 installed on Ubuntu 18.04(Linux mint 19). Tensorflow-gpu works fine on GPU(GTX 1080ti).Now i am trying to build dlib with CUDA support:sudo python3 setup.py install --yes …

View 3 dimensional Numpy array in Matplotlib and taking arguments from Keyboard or mouse

I have 3 dimensional data say (5,100,100). Now I would like to see them slice by slice upon hitting the down arrow button.

python default argument syntax error

I just wrote a small text class in python for a game written using pygame and for some reason my default arguments arent working. I tried looking at the python documentation to see if that might give m…

Variable not defined (Python)

FlightType=input("Which flight would you like to fly? Type 2 Seater, 4 Seater, or Historic.") # No validation included for the inputFlightLen=input("Would you like to book the 30 minu…

PyGame: draw.rect() has invalid parameters

Im trying to learn mouse events with PyGame, and Im trying to draw a box wherever the user clicks. Im setting a variable equal to pygame.mouse.get_pos(), and calling individual tuple members according …

Cant press enter in selenium2library

Im trying to make a test that will open Facebook, log in and search something. However Im having trouble getting Facebook to search. Selenium types whatever it needs in the search bar, but I cant find …

Converting string to datetime in Python using strptime

Im trying to convert the following String to datetime object in Python.datetime_object = datetime.strptime(Sat, 26 Nov 2016 15:17:00 +0000, %a, %b %d %Y %H:%c %z)I get the following error,File "&l…