Python Forum
Concatinating two strings to make a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatinating two strings to make a variable
#1
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.
Reply
#2
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
Reply
#3
Perfect! Thanks ...

and ok

first post
Reply
#4
(Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...

in any case, I suspect design flaw
Reply
#5
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,773 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  extract particular strings into a variable lokesh 3 2,122 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Finding multiple strings between the two same strings Slither 1 2,521 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to I define a variable between strings in telnetlib write? Fez 2 3,402 May-02-2019, 06:53 PM
Last Post: Fez
  How do you make functions that take a variable that is not defined? magic 6 4,425 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Trying to make two different strings from the same root De1uge 0 2,036 Jun-09-2018, 04:06 PM
Last Post: De1uge
  Spyder IDE - make variable explorer to follow the color scheme of the Editor Antonio 0 5,453 May-05-2018, 10:20 PM
Last Post: Antonio
  lists, strings, and byte strings Skaperen 2 4,232 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020