Add some space after each character string in the output - text

I want to write headers in a file, but when I do the following:
write(30,*) 'apple', 'ball', 'cat'
write(30,*) 'a', 'b', 'a', 'b', 'a', 'b'
I get output header as:
appleballcat
ababab
but I need:
apple ball cat
a b a b a b
How to do this?

You should use ' a ' to get appace after the a. Don't do it with b or else you'll get 2 spaces. Do the same for apple and cat. Your using 'apple' but you should use ' apple '.

There are several ways to add one space after each "word", for example...
!! no space may be given between words
write(30,*) 'apple', 'ball', 'cat'
write(30,*) 'a', 'b', 'a', 'b', 'a', 'b'
!! "*" indicates an unlimited repeat count
write(30,'(*(A,1X))') 'apple', 'ball', 'cat'
write(30,'(*(A,1X))') 'a', 'b', 'a', 'b', 'a', 'b'
!! ":" terminates format control if there are no more items in the I/O list
write(30,'(*(A : 1X))') 'apple', 'ball', 'cat'
write(30,'(*(A : 1X))') 'a', 'b', 'a', 'b', 'a', 'b'
!! for old compilers (1000 is some large number)
write(30,'(1000(A,1X))') 'apple', 'ball', 'cat'
write(30,'(1000(A,1X))') 'a', 'b', 'a', 'b', 'a', 'b'
which gives (with gfortran-8.2)
appleballcat
ababab
apple ball cat
a b a b a b
apple ball cat
a b a b a b
apple ball cat
a b a b a b
They may be useful for printing words contained in character arrays etc. More details can be found, for example, in a Format Specifications page.

Related

Set itertools product maximum repeat value per element

I want to generate different combinations of 3 elements a, b, and c. The length of these combinations needs to be 4. I want to have a maximum of 4 times from 'a' and a maximum 1 time from each 'b' and 'c' element. So, for example, we can have ['a',' a',' a','a'] or ['a','a','b','c'] but not ['a','b','b','b'].
There is a similar question in 1, but, as far as I know, using the last 'gen' function, the length of a generation is controlled by the multiplication of a maximum number of repetitions (4 in my case). Also, cases were limited to tuples with exactly 1 'b' and 1 'c' and the rest are 'a'. For the last issue, I replaced 'combinations' with 'combinations_with_replacement', but it still produces tuples with 4 elements and there is no ['a',' a',' a','a'].
How can I tackle this problem?
Here is the code:
from itertools import combinations_with_replacement
def gen(ns, elems=None, C=None, out=None):
if elems is None:
elems = list(range(len(ns)))
else:
assert len(elems) == len(ns)
if out is None:
N = 1
for n in ns:
N *= n
out = [elems[0]]*N
C = range(N)
if len(ns) == 1:
yield out
else:
n = ns[-1]
e = elems[-1]
for c in combinations_with_replacement(C,n):
out_ = out.copy()
for i in c:
out_[i] = e
C_ = [i for i in C if i not in c]
yield from gen(ns[:-1], elems[:-1], C_, out_)
for tmp_list in gen([4,1,1], elems=['a', 'b', 'c'], C=None, out=None):
print(tmp_list)
output:
['c', 'b', 'a', 'a']
['c', 'a', 'b', 'a']
['c', 'a', 'a', 'b']
['b', 'c', 'a', 'a']
['a', 'c', 'b', 'a']
['a', 'c', 'a', 'b']
['b', 'a', 'c', 'a']
['a', 'b', 'c', 'a']
['a', 'a', 'c', 'b']
['b', 'a', 'a', 'c']
['a', 'b', 'a', 'c']
['a', 'a', 'b', 'c']
Please note that since I care about the execution time, I want to generate the tuples in a loop.

python beginner - repeating value of a list for a couple of times

Trying to print the value in y for the number of times that shows in x.
How should I modify the following syntax to achieve the expected output?
x = 5
y = ['a', 'b']
z = []
for num in list(range(x)):
for idx, num1 in enumerate(y):
z.append(num1)
Output based on above:
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
Expected output:
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']
First, you don't need the list() on range, it's already iterable. Also, you're not using the index, so don't bother with enumerate.
The problem is that you are repeating 5 times going through y and appending it z list. You need to do the opposite:
for num1 in y:
for num in range(x):
z.append(num1)
I also suggest to try a development environment that allows you to debug your program. Use it to get step by step into your code and understand exactly what the computer is doing when executing your code.
You can do it in a single pass.
z=[chr for chr in y for i in range(x)]

zip functionality in python behaving weird

>>> l1 = ['A', 'B', 'C']
>>> l2 = ['G', 'F', 'K', 'J']
>>> for (i, j) in zip(l1, l2):
print(i, j)
l1.pop(0)
print(l1)
print(l2)
output is :
'A'
A G
['B', 'C']
['G', 'F', 'K', 'J']
'B'
C F
['C']
['G', 'F', 'K', 'J']
Expected :
'A'
A G # line 2
['B', 'C']
['G', 'F', 'K', 'J']
'B'
C F # line 3
['C']
['G', 'F', 'K', 'J']
l2 still has ['C']. Why ? Also, at line 2 how come A is there, when A is already popped.
If it means, that i took the value already before popping, then why line 3 has C F. It should be B F.
Please explain. I am totally confused here.
I don't see a problem because you are popping A after the zip function is executed. So even if you remove all elements in l1 and l2 in the first iteration, the loop executes the same because the zip() function had already returned the values before first iteration. range(), zip() or whatever function you provide on the for in , it will execute first, then it will iterate, modifying the original values won't change anything, because it is already returned as a separate value.

adding a string to a list is creating a list of characters

I'm trying to make a script that searches for emails inside a list. The problem is when adding the email found to the email list it returns the email string but it is chopped like in .split()
my_list = ['jose', 'ana', 'ana#gmail.com']
email_list = []
for i in my_list:
if '#gmail.com' in i:
print(i)
email_list += i
print(email_list)
the first print() statement returns what I expected 'ana#gmail.com', but when I print the email_list I get it all chopped, output:
ana#gmail.com
['a', 'n', 'a', '#', 'g', 'm', 'a', 'i', 'l', '.', 'c', 'o', 'm']
You can't add to a list like that. You'll want to use email_list.append(i)
Python does this because you can do mathematical operations on list and do fun things, e.g.
l = []
l = 5 * [2]
l
[2, 2, 2, 2, 2]

How to use the difference between two lists to transform the first into the second? Python

I have a large number of identical lists 'old' which I want to transform in the same way into a list 'new'. The way I want to do it, is to make an example of the desired list 'new'. Then I turn the difference between the two lists 'old' and 'new' into a rule, and then use that rule to turn my other lists 'old_2' into 'new_2'.
I cannot figure out how to do the first step and the second step does not give me the expected result. Is there an elegant way to do this?
import numpy
# 0 1 2 3 4 5
old_1 = ['A', 'B', 'C', 'D', 'E', 'F']
new = ['B', 'C', 'D', 'E', 'A']
# 01 Get the difference new - /- old_1 based on index positions of
# the list elements, to get something like this:
order = [1,2,3,4,0]
# 02 Then use this order to transform a second identical list, old_2.
# For this I wanted to use the following:
old_2 = ['A', 'B', 'C', 'D', 'E', 'F']
old_2 = numpy.array(old_2)
order = numpy.array(order)
inds = order.argsort()
print('inds =', inds) # As a check, this gives the wrong order: [4 1 0 2 3]
new_2 = old_2[inds]
# I expected this to result in what I want, which is:
print(new_2)
['C', 'B', 'D', 'E', 'A']
# But what I get in reality is this:
inds = [4 1 0 2 3]
['E' 'B' 'A' 'C' 'D']
Any suggestions to get the desired result?
new_2 = ['B', 'C', 'D', 'E', 'A']
From what I understand, I tried to edit your code. Hopefully it helps.
import numpy as np
def get_order(new, old):
order = []
for element in new:
order.append(old.index(element))
return order
def main():
old_1 = ['A', 'B', 'C', 'D', 'E', 'F']
new = ['B', 'C', 'D', 'E', 'A']
order = get_order(new, old_1)
print(order)
old_2 = ['A', 'B', 'C', 'D', 'E', 'F']
old_2 = np.array(old_2)
order = np.array(order)
#inds = order.argsort()
#print('inds =', inds) # As a check, this gives the wrong order: [4 1 0 2 3]
new_2 = old_2[order]
print(new_2)
if __name__ == '__main__':
main()
Output
[1, 2, 3, 4, 0]
['B' 'C' 'D' 'E' 'A']

Resources