I am trying to separate items in a list by character, and this is done but whenever i run the code it separates the items but shows them separated multiple times. How can I fix this?
I've already tried using range in a for function, but that hasn't worked. The only thing that gives an output is using
for character in x
My code:
def rle():
askq = int(input("How many lines of RLE compressed data do you want to enter?"))
if askq < 2:
print("You must enter at least 2 lines of RLE compressed data.")
rle()
print("Please enter your RLE compressed data one line at a time")
lines = []
for i in range (0, askq):
i = input("Which lines would you like to convert?")
lines.append(i)
num=0
lines_input = [1,num]
lines2 = []
x = []
for i in range(0,askq):
num+=1
if num in lines_input:
x.append(lines[i])
for x in lines:
for character in x:
lines2.append(character)
print(lines2)
rle()
I expect the output of
lines2
to be
["0","1","d","6","1"," ","0","1","b"]
but instead i get
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
Try this update
def rle():
askq = int(input("How many lines of RLE compressed data do you want to enter?"))
if askq < 2:
print("You must enter at least 2 lines of RLE compressed data.")
rle()
print("Please enter your RLE compressed data one line at a time")
lines = []
for i in range (0, askq):
i = input("Which lines would you like to convert?")
lines.append(i)
new_list = []
for i in lines:
new_list.extend(list(i))
print(new_list)
rle()
Related
I have a big table with the size of 5,905,635*30 (see figure 1), and a list with the size 5,905,635 of rows (see figure 2). I want to reassign the cells' values in the table, given the elements of the list (see figure 3).
figure 1
figure 2
figure 3
For example, like the codes below, I want to get df2 given df1 and list1; an easy way is to loop the elements of list1, the first element is 'B', so assign the first row of column B to 1 in df1, and the second element is 'C', then assign the second row of column C to 1, and etc. The final result should be df2. The problem with this solution is too slow if I have a big size of table. I wonder if there is a fast way to achieve this goal.
df1 = pd.DataFrame({'A': ['0', '0','0', '0', '0', '0', '0'],
'B': ['0', '0','0', '0', '0', '0', '0'],
'C': ['0', '0','0', '0', '0', '0', '0'],
'D': ['0', '0','0', '0', '0', '0', '0'],
'E': ['0', '0','0', '0', '0', '0', '0']})
list1 = ['B','C','A','E','D','A','D']
df2 = pd.DataFrame({'A': ['0', '0','1', '0', '0', '1', '0'],
'B': ['1', '0','0', '0', '0', '0', '0'],
'C': ['0', '1','0', '0', '0', '0', '0'],
'D': ['0', '0','0', '0', '1', '0', '1'],
'E': ['0', '0','0', '1', '0', '0', '0']})
The problem of this solusion is too slow if I have a big size of the table. I wonder if there is a fast way to achieve this goal.
While swapping in python using rindex for this specific case why swapping is not happening? Case-1 is giving correct ans, but not case-2.
Case-1:
S=['1', '1', '1', '2', '1', '1']
S[S.index('1')], S[''.join(S).rindex('2')] = S[''.join(S).rindex('2')], S[S.index('1')]
print(S)
Output: ['2', '1', '1', '1', '1', '1']
Case-2:
S=['2', '1', '1', '1', '1', '1']
S[S.index('1')], S[''.join(S).rindex('2')] = S[''.join(S).rindex('2')], S[S.index('1')]
print(S)
Output: ['2', '1', '1', '1', '1', '1']
But Expected Output: ['1', '2', '1', '1', '1', '1']
Calculate your indices before doing the swap, not in the middle of it:
index1, index2 = S.index('1'), ''.join(S).rindex('2')
S[index1], S[index2] = S[index2], S[index1]
Suppose I have a list of lists of varying lengths.
list1 = [['0', '0'],['0', '1', '0', ' '],['0', '1', ' ', '0', '1', ' '],['1', '1', ' ', ' ', '1', '1', ' ', '0'],[]]
I want to compare this list against another list of lists:
list2=[['0','0','0','0'],['1','1','1','1']]
Now, I want to check if list2 is contained in list1. The output should be True since list1 has ['1', '1', ' ', ' ', '1', '1', ' ', '0'] which contains ['1','1','1','1']
What would be the best way to check if any of the list2 elements are present in list1? The answer should be a Boolean in case any of the list2 elements are found in list1 by maintaining order.
I have tried quite a few things, but can't seem to get the desired output. Thanks in advance for the suggestions.
This will do the trick
list1 = [['0', '0'], ['0', '1', '0', ' '], ['0', '1', ' ', '0', '1', ' '], ['1', '1', ' ', ' ', '1', '1', ' ', '0'], []]
list2 = [['0', '0', '0', '0'], ['1', '1', '1', '1']]
for i in list2:
string2 = ''.join(i)
for j in list1:
string1 = ''.join(j).replace(' ','')
if string2 in string1:
print('yes',i,j)
# output yes ['1', '1', '1', '1'] ['1', '1', ' ', ' ', '1', '1', ' ', '0']
Simplistic approach:
list1 = [['0', '0'],['0', '1', '0', ' '],['0', '1', ' ', '0', '1', ' '],
['1', '1', ' ', ' ', '1', '1', ' ', '0'],[]]
list2 = [['0','0','0','0'],['1','1','1','1']]
# computing the shortened strings once, storing as string,orignal - tuples
texts = [(''.join(inner).replace(" ",""),inner) for inner in list1]
find = [(''.join(inner).replace(" ",""),inner) for inner in list2]
for (shortened,inner) in texts:
for (pattern,f) in find:
if pattern in shortened:
print("Found", f , "in", inner)
to get
Found ['1', '1', '1', '1'] in ['1', '1', ' ', ' ', '1', '1', ' ', '0']
It is simplistic because it is a quadratic search.
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding=[]
rawrow=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
nofinding.append(rawrow)
print(nofinding[:2])
I am reading datarows from a csv file and want to create a customized nested list based on certain columns. I want that
list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
shall return a list like
['00030805_000.png,No Finding,1,34777,69373']
which is stored in rawrow and then append to a bigger list i.e. nofinding but i am getting output as
[['0', '0', '0', '3', '0', '8', '0', '5', '', '0', '0', '0', '.',
'p', 'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g',
',', '1', ',', '3', '4', '7', '7', '7', ',', '6', '9', '3', '7', '3'],
['0', '0', '0', '3', '0', '8', '0', '4', '', '0', '0', '0', '.', 'p',
'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g', ',',
'1', ',', '3', '5', '4', '0', '5', ',', '6', '3', '0', '8', '8']]
Desired output
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No
Finding,1,35405,63088'] ]
Thank you
Your issue is that rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18])) is turning the string in to a list of characters
if you want to leave this as a comma delimited string replace that line with the following:
rawrow = row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]
or more cleanly:
rawrow = ",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]])
I am curious though why you want:
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No Finding,1,35405,63088'] ]
Instead of this:
[ ['00030805_000.png','No Finding',1,34777,69373], ['00030804_000.png','No Finding',1,35405,63088] ]
which you could achieve with the following:
rawrow = []
for row_index in [0, 1, 2, 17, 18]:
rawrow.append(row[row_index].split(","))
or in one line:
rawrow = [row[row_index].split(",") for row_index in [0, 1, 2, 17, 18]]
Furthermore, your whole code could be consolidated as follows:
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding = [",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]]) for row in datarows if row[1]=='No Finding' and row[2]=='1']
print(nofinding[:2])
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
rawrow = []
nofindings=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = [''.join(row[row_index]) for row_index in [0, 1, 2, 17, 18] ]
nofindings.append(rawrow)
print(nofindings[:3])
Solved my issues.
I am trying to convert a list of strings to a list of letters/numbers but keeping the length of list the same. Here is my list look like,
a = ["0587828028", "2967480535"]
My code to convert the above list to split the string and save in a new list.
new_a = []
for i in range(len(a)):
new_a += a[i]
And the output is on list,
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8', '2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Desired output should be 2 list:
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'] ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Any suggestion is much appreciated, I am very new in python.
Just use the built-in list() iterable expansion:
a = ["0587828028", "2967480535"]
new_a = [list(x) for x in a]
# [['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'],
# ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']]