Difference between string and list and usuage of join - python

I want to understand why and how python treats below example as string when it is actually a list. Does join(expression) treats expression as string?
EXAMPLE 1
available_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
print 'Available letters: ' + ''.join(available_letters)
When I check type of ' '.join(available_letters) ,it has type string.
But if I use
print 'Available letters: ' + available_letters
then I get error cannot concatenate a string and a list.

This is the reason:
>>> available_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> x = ' '.join(available_letters)
>>> available_letters
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> x
'a b c d e f g h i j k l m n o p q r s t u v w x y z'
>>> type(available_letters)
<type 'list'>
>>> type(x)
<type 'str'>
As you see above, the availabe_letters is a variable of list type itself. But when you call ' '.join method on it, its output is a string. concatenating of list and str is not correct, but concatenating of strs is okay.

Python3:
available_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
print ('Available letters: ', ''.join(available_letters))

Related

How can I let my output be printed in a single string instead of seperate letters?

So I wrote this code to get every single row in a grid
def rows(test):
r = []
for x in test:
r.append(x)
return str(r)
the grid is this btw
test = [["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
and after running rows(test), I get this
[['r', 'a', 'w', 'b', 'i', 't'], ['x', 'a', 'y', 'z', 'c', 'h'], ['p', 'q', 'b', 'e', 'i', 'e'], ['t', 'r', 's', 'b', 'o', 'g'], ['u', 'w', 'x', 'v', 'i', 't'], ['n', 'm', 'r', 'w', 'o', 't']]
but I want it to be
[['rawbit','xayzch','pqbeie','trsbog', 'uwxvit', 'nmrwot']
What should I change??
Use .join:
def rows(test):
r = list()
for row in test:
r.append("".join(row))
return r
>>> rows(test)
['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot']
You can use ''.join() like below:
>>> lst = [['r', 'a', 'w', 'b', 'i', 't'], ['x', 'a', 'y', 'z', 'c', 'h'], ['p', 'q', 'b', 'e', 'i', 'e'], ['t', 'r', 's', 'b', 'o', 'g'], ['u', 'w', 'x', 'v', 'i', 't'], ['n', 'm', 'r', 'w', 'o', 't']]
>>> list(map("".join, lst))
#OR
>>> [''.join(l) for l in lst]
['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot']
If you want as function:
def rows(test):
# return [''.join(t) for t in test]
# Or
return list(map(''.join, test))

Select n items by sequence of a list repeatedly in python

Say I have a long list:
>>> import string
>>> my_list = list(string.ascii_lowercase)
>>> my_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
I want to loop over this list and select n items by sequence repeatedly. E.g. if I want to select 5 items, then it should be like:
step 1: ['a', 'b', 'c', 'd', 'e']
step 2: ['f', 'g', 'h', 'i', 'j']
step 3: ['k', 'l', 'm', 'n', 'o']
step 4: ['p', 'q', 'r', 's', 't']
step 5: ['u', 'v', 'w', 'x', 'y']
step 6: ['z', 'a', 'b', 'c', 'd']
step 7: ['e', 'f', 'g', 'h', 'i']
......
So the point is: I want to make sure when I reach the last item of the list, the first items can be appended to the last one and the looping just keep going.
For appending the first items to the last items, I've tried something like this:
def loop_slicing(lst_, i):
""" Slice iterable repeatedly """
if i[0] > i[1]:
return [n for n in lst_[i[0]:]+lst_[:i[1]]]
else:
return lst_[i[0]:i[1]]
When I call this function, I can do this:
>>> loop_slicing(my_list, (0, 5))
['a', 'b', 'c', 'd', 'e']
>>> loop_slicing(my_list, (25, 4))
['z', 'a', 'b', 'c', 'd']
Where I can just make a generator which can generate 5 sequential numbers in range(0, 26) to loop over my_list and get 5 items each time.
I don't know if this is the best approach. So is there any more efficient way to do the stuff?
Using the itertools module you can cycle and slice a string via an infinite generator:
from itertools import cycle, islice
from string import ascii_lowercase
def gen(x, n):
c = cycle(x)
while True:
yield list(islice(c, n))
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
...
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
Debatably simpler solution using a list comprehension:
def batch_list(ns, batch_size):
return [ns[i:i+batch_size] for i in range(0, len(ns), batch_size)]
>>> batch_list('abcdefghijk', 3)
['abc', 'def', 'ghi', 'jk']
This is a simple construction that I find myself writing often when I want to batch some list of tasks to perform.
EDIT: Just realized the OP asked for the construction to cycle around to the beginning to complete the last batch if needed. This does not do that and will have the last batch truncated.
Thanks for asking,
I took some time to understand the objective of your algorithm but if you want to loop and save all of your sublists I think this should work :
def slicing_items(slc_len = 5, lst, iterate_num = 25):
# slc_len correspond to the number of slices, lst is the list of sequences
n = len(lst)
k = 1
p = k * slc_len
slicing_list = []
while k < iterate_num:
current_slice = []
if p >= n:
for i in range (1, p//n):
current_slice += lst #How many times we passed the length of the list
p = p % n #How many items remaining ?
current_slice += lst[-(slc_len-p):]
current_slice += lst[:p]
else:
current_slice = lst[p-slc_len:p]
k += 1
p += slc_len
slicing_list.append(current_slice)
return slicing_list
Output :
slicing_items(5,my_list,10)
>>> [['a', 'b', 'c', 'd', 'e'],
['f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm', 'n', 'o'],
['p', 'q', 'r', 's', 't'],
['u', 'v', 'w', 'x', 'y'],
['z', 'a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n'],
['o', 'p', 'q', 'r', 's']]
However if you just want the last slice over your iterate_num then your function should fit perfectly (maybe you should use slicing over than rewriting the list in your first boolean statement for rapidity)
Using generator and slicing:
from string import ascii_lowercase
def gen(x, n):
start, stop = 0, n
while True:
if start < stop:
yield list(x[start:stop])
else:
yield ((list(x[start:])) + (list(x[:stop])))
start = stop
stop = (stop + n) % len(x)
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
print(next(G))
print(next(G))
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
print(next(G))
OUTPUT :
['a', 'b', 'c', 'd', 'e']
['f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o']
['p', 'q', 'r', 's', 't']
['u', 'v', 'w', 'x', 'y']
['z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i']
This is a really interesting problem , if you want to "just" solve the problem go for itertools cycle approach , It have already in-built function, But if you want to enjoy the joy of algorithms building, Go with your own solution and try something :
Here i tried with recursion approach, As you said it will keep going so you have to handle recursion by setting your max limit:
import math
import sys
sys.setrecursionlimit(500)
data=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
try:
def recursive_approch(m, x, n, hold=0, value=0, left=0):
print(hold)
max_time = len(m) / n
max_t = int(math.modf(max_time)[1])
left = len(m) % n
if value == max_t:
if len(x) == left:
x = x + m[:-len(x)]
value = 0
left = 0
else:
hold = x[:n]
value = value + 1
return recursive_approch(m, x[n:], n, hold=hold, value=value, left=left)
return recursive_approch(m, x, n, hold=hold, value=value, left=left)
print(recursive_approch(data, data, 6))
except RecursionError:
print('maximum recursion')
You have to pass the number for slice so if you want to slice 6-6 then:
print(recursive_approch(data, data, 6))
output:
['a', 'b', 'c', 'd', 'e', 'f']
['g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r']
['s', 't', 'u', 'v', 'w', 'x']
['s', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
...................
If you want 3-3 then:
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h', 'i']
['j', 'k', 'l']
['m', 'n', 'o']
['p', 'q', 'r']
['s', 't', 'u']
['v', 'w', 'x']
['v', 'w', 'x']
['y', 'z', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
.......
if you pass 12 :
0
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
....

Delete a string from an array in Python

I'm writing some code in Python to read from a file some text, and make a 2-dimensional array from it. But when I make the array, in the last spot of the first 2 array(of three) there is : '\n', and I want delete it.
This is the file(data.txt):
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
And this is the Python code:
data = open("data.txt", mode="r")
arr = data.readlines()
for i in range(len(arr)):
arr[i] = list(arr[i].split(","))
#here I tryed to detele it
for i in range(len(arr)):
if arr[i][len(arr[i])-1] == '\\n':
del arr[len(arr[i])-1]
data.close()
This is the result of the code(but there is anyway '\n'):
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\n']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
How I could delete those?
Short solution using str.rstrip() and str.splitlines() functions:
with open('data.txt', 'r') as f:
items = [l.rstrip(',').split(',') for l in f.read().splitlines()]
print(items)
The output:
[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']]
You can use rstrip and list comprehension.
with open("data.txt", 'r', encoding="utf-8") as file:
array = [line.rstrip(',\n').split(',') for line in file]
You can filter the list and only keep values that are not "\n":
for i in range(len(arr)):
arr[i] = [b for b in arr[i] if b != "\n"]
You can just strip the \n as you read the lines:
arr = []
with open('data.txt', mode="r") as f:
for line in f.readlines():
arr.append(line.strip(',\n').split(','))
print arr

Python: Alphabet array sort

I was trying a sample exercise on regexes. To find all the letters of the alphabets. Sort the array, and finally eliminate all repetitions.
>>> letterRegex = re.compile(r'[a-z]')
>>> alphabets = letterRegex.findall("The quick brown fox jumped over the lazy dog")
>>> alphabets.sort()
>>> alphabets
['a', 'b', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'o', 'o', 'o', 'p', 'q', 'r', 'r', 't', 'u', 'u', 'v', 'w', 'x', 'y', 'z']
After doing the sort I tried to make a loop that'll eliminate all repetitions in the array.
e.g [...'e', 'e'...]
So I did this
>>> i, j = -1,0
>>> for items in range(len(alphabets)):
if alphabets[i+1] == alphabets[j+1]:
alphabets.remove(alphabets[j])
However it didn't work. How can I remove repetitons?
Here's a much easier way of removing co-occurrences:
import itertools
L = ['a', 'b', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'o', 'o', 'o', 'p', 'q', 'r', 'r', 't', 'u', 'u', 'v', 'w', 'x', 'y', 'z']
answer = []
for k,_group in itertools.groupby(L):
answer.append(k)
Or simpler still:
answer = [k for k,_g in itertools.groupby(L)]
Both yield this:
In [42]: print(answer)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Python alphabet shifting string

i have tried but can't seem to find my mistake in my code.
My code is suppose to switch all the alphabetic characters (like a/aa/A/AA) and do nothing with the rest but when i run the code it doesn't give an error yet do what i want.
Could anyone tell me what i have done wrong or have forgotten?
letter = input("type something")
shift = int(input("type how many shifts"))
if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
a = ord(letter) + shift
b = chr(a)
print(b)
else:
print(letter)
EDIT: thanks for the == replacement for in! Does someone know why using more than one character in letter gives the same print?(Desired output: when i put in abc and 1 i want it to print bcd)
I suppose you want to shift the letters so if the input letter is 'a' and shift is 3, then the output should be 'd'.
In that case replace
if letter == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
with
if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
Or better yet as Tempux suggested you can use
if letter.isalpha()
If you want to shift multple letters you need to loop across each character. Try the following code for multiple letters
letter = input("type something")
shift = int(input("type how many shifts"))
s = ""
for l in letter:
if l.isalpha():
a = ord(l) + shift
s += chr(a)
else:
s += l
print(s)
You compare letter with list, but i think you want to check for contain letter in list, so you should just replace == to in
From the looks of it, I'd say you're more after something like this:
import string
text = input("type something> ")
shift = int(input("enter number of shifts> "))
for letter in text:
index = ord(letter) - ord('a') + shift
print(string.ascii_letters[index % len(string.ascii_letters)])

Categories