Split array by rows - python

I have a very basic problem.I have wrote a code which open a .txt file which contain a numbers 1 2 3 4 5 6 7 8 9.Then it square all of it and write to other file.
Right now I want to add to this code procedure which split all of this numbers in rows and rewrite,like this:
1 4 9
16 25 36
49 64 81
My code already:
n=[]
dane = open("num.txt", "r")
for i in dane:
i = i.replace('\n','')
for j in i.split(' '):
j = int(j)
j = j**2
n.append(j)
nowy = open("newnum.txt","w")
nowy.write(str(n))
nowy.close()

The code you have written works fine expect for the writing part. For which you need to change the last three lines of code as
nowy = open("newnum.txt","w")
for i in range(0,len(n),3):
nowy.write("{} {} {}\n".format(n[i],n[i+1],n[i+2]))
nowy.close()
The for loop can be explained as,
loop through the list n that you have generated 3 at a time by using the third argument to the range function which is called step.
write out the values three at a time into the file, terminated by the newline character
The output after changing the lines of code is as expected
1 4 9
16 25 36
49 64 81
Ref:
format
range

As a complement to #Bhargav's answer, according to the doc "[a] possible idiom for clustering a data series into n-length groups [is] using zip(*[iter(s)]*n)"
You can use the star to unpack a list/tuple as arguments to format function call too.
All this will lead to a more Pythonic (or, rather crypto-Pythonic ?) version of the writing part:
with open("newnum.txt","w") as nowy:
for sublist in zip(*[iter(n)]*3):
nowy.write("{} {} {}\n".format(*sublist))
Please note the use of a context manager (with statement) to ensure proper closing of the file in all cases when exiting from the block. As other changes would be subject to discussion, that later is a must -- and you should definitively take the habit of using it
(BTW, have you noticed you never closed the dane file? A simple mistake that would have been avoided by the use of a context manager to manage that resource...)

You can try this:
strNew = ''
dane = open("num.txt", "r")
row = 0
for i in dane:
i = i.replace('\n','')
for j in i.split(' '):
row += 1
j = int(j)
j = j**2
if (row % 3) == 0:
strNew += str(j)+'\n'
else:
strNew += str(j) + ' ' # it can be ' ' or '\t'
nowy = open("newnum.txt","w")
nowy.write(strNew)
nowy.close()
The result is:
1 4 9
16 25 36
49 64 81

n=[]
dane = open("num.txt", "r")
for i in dane:
i = i.replace('\n','')
for j in i.split(' '):
j = int(j)
j = j**2
# new code added
# why str(j)? Because array in Python can only have one type of element such as string, int, etc. as opposed to tuple that can have multiple type in itself. I appended string because I wanna append \n at the end of each step(in outer loop I mean)
n.append(str(j))
# new code added
n.append("\n")
nowy = open("newnum.txt","w")
nowy.write(str(n))
nowy.close()

Related

How to use Enumerate with Variable data properly?

I am trying to use enumerate with data in a variable but the variable data is getting enumerated
as a single string how can i use in the below format
Excepted output comes when i use with statement :
with open("sample.txt") as file:
for num, line in enumerate(file):
print(num, line)
output
0 sdasd
1 adad
2 adadf
but when
data = "adklkahdjsa saljdahsd \nsjdksd"
for num, line in enumerate(data):
print(num, line)
output
0 a
1 d
2 k
3 l
4 k
5 a
6 h
7 d
8 j
9 s
10 a
11
12 s ... so on
enumerate expects an iterable. In your example it takes the string as iterable an iterates over each character.
It seems what you want is to iterate over each word in the text. Then you first need to split the string into words.
Example:
data.split(' ') # split by whitespace
Full Example:
data = "adklkahdjsa saljdahsd \nsjdksd"
for num, line in enumerate(data.split(' ')):
print(num, line)

Splitting a string in python at the colon character

This is my code to split a string list at the colon:
this is more info to maybe help with the question
my_file = open("Accounts.txt", "r")
rawAccounts = my_file.read()
Accounts = []
b = 0
j = 0
x = 0
size = 0
dummy= "c"
lessrawAccounts = rawAccounts.split("\n")
while x != 100000:
size = len(lessrawAccounts[j])
if lessrawAccounts[j[b]] != ":":
Accounts[j[b]] = lessrawAccounts[j[b]]
b = b + 1
else:
j = j + 1
while b <= size:
Accounts[j[b]] = lessrawAccounts[j[b]]
b = b + 1
If you want to store only emails from your list on the basis of semicolons you can use this...
lessrawAccounts = ['JohnDoe#gmail.com:userpass']
Accounts = []
passwords = []
for line in lessrawAccounts:
Accounts.append(line.split(":")[0])
passwords.append(line.split(":")[1])
print(Accounts,passwords)
it would be clearer if you gave examples of the strings you wanted to split.
To answer your "question", the reader needs to parse you code to try to work out what you want to do.
Your question is titled more or less "how to split a string at the : character".
before,_,after = "before:after".partition(":")
the partition function splits a string according to a partition string (it can be more than one character). It returns three values, I have discarded the middle value, since the middle value is the partitioning string.

Python program which prints out line number and length of list: Error

Now I'm using while loops to try and do this because I'm not too good at using for loops. As the title reads, I'm trying to print out a table which has the line number next to the length of each line.
Error: When I hit run all I get is the above print out (line and number of words with dashes below). I do not get a series of printouts of y and z
Note: I'm probably making this way harder than it needs to be
Code:
list1 = ['Lets go outside','pizza time','show me the money']
list2 = []
print('line number of words')
print('---- ---------------')
x = 0
len_l1 = len(list1)
while len_l1 > 0:
split_lis1 = list1[0+x].split(' ')
list2.append(split_lis1)
len_l1 -= 1
x += 1
while len_l1 > 0:
q = 1
y = len(list1) - len(list1) + q(x)
z = len(list2[0+x])
print(y, z)
len_l1 -= 1
x += 1
what I want the print out to look like:
line number of words
---- ---------------
0 3
1 2
2 4
Thanks.
Yes, you might have overcomplicated the solution as there are out of the box Python methods that help you easily solve problems like this. For iteration with indexes, use enumerate, in the example below we set the index to start at 1. We can also use some simple string formatting defined in fmt to ensure consistent spacings.
li = ['Lets go outside','pizza time','show me the money']
print('line number of words')
print('---- ---------------')
fmt = ('{} {}')
for idx, sentence in enumerate(li,1):
no_of_words = len(sentence.split())
print(fmt.format(idx, no_of_words))
Then simple use split to split the whitespaces and get the total number of words and let enumerate manage the whole thing for you.
>>
line number of words
---- ---------------
1 3
2 2
3 4
list1 = ['Lets go outside','pizza time','show me the money']
print('line number of words')
print('---- ---------------')
for i in range(0, len(list1)):
length = len(list1[i].split(" "))
print(i + 1, " ", length)
Check out python docs for range and for details.

Nested Loops to create a pattern

How could I use Nested Loops to create the following pattern?
111111
11111
1111
111
11
1
So far i have this and i seem to be stuck.
def main():
stars = "******"
for x in range (1,7):
print(stars)
for y in range (1,1):
stars = stars.replace("*"," ")
main()
You need to replace just 1 star in the inner loop:
stars = "******"
for x in range(6):
stars = stars.replace("*","1")
print(stars)
for y in range(1): # need range(1) to loop exactly once
stars = stars.replace("1","",1)
Output:
111111
11111
1111
111
11
1
If you actually want stars:
stars = "******"
for x in range(6):
print(stars)
for y in range(1):
stars = stars.replace("*","",1)
Output:
******
*****
****
***
**
*
The last arg to str.replace is count where only the first count occurrences are replaced. So each time we only replace a single character.
If you have to uses the stars variable and replace then the code above will work, if you just need nested loops and to create the pattern, you can loop down from 5 and use end="" printing once in the inner loop:
for x in range(5, -1, -1):
print("1" * x, end="")
for y in range(1):
print("1")
Again the same output:
111111
11111
1111
111
11
1
def main(symbol, number):
for x in reversed(range(number)):
s = ""
for y in range(x+1):
s += symbol
print s
main("1", 6)
You can give arguments one symbol (Example - '1','*') and number (Example - '5 for *****' to start with)
You are replacing all the stars in the string with the replace method, meaning you would online print one line of start. You could use the substring method for a better result.
Check answer of Padraic Cunningham with Nested Loop.
Without Nested Loop:
Create Count list by range method and reverse method of list.
Iterate list and print 1 multiple of count.
code:
counters = range(1, 7)
counters.reverse()
for i in counters:
print "1"*i
Output:
111111
11111
1111
111
11
1
Since you're requesting a nested loop, I guess this is just a training exercise and it is not important how efficient it is.
Therefore let me propose a solution with a 'proper' inner loop and without reversed ranges:
def triangle(x, c='1'):
for i in range(x):
line = ''
for _ in range(i, x):
line += c
print(line)
You can get the same output using a simple approach, as Python supports * operator on Strings which returns a string with repeated occurrences.
character = "1" #You can change it to "*"
for i in range(6, 0, -1):
print character*i
Output:
111111
11111
1111
111
11
1

Python - replay values in list

Please help for task with the list in Python my logic is bad works:( .
This is full text of task: Write a program that takes a list of
numbers on one line and displays the values in a single row, are
repeated in it more than once.
To solve the problem can be useful sort method list.
The procedure for withdrawal of repetitive elements may be arbitrary.
My beginning code is :
st = (int(i) for i in input().split())
ls = []
for k in st:
if k == k + 1 and k > 1:
Task is : if we have replay value in list we must print it. We only can use sort() method and without any modules importing.
Results Examples:
Sample Input 1:
4 8 0 3 4 2 0 3
Sample Output 1:
0 3 4
Sample Input 2:
10
Sample Output 2:
Sample Input 3:
1 1 2 2 3 3
Sample Output 3:
1 2 3
This code isn't run( sort() function doesn't want sort my_list. But I must input values like my_list = (int(k) for k in input().split())
st = list(int(k) for k in input())
st.sort()
for i in range(0,len(st)-1):
if st[i] == st[i+1]:
print(str(st[i]), end=" ")
my_list = (int(k) for k in input().split())
After running this line, my_list is a generator, something that will create a sequence - but hasn't yet done so. You can't sort a generator. You either need to use []:
my_list = [int(k) for k in input().split()]
my_list.sort()
which makes my_list into a list from the start, instead of a generator, or:
my_list = list(int(k) for k in input().split()))
my_list.sort()
gather up the results from the generator using list() and then store it in my_list.
Edit: for single digits all together, e.g. 48304, try [int(k) for k in input()]. You can't usefully do this with split().
Edit: for printing the results too many times: make the top of the loop look backwards a number, like this, so if it gets to the second or third number of a repeating number, it skips over and continues on around the loop and doesn't print anything.
for i in range(0,len(st)-1):
if st[i] == st[i-1]:
continue
if st[i] == st[i+1]:
print...
st = (int(i) for i in input().split())
used = []
ls = []
for k in st:
if k in used: # If the number has shown up before:
if k not in used: ls.append(k) # Add the number to the repeats list if it isn't already there
else:
used.append(k) # Add the number to our used list
print ' '.join(ls)
In summary, this method uses two lists at once. One keeps track of numbers that have already shown up, and one keeps track of second-timers. At the end the program prints out the second-timers.
I'd probably make a set to keep track of what you've seen, and start appending to a list to keep track of the repeats.
lst = [num for num in input("prompt ").split()]
s = set()
repeats = []
for num in lst:
if num in s and num not in repeats:
repeats.append(num)
s.add(num)
print ' '.join(map(str,repeats))
Note that if you don't need to maintain order in your output, this is faster:
lst = [num for num in input("prompt ").split()]
s = set()
repeats = set()
for num in lst:
if num in s:
repeats.add(num)
s.add(num)
print ' '.join(map(str, repeats))
Although if you can use imports, there's a couple cool ways to do it.
# Canonically...
from collections import Counter
' '.join([num for num,count in Counter(input().split()).items() if count>1])
# or...
from itertools import groupby
' '.join([num for num,group in groupby(sorted(input().split())) if len(list(group))>1])
# or even...
from itertools import tee
lst = sorted(input('prompt ').split())
cur, nxt = tee(lst)
next(nxt) # consumes the first element, putting it one ahead.
' '.join({cur for (cur,nxt) in zip(cur,nxt) if cur==nxt})
this gives the answers you're looking for, not sure if it's exactly the intended algorithm:
st = (int(i) for i in input().split())
st = [i for i in st]
st.sort()
previous = None
for current in st:
if ((previous is None and current <= 1)
or (previous is not None and current == previous + 1)):
print(current, end=' ')
previous = current
>>> "4 8 0 3 4 2 0 3"
0 3 4
>>> "10"
>>> "1 1 2 2 3 3"
1 2 3
updated to:
start with st = (int(i) for i in input().split())
use only sort method, no other functions or methods... except print (Python3 syntax)
does that fit the rules?

Categories

Resources