如何将数字列表转换为字符列表?大家好你们好,
我有一个包含数字的列表,我想将它们转换为字符。 有没有一种方法可以不使用循环? 我找不到合适的功能。
谢谢你的帮助!
脚本编写回复:如何将数字列表转换为字符列表?这是一个小脚本,用于创建可以执行所需功能的函数。您可以在代码中定义和使用该函数,也可以在开放代码中使用4条语句进行转换。
Names Default To Here( 1 );
theList = {7,8,9};
NumListToChar = Function( {myList},
{defaultlocal},
dtx = New Table( "x", New Column( "y", values( myList ) ), private );
dtx:y << data type( character );
myList = dtx:y << get values;
Close( dtx, nosave );
Return( myList );
);
theList = NumListToChar( theList );回复:如何将数字列表转换为字符列表?感谢您的回答@txnelson确实是我要找的东西。
我只是想知道在循环和涉及表创建的函数之间最快的方法是什么。 任何想法?
回复:如何将数字列表转换为字符列表?JMP 16中的新功能是对于Each()函数它遍历容器(列表,矩阵或关联数组)以对每个元素执行操作。
这是一个示例,说明如何使用它将数字列表转换为字符串列表。
Names Default To Here( 1 );
theList = {7, 8, 9};
For Each( {value, index}, theList, theList[index] = Char( value ) );
回复:如何将数字列表转换为字符列表?谢谢@杰夫·珀金森!
我只是出于好奇而进行了一些模拟,以比较每个解决方案的执行时间,而且似乎每个函数平均来说是最快的!
因此,尽管@txnelson也很好用。
感谢你们俩:)
回复:如何将数字列表转换为字符列表?这是针对这三种方法的快速计时程序,分别用于循环,每种方法以及使用数据表的自定义功能。
Names Default To Here( 1 );
listsize=1000000;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
NumListToChar = Function( {myList},
{defaultlocal},
dtx = New Table( "x", New Column( "y", values( myList ) ), private );
dtx:y << data type( character );
myList = dtx:y << get values;
Close( dtx, nosave );
Return( myList );
);
start = HP Time();
For Each( {value, index}, theList, theList[index] = Char( value ) );
foreachloop = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
theList = NumListToChar( theList );
customfunction = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
For( i = 1, i <= N Items( theList ), i++,
theList[i] = Char( theList[i] )
);
forloop = HP Time() - start;
show(foreachloop, customfunction, forloop);
对于我来说,如上所示的listsize,三种方法的结果非常相似:
foreachloop = 1106974;
customfunction = 1077862;
forloop = 1400643;
回复:如何将数字列表转换为字符列表?扩展杰夫的答案
不同的间接费用对小尺寸不利,对大尺寸有利。
dt = New Table( "Untitled 3",
Add Rows( 0 ),
New Column( "size", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "for", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "foreach", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "custom", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) )
);
NumListToChar = Function( {myList},
{defaultlocal},
dtx = New Table( "x", New Column( "y", values( myList ) ), private );
dtx:y << data type( character );
myList = dtx:y << get values;
Close( dtx, nosave );
Return( myList );
);
For( listsize = 1, listsize < 1e7, listsize *= 2,
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
For Each( {value, index}, theList, theList[index] = Char( value ) );
foreachloop = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
theList = NumListToChar( theList );
customfunction = HP Time() - start;
theList = As List( J( listsize, 1, Random Integer( 100 ) ) );
start = HP Time();
For( i = 1, i <= N Items( theList ), i++,
theList[i] = Char( theList[i] )
);
forloop = HP Time() - start;
Show( foreachloop, customfunction, forloop );
dt << addrows( 1 );
dt:size[N Rows( dt )] = listsize;
dt:for[N Rows( dt )] = forloop / 1e6;
dt:foreach[N Rows( dt )] = foreachloop / 1e6;
dt:custom[N Rows( dt )] = customfunction / 1e6;
Wait( 0.1 ); // so it shows on display
);回复:如何将数字列表转换为字符列表?感谢您的附加信息@杰夫·珀金森和@Craige_Hales !
我们总是在社区中学到很多东西!!
回复:如何将数字列表转换为字符列表?很好地说明了不同功能的大小相关性能。 我仍在使用JMP 15,无法使用对于每个()但是,为了进一步扩展Jeff的计时练习,我将介绍另外两个具有相同结果的函数。
Repeat(k = 0; {k++; Char( theList[k] )}, listsize);
Words( Substr( Char( theList ), 2, Length( Char( thelist ) ) - 2 ), ", " );

For循环(对于小型列表而言最快)被大小大于50的Word,大小大于1000的data-table-tourtour函数和大小大于30000的Repeat所超越。
令我惊讶的是,当字数大于50时,Words仍然是最快的,大约是数百万个“ words”的两倍。
回复:如何将数字列表转换为字符列表?哇,我在寻找一种解决方案,我有很多! JMP太神奇了! 谢谢大家!
回复:如何将数字列表转换为字符列表?@DonMcCormack --挑战!
回复:如何将数字列表转换为字符列表?谢谢@Craige_Hales ! 我喜欢。 让我看看是否可以将其应用于下一个挑战。 我们应该尝试避免任何迭代还是只是避免为了(及其衍生产品)?避免迭代的一种方法是通过表达式求值。 如果您不熟悉这种方法,请查看@joseph_morgan的发现教程,第1点。 对于上述问题,该函数可能看起来像这样:
convertNumberToCharacter = Function({inList},{i=1,outList={}},
outList[1::N Items(inList)] = Expr(Char(inList[i++]));
Eval List(outList);
);
/* You can check it out using the code below.
tStart = HP Time();
alist = As List(J(10000,1,Random Normal(1e6,1e5)));
newList = convertNumberToCharacter(alist);
Print((HP Time()-tStart)/1e6);
*/
我已经在一些“挑战”中对此进行了尝试,虽然简洁,但往往会花费更长的时间,而且更难理解(除非您对表达式处理相当熟悉)。
回复:如何将数字列表转换为字符列表?还有“转换每个”,似乎比这里的“ For Each”快一点。
theList = Transform Each( {value}, theList, Char( value ) );
回复:如何将数字列表转换为字符列表?@DonMcCormack取决于问题的大小,香草for-loop最快和最慢。 我是真的惊讶的@小姐基于单词的解决方案在大尺寸情况下表现出色。 我真的很喜欢意想不到的答案,这些答案展示了这样的JMP功能。 我感到有些惊讶,因为在某种规模下,每种方法都比其他方法要快。 我也喜欢知道这一点。
我不会尝试避免任何事情。 我喜欢您当前的讨论计划,而不是明确的获胜者。 以下是一些讨论入门思路:
- 如果您有N个要转换的列表,并且它们都有M个元素,那么N和M如何影响您将编写的代码? 不仅要考虑速度,还要考虑正确性和易于维护性。
- 哪种想法最适合内存受限的机器?
- 您将如何显示“ NaN”而不是“。”?
- 通用例程是什么样的? 如果它内置在JMP中,您会怎么称呼它?