how to split string and assign it to arey with adding some parts in python

Question:

I have some string which contains parts separated by commas and need to add some part to each and assign all to array of variables.

the string looks like

chp_algos = 'AES256_SSE','AES128_CBC','AES64_CBC','AES33_CBC'

I want to put in array which looks like:

arr = [
  [AES128_CBC],
  [AES128_CBC_fer],
  [AES128_SSE],
  [AES128_SSE_fer],
  [AES64_CBC],
  [AES64_CBC_fer],
  [AES33_CBC],
  [AES33_CBC_fer]
]

and I want to map the following final result to db

f = 'AES128_CBC_fer AES128_SSE_fer AES64_CBC_fer AES33_CBC_fer'
Asked By: mick

||

Answers:

You can do this by sorting chp_algos then using f strings in a generator expression

>>> ' '.join(f'{i}_fer' for i in sorted(chp_algos))
'AES128_CBC_fer AES256_SSE_fer AES33_CBC_fer AES64_CBC_fer'
Answered By: Cory Kramer

As written in the question, chp_algos is a tuple, not a string. So, it is already "split"

I’d recommend not using a list of lists. Just create a list of strings.

from itertools import chain
arr = list(chain.from_iterable([x, x + '_fer'] for x in chp_algos))

Output

['AES256_SSE',
 'AES256_SSE_fer',
 'AES128_CBC',
 'AES128_CBC_fer',
 'AES64_CBC',
 'AES64_CBC_fer',
 'AES33_CBC',
 'AES33_CBC_fer']

With that, you can filter.

But you could also just skip arr and build a new list from concatenating to the original string values

f = ' '.join(x for x in arr if x.endswith('_fer'))
Answered By: OneCricketeer

Could you clarify your string chp_algos? The way you wrote it now it is not compatible with python.

Anyway, what you can do in your case, assuming that chp_algos is a string of the form chp_algos= "'AES256_SSE','AES128_CBC','AES64_CBC','AES33_CBC'", then you can split the string into a list of strings via chp_algos.split(",").

The argument of split() is the delimiter which should be used to split the string.

Now you have something like array = ["'AES256_SSE'", "'AES128_CBC'", "'AES64_CBC'", "'AES33_CBC'"].

To get the array that you want you can just do a simple loop through your array:

arr = []
for element in array:
    arr.append([element])
    arr.append([element + str("_fer")])

Now you might have some issues with the quotes (depends on how your data looks like). But these you can just remove by looking at the relevant indices of element. To do this just replace element in the code above by element[1:-2]. This removes the first and the last element of the string.

To get the f string in the very end, you can just loop through arr[1::2] which returns every 2nd element of arr starting at the second one (index 1).

Answered By: Patrick

Say we have a string:

s = 'AES256_SSE,AES128_CBC,AES64_CBC,AES33_CBC'

In order to replace commas with a space and append a suffix to each part:

' '.join(f'{p}_fer' for p in s.split(','))

As for an array:

def g(s):
    for s in s.split(','):
        yield s
        yield f'{s}_fer'

arr = [*g(s)]
Answered By: Vitalizzare
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.