Can you extract two indexes from the list (using for...))

Asked 2 years ago, Updated 2 years ago, 40 views

I tried to combine Ji-Soo Young-Cheol-hee and extract her name (e.g. Ji-hee, Su-cheol, Young-soo, etc.). So the desired result is

 Jihee 40 150

Water iron 50 200

Youngsoo 45 190

.... That's what I wanted to get Can I pick two elements in the list? I wanted to do fori, jin hanguls I couldn't do that, so I rolled my head like below, but there's an error again.

import random
Hanguls = list ("Jisoo Young Chul Hee")
for i in hanguls:
    for j in hanguls.remove(i):
        name = i+j
        weight = random.randrange(40,100)
        height = random.randrange(140,200)

        print("{} {} {} \n".format(name, weight, height))

Below is the error.

Traceback (most recent call last):
  File "C:\Users\Taewook Kim\Desktop\Programming\PracticePython\practice.py", line 4, in <module>
    for j in hanguls.remove(i):
TypeError: 'NoneType' object is not iterable

list python

2022-09-20 17:37

2 Answers

I think this will work.

import random

Hanguls = "Ji-Soo Young Chul-hee."
a = []
for i in range(len(hanguls)):
    while 1:
        c = random.randrange(0, len(hanguls))
        if c != i:
            break
    name = hanguls[i] + hanguls[c]
    weight = random.randrange(40, 100)
    height = random.randrange(140, 200)
    print("{} {} {}\n".format(name, weight, height))

>> Jichel 95 159

Suzy 64 188

Youngji 66 164

Withdrawal 43159

HEECHUL 50145


2022-09-20 17:37

You can use permutations from itertools.

>>> h = list ("Jisoo Young Chul Hee")
>>> from itertools import permutations
>>> names = list(permutations(h, 2))
>>> names
[('Ji', 'Su'), ('Ji', 'Young', ('Ji', 'Cheol'), ('Ji', 'Hee'), ('Su', 'Ji', 'Young'), ('Ji', 'Ji', 'Ji', ('Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji', 'Ji',
>>> names = [ ''.join(e) for e in permutations(h, 2) ]
>>> names
["Ji-Soo", "Ji-Soo", "Ji-Soo", "Su-Soo", "Su-Soo", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Ji", "Cheol-Soo-Soo", "Cheol-Soo-Soo-Soo", "Young-Soo", "Young", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Soo", "Young-Ji", "Young-Ji",
>>> 


2022-09-20 17:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.