Simple Python List Concatenation - python

I have a list of strings, with each string as a character. They're in order of the word. How do I put each character all together in one string. Example:
list_characters = ['H', 'e', 'l', 'l', 'o']
To become:
'Hello'
Please help and thank you

In [1]: s = ['H', 'e', 'l', 'l', 'o']
In [2]: ''.join(s)
Out[2]: 'Hello'
The join method is a bit different from others you may be familiar with in that you put the element you want to use to 'join` the elements together first, and then call the method on that. Here are some more examples:
In [4]: print '\n'.join(s)
H
e
l
l
o
In [5]: ' '.join(s)
Out[5]: 'H e l l o'
In [6]: 'GOODBYE'.join(s)
Out[6]: 'HGOODBYEeGOODBYElGOODBYElGOODBYEo'
The join method accepts any 'iterable', which is anything you can 'iterate' over (such as a list, as in your example). Strings themselves are also iterables, so you could even do this:
In [7]: s = 'Hello'
In [8]: 'Z'.join(s)
Out[8]: 'HZeZlZlZo'

Although I highly recommend the join() method as mentioned by RocketDonkey above, another way would be this:
reduce(lambda acc,x:acc+x,['H', 'e', 'l', 'l', 'o'])

You need to use join() to concatenate all the elements of list into a string like this:
test = ['H', 'e', 'l' 'l', 'o']
''.join(test)
If you want to join with '-' character in between them, use this:
test = ['H', 'e', 'l' 'l', 'o']
'-'.join(test)

You can join the elements of list into a string by various methods:
By iterating through the list
str=""
for i in list:
str+=i
print(str) #hello
Using built in function 'join()'
list = ['h', 'e', 'l', 'l', 'o']
str=""
print("".join(list)) #hello
Using list comprehension
str = "".join([str(elem) for elem in list])
print(str) #hello
Using map() function
str = ''.join(map(str, list))
print(str) #hello
#Note: Last two methods can be used in case you have int elements also,
as Python doesn't allow concatenation of elements with different data types.

Related

words splitting to letters when making a list out of them in python

I am trying to create a list of words or phrases in python 3. For example;
a = ('Hello World')
when i am trying to convert it to a list, this happens,
list(a)
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
but i want it to be ['Hello World']
Please can anyone help?
You simply want to use [a] instead of list(a).
a='hello world'
create a list object then add the string object into list object using list built in function
n=[]
n.append(a)
print(n)

(In Python) What's the difference between list(string).reverse() and list(string)[::-1]?

I can execute both expressions in Python shell without error:
string = 'this is a string'
list(string)[::-1]
(output) ['g', 'n', 'i', 'r', 't', 's', ' ', 'a', ' ', 's', 'i', ' ', 's', 'i', 'h', 't']
list(string).reverse()
I can do:
string = ''.join(list(string)[::-1])
which effectively reverse the string in place. However when I do:
string = ''.join(list(string).reverse()
I got an error:
TypeError: can only join an iterable
So list(string).reverse() does not return an iterable but list(string)[::-1] does. Can someone help me understand the underlying differences?
list(string).reverse() modifies the list in place and returns None
So you are are doing:
"".join(None)
Hence the error.
list.reverse() mutates the list it is called from so the list is altered after the call, while sequence[::-1] creates a new list and returns it, so the original list is unaffected.
list.reverse is returning None so you don't need to assign it back, but, seq[::-1] needs to be assigned back, Example:
l=[1,2,3]
print(l.reverse())
print(l)
Output:
None
[3,2,1]
Example 2:
l=['a','b','c']
print(l[::-1])
print(l)
Output:
['c','b','a']
['a','b','c']
Example 2 needs to be assigned back

How to get all substrings in a list of characters (python)

I want to iterate over a list of characters
temp = ['h', 'e', 'l', 'l', 'o', '#', 'w', 'o', 'r', 'l', 'd']
so that I can obtain two strings, "hello" and "world"
My current way to do this is:
#temp is the name of the list
#temp2 is the starting index of the first alphabetical character found
for j in range(len(temp)):
if temp[j].isalpha() and temp[j-1] != '#':
temp2 = j
while (temp[temp2].isalpha() and temp2 < len(temp)-1:
temp2 += 1
print(temp[j:temp2+1])
j = temp2
The issue is that this prints out
['h', 'e', 'l', 'l', 'o']
['e', 'l', 'l', 'o']
['l', 'l', 'o']
['l', 'o']
['o']
etc. How can I print out only the full valid string?
Edit: I should have been more specific about what constitutes a "valid" string. A string is valid as long as all characters within it are either alphabetical or numerical. I didn't include the "isnumerical()" method within my check conditions because it isn't particularly relevant to the question.
If you want only hello and world and your words are always # seperated, you can easily do it by using join and split
>>> temp = ['h', 'e', 'l', 'l', 'o', '#', 'w', 'o', 'r', 'l', 'd']
>>> "".join(temp).split('#')
['hello', 'world']
Further more if you need to print the full valid string you need to
>>> t = "".join(temp).split('#')
>>> print(' '.join(t))
hello world
You can do it like this:
''.join(temp).split('#')
List has the method index which returns position of an element. You can use slicing to join the characters.
In [10]: temp = ['h', 'e', 'l', 'l', 'o', '#', 'w', 'o', 'r', 'l', 'd']
In [11]: pos = temp.index('#')
In [14]: ''.join(temp[:pos])
Out[14]: 'hello'
In [17]: ''.join(temp[pos+1:])
Out[17]: 'world'
An alternate, itertools-based solution:
>>> temp = ['h', 'e', 'l', 'l', 'o', '#', 'w', 'o', 'r', 'l', 'd']
>>> import itertools
>>> ["".join(str)
for isstr, str in itertools.groupby(temp, lambda c: c != '#')
if isstr]
['hello', 'world']
itertools.groupby is used to ... well ... group consecutive items depending if they are of not equal to #. The comprehension list will discard the sub-lists containing only # and join the non-# sub-lists.
The only advantage is that way, you don't have to build the full-string just to split it afterward. Probably only relevant if the string in really long.
If you just want alphas just use isalpha() replacing the # and any other non letters with a space and then split of you want a list of words:
print("".join(x if x.isalpha() else " " for x in temp).split())
If you want both words in a single string replace the # with a space and join using the conditional expression :
print("".join(x if x.isalpha() else " " for x in temp))
hello world
To do it using a loop like you own code just iterate over items and add to the output string is isalpha else add a space to the output:
out = ""
for s in temp:
if s.isalpha():
out += s
else:
out += " "
Using a loop to get a list of words:
words = []
out = ""
for s in temp:
if s.isalpha():
out += s
else:
words.append(out)
out = ""

pyside/pyqt: when converting str() to QTreeWidgetItem() the str() is shortened to the [0] of str()

s = 'someString'
s = QTreeWidgetItem(s)
print(s.text(0)) # 0 being 'column'
Output:
's'
It also appears as 's' if I run 'addChild(s)' to another QTreeWidgetItem.
QTreeWidgetItem construct is meant to be passed multiple strings (not a single one):
>>> s = QTreeWidgetItem(['someString', 'otherString'])
>>> print(s.text(0))
someString
>>> print(s.text(1))
otherString
Passing a single string object 'someString' is like passing a sequence with multiple single-character strings ['s', 'o', 'm', 'e', 'S', 't', 'r', 'i', 'n', 'g'].
If you want pass a single string, wrap in list or tuple:
s = QTreeWidgetItem(['someString'])

How do I split a string into a list of characters?

How do I split a string into a list of characters? str.split does not work.
"foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
Use the list constructor:
>>> list("foobar")
['f', 'o', 'o', 'b', 'a', 'r']
list builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at each iteration step.
You take the string and pass it to list()
s = "mystring"
l = list(s)
print l
You can also do it in this very simple way without list():
>>> [c for c in "foobar"]
['f', 'o', 'o', 'b', 'a', 'r']
If you want to process your String one character at a time. you have various options.
uhello = u'Hello\u0020World'
Using List comprehension:
print([x for x in uhello])
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using map:
print(list(map(lambda c2: c2, uhello)))
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Calling Built in list function:
print(list(uhello))
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using for loop:
for c in uhello:
print(c)
Output:
H
e
l
l
o
W
o
r
l
d
If you just need an array of chars:
arr = list(str)
If you want to split the str by a particular delimiter:
# str = "temp//temps" will will be ['temp', 'temps']
arr = str.split("//")
I explored another two ways to accomplish this task. It may be helpful for someone.
The first one is easy:
In [25]: a = []
In [26]: s = 'foobar'
In [27]: a += s
In [28]: a
Out[28]: ['f', 'o', 'o', 'b', 'a', 'r']
And the second one use map and lambda function. It may be appropriate for more complex tasks:
In [36]: s = 'foobar12'
In [37]: a = map(lambda c: c, s)
In [38]: a
Out[38]: ['f', 'o', 'o', 'b', 'a', 'r', '1', '2']
For example
# isdigit, isspace or another facilities such as regexp may be used
In [40]: a = map(lambda c: c if c.isalpha() else '', s)
In [41]: a
Out[41]: ['f', 'o', 'o', 'b', 'a', 'r', '', '']
See python docs for more methods
The task boils down to iterating over characters of the string and collecting them into a list. The most naïve solution would look like
result = []
for character in string:
result.append(character)
Of course, it can be shortened to just
result = [character for character in string]
but there still are shorter solutions that do the same thing.
list constructor can be used to convert any iterable (iterators, lists, tuples, string etc.) to list.
>>> list('abc')
['a', 'b', 'c']
The big plus is that it works the same in both Python 2 and Python 3.
Also, starting from Python 3.5 (thanks to the awesome PEP 448) it's now possible to build a list from any iterable by unpacking it to an empty list literal:
>>> [*'abc']
['a', 'b', 'c']
This is neater, and in some cases more efficient than calling list constructor directly.
I'd advise against using map-based approaches, because map does not return a list in Python 3. See How to use filter, map, and reduce in Python 3.
split() inbuilt function will only separate the value on the basis of certain condition but in the single word, it cannot fulfill the condition. So, it can be solved with the help of list(). It internally calls the Array and it will store the value on the basis of an array.
Suppose,
a = "bottle"
a.split() // will only return the word but not split the every single char.
a = "bottle"
list(a) // will separate ['b','o','t','t','l','e']
Unpack them:
word = "Paralelepipedo"
print([*word])
To split a string s, the easiest way is to pass it to list(). So,
s = 'abc'
s_l = list(s) # s_l is now ['a', 'b', 'c']
You can also use a list comprehension, which works but is not as concise as the above:
s_l = [c for c in s]
There are other ways, as well, but these should suffice.
Later, if you want to recombine them, a simple call to "".join(s_l) will return your list to all its former glory as a string...
You can use extend method in list operations as well.
>>> list1 = []
>>> list1.extend('somestring')
>>> list1
['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
If you wish to read only access to the string you can use array notation directly.
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> t = 'my string'
>>> t[1]
'y'
Could be useful for testing without using regexp.
Does the string contain an ending newline?
>>> t[-1] == '\n'
False
>>> t = 'my string\n'
>>> t[-1] == '\n'
True
Well, much as I like the list(s) version, here's another more verbose way I found (but it's cool so I thought I'd add it to the fray):
>>> text = "My hovercraft is full of eels"
>>> [text[i] for i in range(len(text))]
['M', 'y', ' ', 'h', 'o', 'v', 'e', 'r', 'c', 'r', 'a', 'f', 't', ' ', 'i', 's', ' ', 'f', 'u', 'l', 'l', ' ', 'o', 'f', ' ', 'e', 'e', 'l', 's']
from itertools import chain
string = 'your string'
chain(string)
similar to list(string) but returns a generator that is lazily evaluated at point of use, so memory efficient.
Here is a nice script that will help you find which method is most efficient for your case:
import timeit
from itertools import chain
string = "thisisthestringthatwewanttosplitintoalist"
def getCharList(str):
return list(str)
def getCharListComp(str):
return [char for char in str]
def getCharListMap(str):
return list(map(lambda c: c, str))
def getCharListForLoop(str):
list = []
for c in str:
list.append(c)
def getCharListUnpack(str):
return [*str]
def getCharListExtend(str):
list = []
return list.extend(str)
def getCharListChain(str):
return chain(str)
time_list = timeit.timeit(stmt='getCharList(string)', globals=globals(), number=1)
time_listcomp = timeit.timeit(stmt='getCharListComp(string)', globals=globals(), number=1)
time_listmap = timeit.timeit(stmt='getCharListMap(string)', globals=globals(), number=1)
time_listforloop = timeit.timeit(stmt='getCharListForLoop(string)', globals=globals(), number=1)
time_listunpack = timeit.timeit(stmt='getCharListUnpack(string)', globals=globals(), number=1)
time_listextend = timeit.timeit(stmt='getCharListExtend(string)', globals=globals(), number=1)
time_listchain = timeit.timeit(stmt='getCharListChain(string)', globals=globals(), number=1)
print(f"Execution time using list constructor is {time_list} seconds")
print(f"Execution time using list comprehension is {time_listcomp} seconds")
print(f"Execution time using map is {time_listmap} seconds")
print(f"Execution time using for loop is {time_listforloop} seconds")
print(f"Execution time using unpacking is {time_listunpack} seconds")
print(f"Execution time using extend is {time_listextend} seconds")
print(f"Execution time using chain is {time_listchain} seconds")

Categories