如何将数字列表转换为字符列表?大家好你们好,
我有一个包含数字的列表,我想将它们转换为字符。 有没有办法在不使用循环的情况下做到这一点? 我无法找到正确的功能。
感谢您的帮助!
数据混合和清理回复:如何将数字列表转换为字符列表?@唐麦考马克普通的 for 循环既最快又最慢,具体取决于问题的大小。 我是真的惊讶@多发性硬化症基于单词的解决方案在大尺寸下表现得非常好。 我真的很喜欢那些意想不到的答案,它们展示了 JMP 中的此类功能。 我有点惊讶 for-each 在某些尺寸下比 for 更快。 我也喜欢知道这一点。
我不会试图避免任何事情。 我喜欢你目前的讨论计划,而不是明显的获胜者。 以下是一些讨论的起始想法:
- 如果您有 N 个列表要转换,并且它们都有 M 个元素,那么 N 和 M 对您要编写的代码有何影响? 不仅要考虑速度,还要考虑正确性和易于维护。
- 哪些想法最适合内存受限的机器?
- 你会如何显示“NaN”而不是“.”?
- 通用例程是什么样的? 如果它内置于 JMP 中,您会怎么称呼它?
回复:如何将数字列表转换为字符列表?还有 Transform Each,它似乎比这里的 For Each 快一点。
theList = Transform Each( {value}, theList, Char( value ) );
回复:如何将数字列表转换为字符列表?谢谢@Craige_Hales ! 我喜欢。 让我看看我能否将其投入到下一个挑战中。 我们应该尝试避免任何迭代还是只是为了(及其衍生品)?避免迭代的一种方法是通过表达式求值。 如果您不熟悉这种方法,请查看@约瑟夫_摩根的发现教程,第 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);
*/
我在一些挑战赛中尝试过这一点,虽然简洁,但往往需要更长的时间并且更难理解(除非您非常熟悉表达式处理)。
回复:如何将数字列表转换为字符列表?@唐麦考马克-挑战!
回复:如何将数字列表转换为字符列表?哇,我一直在寻找一种解决方案,但我得到了很多! JMP 太棒了! 感谢大家!
回复:如何将数字列表转换为字符列表?很好地说明了不同功能的尺寸相关性能。 我仍在使用 JMP 15,但无法使用对于每个()还没有,但是为了进一步扩展 Jeff 的计时练习,我又添加了两个给出相同结果的函数。
Repeat(k = 0; {k++; Char( theList[k] )}, listsize);
Words( Substr( Char( theList ), 2, Length( Char( thelist ) ) - 2 ), ", " );

For 循环对于小列表来说是最快的,在大小 > 50 时被 Words 超越,在大小 > 1000 时被 data-table-detour 函数超越,在大小 > 30000 时被 Repeat 超越。
我有点惊讶地发现,Words 在大小 > 50 时仍然是最快的,在数百万“单词”时速度大约是其两倍。
回复:如何将数字列表转换为字符列表?感谢您的附加信息@杰夫·珀金森和@Craige_Hales !
我们总是在社区中学到很多东西!
回复:如何将数字列表转换为字符列表?扩展杰夫的答案
Different overheads hurt for small sizes and help for large sizes.Different overheads hurt for small sizes and help for large sizes.不同的管理费用对于小尺寸会造成影响,而对于大尺寸则有帮助。
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
);回复:如何将数字列表转换为字符列表?下面是 for 循环、foreach 和使用数据表的自定义函数这三种方法的快速计时程序。
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);
对于我来说,使用上面显示的列表大小,三种方法之间的结果非常相似:
foreachloop = 1106974;
customfunction = 1077862;
forloop = 1400643;
回复:如何将数字列表转换为字符列表?谢谢@杰夫·珀金森!
出于好奇,我做了一些模拟来比较每个解决方案的执行时间,似乎每个函数平均是最快的!
所以我会选择那个,尽管功能@tx尼尔森效果也很好。
感谢你们俩 :)
回复:如何将数字列表转换为字符列表?JMP 16 中的新功能是对于 Each() 函数它迭代容器(列表、矩阵或关联数组)以对每个元素执行操作。
下面是一个示例,展示如何使用它将数字列表转换为字符串列表。
Names Default To Here( 1 );
theList = {7, 8, 9};
For Each( {value, index}, theList, theList[index] = Char( value ) );
回复:如何将数字列表转换为字符列表?感谢您的回答@tx尼尔森这确实是我一直在寻找的东西。
我只是想知道循环和涉及表创建的函数之间哪个更快。 任何想法?
回复:如何将数字列表转换为字符列表?这是一个小脚本,它创建一个可以执行您想要的操作的函数。您可以在代码中定义并使用该函数,也可以使用开放代码中的 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 );