我知道必须有一种更好的方法,用更少的代码行,您将如何反转“ some_numbers”中的数字?
CREATE PROC dbo.SP_GetLogString (@TABLE VARCHAR(50), @ID INT)
AS
BEGIN
DECLARE @RETURNVALUE NVARCHAR(MAX) = ''
DECLARE @PKey NVARCHAR(50) = (SELECT
column_name AS PRIMARYKEYCOLUMN
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
AND KU.table_name = @TABLE)
DECLARE @WHERECLAUSE NVARCHAR(MAX) = @PKey + ' = ' + CONVERT(NVARCHAR(50), 1000)
DECLARE @COLUMNSWITHTYPE NVARCHAR(MAX) = ''
DECLARE @COLUMNS NVARCHAR(MAX) = ''
SELECT
@COLUMNSWITHTYPE = @COLUMNSWITHTYPE +
CASE
WHEN CL.DATA_TYPE = 'BIT' THEN 'CONVERT(NVARCHAR(max), CASE WHEN ' + CL.COLUMN_NAME + ' = 1 THEN ''True'' ELSE ''False'' END) AS ' + CL.COLUMN_NAME + ''
WHEN CL.DATA_TYPE = 'float' THEN 'CONVERT(NVARCHAR(max), CONVERT(numeric,' + CL.COLUMN_NAME + ')) AS ' + CL.COLUMN_NAME + ''
ELSE 'CONVERT(NVARCHAR(max), ' + CL.COLUMN_NAME + ') AS ' + CL.COLUMN_NAME + ''
END + ', '
FROM INFORMATION_SCHEMA.COLUMNS CL
WHERE TABLE_NAME = @TABLE
AND CL.DATA_TYPE <> 'image'
SELECT
@COLUMNS = @COLUMNS + CL.COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.COLUMNS CL
WHERE TABLE_NAME = @TABLE
AND CL.DATA_TYPE <> 'image'
SELECT
@COLUMNSWITHTYPE = LEFT(@COLUMNSWITHTYPE, LEN(@COLUMNSWITHTYPE) - 1)
SELECT
@COLUMNS = LEFT(@COLUMNS, LEN(@COLUMNS) - 1)
SELECT
@COLUMNSWITHTYPE = 'SELECT ' + @COLUMNSWITHTYPE + ' FROM ' + @TABLE + ' WHERE ' + @WHERECLAUSE
DECLARE @DynamicSQL NVARCHAR(MAX) = ''
SELECT
@DynamicSQL = 'DECLARE @RESULT NVARCHAR(MAX) = ''''
SELECT @RESULT = @RESULT + TAB.COLUMN_NAME + '':'' + U.ColumnValue +'', ''
FROM INFORMATION_SCHEMA.COLUMNS TAB
INNER JOIN
(
SELECT ColumnName, ColumnValue
FROM (' + @COLUMNSWITHTYPE + ')
AS P
UNPIVOT
(
ColumnValue FOR ColumnName IN (' + @COLUMNS + ' )
) as unpvt
)
as U ON U.ColumnName = Tab.COLUMN_NAME
WHERE TAB.TABLE_NAME = ''' + @TABLE + ''' and TAB.DATA_TYPE<>''image'';
SELECT @RESULT = LEFT(@RESULT, LEN(@RESULT) - 1)
SELECT @RESULT' EXEC SP_EXECUTESQL @DynamicSQL ,N'@OUTPUT NVARCHAR(MAX)OUT',@RETURNVALUE
END
GO
答案 0 :(得分:0)
@Tomas Farias是正确的,您可以使用内置的reversed
,如下所示:
print(list(reversed(some_numbers)))
注意:@coldspeed是正确的,它不是一个称为列表的字符串,所以我将其重命名了
编辑:
尝试一下:
print([some_numbers[len(some_numbers)-i-1] for i in range(len(some_numbers))])
答案 1 :(得分:0)
我假设您是在编写反函数或将其作为作业的练习,在这种情况下,使用reversed
不是重点。
使用索引的替代方法:
some_numbers =[1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77]
new_string = []
l = len(some_numbers) - 1
i = 0
while i <= l:
new_string.append(some_numbers[l-i])
i+=1
print("some_numbers string: ",some_numbers)
print("reversed string: ",new_string)
答案 2 :(得分:0)
由于列表大小是固定的,因此使用for
循环在这里倒退也很有意义:
reversed_lst = []
for i in range(len(some_numbers)-1, -1, -1):
reversed_lst.append(some_numbers[i])
或作为列表理解:
reversed_lst = [some_numbers[i] for i in range(len(some_numbers)-1, -1, -1)]
两者都给出:
[77, 66, 55, 44, 33, 22, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1]
注意:更多的pythonic解决方案是使用reversed()
或切片[::-1]
答案 3 :(得分:-1)
只需编译答案:
some_numbers.reverse()
,现在列表已被反转。或者,如果您不想覆盖列表,请致电reversed (list)
。some_numbers [::-1]
。方括号中的数字(称为拼接)告诉Python如何查看列表。第一个数字(因为我们没有在列表的开头添加默认值)告诉Python从哪里开始。第二个数字默认为len (some_numbers)
,告诉Python在哪里停止。第三个数字(-1)告诉Python如何从一个数字移至下一个数字(在我们的示例中为后退1)。some_numbers.sort (reverse = True)
。同样,如果您不想更改列表,而只想要一份副本,请致电sorted (some_numbers, reverse = True)
。