I'm having a slight problem with this function. I'm trying to generate a list of lists. For each item in those lists I need to remove the space to the left and to the right of each item. For some reason it's only working for the first item in each list.
def filereadlistoflists():
master_list=[]
input_file = open('movies.txt','r')
for line in input_file:
add_line = line.strip().split(",")
master_list.append(add_line)
return master_list
Here's the result:
[['Brad Pitt', ' Sleepers', ' Troy', ' Meet Joe Black', ' Oceans Eleven', ' Seven', ' Mr & Mrs Smith'], ['Tom Hanks', ' You have got mail', ' Apollo 13', ' Sleepless in Seattle', ' Catch Me If You Can']
Related
I have a table that contains run numbers. This is then linked to a second table that contains the serial numbers of the panels that go through each run. I was wondering is it possible to design a query that will give for each run number the serial numbers.
I would like it in a table like:
Run Number1, First Serial Number for 1, Second Serial Number for 1, etc..
Run Number2, First Serial Number for 2, Second Serial Number for 2, etc..
I can get in in the form:
Run Number1, First Serial Number for 1
Run Number1, Second Serial Number for 1
Run Number2, First Serial Number for 2
Run Number2, Second Serial Number for 2
Is there a way to set this up?
You can use my DJoin function as this will accept SQL as the source, thus you won't need additional saved queries:
' Returns the joined (concatenated) values from a field of records having the same key.
' The joined values are stored in a collection which speeds up browsing a query or form
' as all joined values will be retrieved once only from the table or query.
' Null values and zero-length strings are ignored.
'
' If no values are found, Null is returned.
'
' The default separator of the joined values is a space.
' Optionally, any other separator can be specified.
'
' Syntax is held close to that of the native domain functions, DLookup, DCount, etc.
'
' Typical usage in a select query using a table (or query) as source:
'
' Select
' KeyField,
' DJoin("[ValueField]", "[Table]", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' The source can also be an SQL Select string:
'
' Select
' KeyField,
' DJoin("[ValueField]", "Select ValueField From SomeTable Order By SomeField", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' To clear the collection (cache), call DJoin with no arguments:
'
' DJoin
'
' Requires:
' CollectValues
'
' 2019-06-24, Cactus Data ApS, Gustav Brock
'
Public Function DJoin( _
Optional ByVal Expression As String, _
Optional ByVal Domain As String, _
Optional ByVal Criteria As String, _
Optional ByVal Delimiter As String = " ") _
As Variant
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotReadKey As Long = 5
' SQL.
Const SqlMask As String = "Select {0} From {1} {2}"
Const SqlLead As String = "Select "
Const SubMask As String = "({0}) As T"
Const FilterMask As String = "Where {0}"
Static Values As New Collection
Dim Records As DAO.Recordset
Dim Sql As String
Dim SqlSub As String
Dim Filter As String
Dim Result As Variant
On Error GoTo Err_DJoin
If Expression = "" Then
' Erase the collection of keys.
Set Values = Nothing
Result = Null
Else
' Get the values.
' This will fail if the current criteria hasn't been added
' leaving Result empty.
Result = Values.Item(Criteria)
'
If IsEmpty(Result) Then
' The current criteria hasn't been added to the collection.
' Build SQL to lookup values.
If InStr(1, LTrim(Domain), SqlLead, vbTextCompare) = 1 Then
' Domain is an SQL expression.
SqlSub = Replace(SubMask, "{0}", Domain)
Else
' Domain is a table or query name.
SqlSub = Domain
End If
If Trim(Criteria) <> "" Then
' Build Where clause.
Filter = Replace(FilterMask, "{0}", Criteria)
End If
' Build final SQL.
Sql = Replace(Replace(Replace(SqlMask, "{0}", Expression), "{1}", SqlSub), "{2}", Filter)
' Look up the values to join.
Set Records = CurrentDb.OpenRecordset(Sql, dbOpenSnapshot)
CollectValues Records, Delimiter, Result
' Add the key and its joined values to the collection.
Values.Add Result, Criteria
End If
End If
' Return the joined values (or Null if none was found).
DJoin = Result
Exit_DJoin:
Exit Function
Err_DJoin:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotReadKey
' Key is not present, thus cannot be read.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_DJoin
End Select
End Function
' To be called from DJoin.
'
' Joins the content of the first field of a recordset to one string
' with a space as delimiter or an optional delimiter, returned by
' reference in parameter Result.
'
' 2019-06-11, Cactus Data ApS, Gustav Brock
'
Private Sub CollectValues( _
ByRef Records As DAO.Recordset, _
ByVal Delimiter As String, _
ByRef Result As Variant)
Dim SubRecords As DAO.Recordset
Dim Value As Variant
If Records.RecordCount > 0 Then
While Not Records.EOF
Value = Records.Fields(0).Value
If Records.Fields(0).IsComplex Then
' Multi-value field (or attachment field).
Set SubRecords = Records.Fields(0).Value
CollectValues SubRecords, Delimiter, Result
ElseIf Nz(Value) = "" Then
' Ignore Null values and zero-length strings.
ElseIf IsEmpty(Result) Then
' First value found.
Result = Value
Else
' Join subsequent values.
Result = Result & Delimiter & Value
End If
Records.MoveNext
Wend
Else
' No records found with the current criteria.
Result = Null
End If
Records.Close
End Sub
Full documentation can be found in my article:
Join (concat) values from one field from a table or query
If you don't have an account, browse to the link: Read the full article.
Code is also on GitHub: VBA.DJoin
I've tried applying some criteria, but the row is still there.
This first empty row shouldn't be there.
Here is the formula, in case you'd like to help me correct it:
=if(I4<>"", { IFERROR(query(Datasets!X3:AU,"select AS, AT, AJ, AR where AS matches date '"&TEXT(DATEVALUE(I4),"yyyy-mm-dd")&"'",0),{"","","",""}); IFERROR(query(Datasets!BC3:BT,"select BC, BD, BS, BQ where BC matches date '"&TEXT(DATEVALUE(I4),"yyyy-mm-dd")&"'",0),{"","","",""}) }, UNIQUE({query(Datasets!X3:AU,"select AS, AT, ' ', SUM(AR) where AS is not null group by AS, AT, ' ' label ' '' ', SUM(AR) ' '",0); query(Datasets!BC3:BT,"select BC, BD, ' ', SUM(BQ) where BC is not null group by BC, BD, ' ' label ' '' ', SUM(BQ) ' '",0) }))
try:
=IF(I4<>"", {IFERROR(QUERY(Datasets!X3:AU,
"select AS,AT,AJ,AR
where AS matches date '"&TEXT(DATEVALUE(I4), "yyyy-mm-dd")&"'", ), {"","","",""});
IFERROR(QUERY(Datasets!BC3:BT,
"select BC,BD,BS,BQ
where BC matches date '"&TEXT(DATEVALUE(I4), "yyyy-mm-dd")&"'", ), {"","","",""})},
UNIQUE({QUERY(Datasets!X3:AU,
"select AS,AT,' ',SUM(AR)
where AS is not null
group by AS,AT,' '
label' ''',SUM(AR)''", );
QUERY(Datasets!BC3:BT,
"select BC,BD,' ',SUM(BQ)
where BC is not null
group by BC,BD,' '
label' ''',SUM(BQ)''", )}))
I'm new to C++. I know a lot of python but I'm extremely new to C++. I was creating an array of chars but I got this error- "Too many Initializers" in VSCode.
Please let me know how to fix it.
Here's the code
1 | class Board {
2 | public:
3 | char pos_list[9];
4 |
5 | void reset_pos() {
6 | pos_list[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
7 | };
8 | };
I am getting this error in line 6.
Please help me :(
EDIT: My initial answer was not correct, please find the modified correct way to do what you are looking for:
You will not be able to use {'','',''} in order to assign empty values to all elements in the array in C++, you can only do that while initializing the array when you declare it. Also, it wouldn't be ideal because it would use hardcoding of ' ' across the entire length of the array. The better way to do this is to loop over the array and then set each element to empty like below:
void reset_pos() {
int len = sizeof(pos_list)/sizeof(pos_list[0]);
for(int i=0; i<len; i++){
pos_list[i] = ' ';
}
};
trafficLightsCount =12
bufferDist = '5 mi. '
intersectionCount = 20
print 'Found', trafficLightCount, light in
the intersectionCount, buffer and, 'buffereDist 'intersections.'
a='something'
b=12
c='Another thing'
print 'Found : ' + '{0},{1},{2}'.format(a,b,c)
You will get the output : Found : something,12,Another thing
I have a list of lists that i want to put in order with the times that a person leaves he's job. I already made a code to give me only the time but i need something to check the times and put them in order.
inFile=removeHeader(file_name) # the information is taken from a txt file and this only gives me the part of the services
#print inFile gives me list of lists like [['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']
for time in inFile:
hours=time[4]
print hours #this gives me only the hours they left work
see https://docs.python.org/2/library/functions.html#sorted
you have to sort the main list by the leave time i.e. the 5th value of the sublists, the key argument of sorted alows you to specify a function that will be called to compute keys the list is sorted based on:
import time
x =[['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']]
sorted(x, key = lambda x: x[4])