Posts: 3
Threads: 1
Joined: Jun 2017
Jun-20-2017, 09:15 AM
(This post was last modified: Jun-20-2017, 09:20 AM by buran.)
I want to concatenate two strings so that they match predefined variables which can load them into a list, something like this ..
k = []
i0 = 'This'
i1 = 'is'
i2 = 'the'
i3 = 'string'
for x in range(3):
k.append('i' + str(x)) Result
Output: ['i0', 'i1', 'i2', 'i3']
Needed result
Output: ['This', 'is', 'my', 'string']
A simple one to start with I know but have tried a few different ways of doing this but have failed. Thanks in advance.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Jun-20-2017, 09:32 AM
(This post was last modified: Jun-20-2017, 09:32 AM by buran.)
Not sure what the point of all this is, but for sake of exercise you can do this by using locals() built-in function
# python3
i0 = 'This'
i1 = 'is'
i2 = 'the'
i3 = 'string'
# approach1
k = []
for x in range(4):
k.append(locals()['i{}'.format(x)])
print(k)
# approach2 list comprehension
another_k = [locals()['i{}'.format(x)] for x in range(4)]
print(another_k) Output: ['This', 'is', 'the', 'string']
['This', 'is', 'the', 'string']
>>>
Also, note the change I made to range
Posts: 3
Threads: 1
Joined: Jun 2017
Perfect! Thanks ...
and ok
first post
Posts: 8,156
Threads: 160
Joined: Sep 2016
(Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...
in any case, I suspect design flaw
Posts: 2,125
Threads: 11
Joined: May 2017
Jun-20-2017, 06:08 PM
(This post was last modified: Jun-20-2017, 06:09 PM by DeaD_EyE.)
(Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...
and ok
first post
Please don't do this. You're using the wrong data structure. Maybe you want to have a dict:
data = {'i0': 'This', 'i1': 'is', 'i2': 'the', 'i3': 'string'}
# direct access
print(data['i0'])
# iterating over values
# unordered
for value in data.values():
print(value)
# sorted access
# sorted sorts by first element, if the the yielded element is a list/tuple
for key, value in sorted(data.items()):
print(value)
# or preserve the order:
from collections import OrderedDict
data = [('i0', 'This'), ('i1', 'is'), ('i2', 'the'), ('i3', 'string')]
ordered_dict = OrderedDict(data)
for value in ordered_dict.values():
print(value) Since Python 3.6 dicts preserve the order, but we should not rely on it. Maybe with next version they guarantee it.
Currently it's just an implementation detail.
Posts: 687
Threads: 37
Joined: Sep 2016
(Jun-20-2017, 06:08 PM)DeaD_EyE Wrote: (Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...
and ok
first post
Please don't do this. You're using the wrong data structure. Maybe you want to have a dict:
data = {'i0': 'This', 'i1': 'is', 'i2': 'the', 'i3': 'string'}
# direct access
print(data['i0'])
# iterating over values
# unordered
for value in data.values():
print(value)
# sorted access
# sorted sorts by first element, if the the yielded element is a list/tuple
for key, value in sorted(data.items()):
print(value)
# or preserve the order:
from collections import OrderedDict
data = [('i0', 'This'), ('i1', 'is'), ('i2', 'the'), ('i3', 'string')]
ordered_dict = OrderedDict(data)
for value in ordered_dict.values():
print(value) Since Python 3.6 dicts preserve the order, but we should not rely on it. Maybe with next version they guarantee it.
Currently it's just an implementation detail.
... or even simpler, an array:
i = [ 'This','is','the', 'string' ] # Stylistically `i` is a bad name for an array
# direct access
print(i[0])
# iterating over value (always ordered)
for value in i:
print(value)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
|