I have 2-D list where each item of the list contain and id and an error with it. i have to concat all the errors if they comes in consecutive lines.
inputList=[["12","hello"],["12","hello"],["12","hello"],["13","hi"],["13","hi"],["12","hello"]]
outputList=["hellohellohello","hihi","hello"]
Related
I am trying to separate a single number (or multiple) from a list of randomly generated list without any repeating values. This random list can be, for example, a range of 7 values from 1-45, then take one or more of those values from the list and use the same range but cannot be the same as the values inside the main list.
import random
rndList = []
rndList = random.sample(range(1,45), 7)
rndList.sort()
print("Some numbers...", rndList[0:6])
print("Separate number...", rndList[6])
This will output a random and sorted list, e.g. [5,12,28,35,39,41] and the last separate value from the list, e.g. [43]. The problem is that the last number will always be greater than the previous whereas I need it to be between 1-45 but never use repeated values from the list.
Is it possible to select the separate value randomly from the list instead of creating another list? Appreciate the help!
The easiest thing would simply be to make your selection and remove it from the list before you sort the list.
rndList = random.sample(range(1,45), 7)
separate = rndList.pop()
rndList.sort()
print("Some numbers...", rndList)
print("Separate number...", separate)
I am having two lists with repeating values and I wanted to take the intersection of the repeating values along with the values that have occurred only once in any one of the lists.
I am just a beginner and would love to hear simple suggestions!
Method 1
l1=[1,2,3,4]
l2=[1,2,5]
intersection=[value for value in l1 if value in l2]
for x in l1+l2:
if x not in intersection:
intersection.append(x)
print(intersection)
Method 2
print(list(set(l1+l2)))
i have a list containing some server names like ['ora04900', 'ora04901', 'ora04898', 'ora04903'].
How can i sort these server names based on the last 3 digits in each element of the list?
You can use the .sort method to sort your list and as the key you indicate through a lambda that you want to use only the last three digit (x[-3:]):
servers = ['ora04900', 'ora04901', 'ora04898', 'ora04903']
servers.sort(key=lambda x: x[-3:])
Here is the source code I have. xs is a list of time float numbers, I am trying to put all of the elements of the list to the same row.
outFile=open('testing.csv','w',newline='')
writeFile=csv.writer(outFile)
writeFile.writerow(['cell'])
writeFile.writerow(['LevelLine for Impedance Magnitude:',baseline])
writeFile.writerow(['TimeMag (second):',xs])'
First I had tried using the for loop to load the numbers such as
for i in range(len(xs)-1):
writeFile.writerow('TimeMag(second):',xs[i])
However, the output result of this code prints xs[i] in different rows. I had tried watching the video and checked the CSV writerow function, but I couldn't find anything that put a list of elements to the same row.
I want my output like this:
output:
row 1: Cell
row 2: TimeMag(second): xs[0] xs[1] xs[2] and so on.
Please help, thank you!
You're close. The object you pass to writerow() should be a flat iterable containing the items you want to write to the row. Your attempt:
writeFile.writerow(['TimeMag (second):',xs])
...isn't flat, because it contains two elements, one of which (xs) is itself a list. Instead, you basically want to prepend your string ('TimeMag (second):') to the xs list. This should do what you're expecting:
writeFile.writerow(['TimeMag (second):'] + xs)
Please, have a look at this constriction:
M = [[1,2,3],
[4,5,6],
[7,8,9]]
T = [row[1] for row in M]
print(T)
The result is [2, 5, 8]
I managed to find something here:
http://docs.python.org/py3k/tutorial/datastructures.html#nested-list-comprehensions
But I'm not satisfied with my understanding of this scheme with 'raw'.
Could you tell me where else in the documentation can I read about it?
By the way, why raw? It seems to be a column?
T = [row[1] for row in M]
This is a list comprehension. List comprehensions basically allow you to create lists on the fly while iterating through other iterables (in this case M).
The code above is more or less identical to this:
T = [] # create empty list that holds the result
for row in M: # iterate through all 'rows' in M
cell = row[1] # get the second cell of the current row
T.append(cell) # append the cell to the list
This is all just put together into a single line and a bit more efficient, but the basic idea is the same.
M is a matrix, but the internal representation you chose is a list of lists; or a list of rows. And in T you want to select a single column of the matrix although you have no direct access to columns in the matrix T. So you basically go through each row, take the cell of the column you are interested in and create a new list with the cells of your columns (as lists are usually horizontally aligned, you are strictly getting the transposed vector of your column).
You iterate through the rows and take second element of the row. Then you collect the extracted elements from the rows. It means that you have extracted the column.
Read the list comprehension from the right to the left. It says:
Loop through the matrix M to get the row each time (for row in M).
Apply the expression to the row to get what you need (here row[1]).
Iterate through the constructed results and build the list of them ([...]).
The last point makes it the list comprehension. The thing between the [ and ] is called a generator expression. You can also try:
column = list(row[1] for row in M)
And you get exactly the same. That is because the list() construct a list from any iterable. And the generator expression is such iterable thing. You can also try:
my_set = set(row[1] for row in M)
to get the set of the elements that form the column. The syntactically brief form is:
my_set = {row[1] for row in M}
and it is called set comprehension. And there can be also a dictionary comprehension like this:
d = { row[1]: True for row in M }
Here rather artificially, the row[1] is used as the key, the True is used as the value.