Jump to content

python list question

FreQUENCY

Hello. I want to make a calculation with the lists items. If the item[x] is a 2 digit number like 11 make a sum of them and replace item[X] with the calculation,which in case is 2(1+1).Essentially divide the item to 2 INT numbers and sum them replacing the list item with the result. Any ideas how?

 

Link to comment
Share on other sites

Link to post
Share on other sites

>>> n = 123456789
>>> [(n//(10**i))%10 for i in range(math.ceil(math.log(n, 10))-1, -1, -1)]

//Output will be


[1, 2, 3, 4, 5, 6, 7, 8, 9]

This is a way of doing it without stringing the list/variable first.

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
Share on other sites

Link to post
Share on other sites

@Hugs12343 I meant that if a list has an item "12",then this will be replaced with the sum of these 2 digits. So it will be 3

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, FreQUENCY said:

@Hugs12343 I meant that if a list has an item "12",then this will be replaced with the sum of these 2 digits. So it will be 3

So what I posted before will get you the separated string.


You just need to convert it back to an int 

 

int(x)

then add the items in the list together

 

sum(listx)

 

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
Share on other sites

Link to post
Share on other sites

You could convert them to a string and split out each character then convert each character to an int and sum them or you can do it mathmatically using the modulus operator '%'. The first way would probably be easier to do... lol. Also, if the sum is still greater than 9 then just do it again if that's the required result.

Link to comment
Share on other sites

Link to post
Share on other sites

@CPBuchholz @Hugs12343 

 
def digit_sum(LIST):
    for i in range(len(LIST)):
      value = str(LIST[i])
      digits = [int(x) for x in value]
      credit_card.append(sum(digits))
 
 
Done ;),with a list append as well
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×