有效地连接大列表元素(Concatenate Big List Elements Efficiently)
我想列出一个元素列表,其中每个元素以4个数字开头,每个元素以4个字母结尾。 这是我的代码
import itertools def char_range(c1, c2): """Generates the characters from `c1` to `c2`""" for c in range(ord(c1), ord(c2)+1): yield chr(c) chars =list() nums =list() for combination in itertools.product(char_range('a','b'),repeat=4): chars.append(''.join(map(str, combination))) for combination in itertools.product(range(10),repeat=4): nums.append(''.join(map(str, combination))) c = [str(x)+y for x,y in itertools.product(nums,chars)] for dd in c: print(dd)
这样运行正常但是当我使用更大范围的字符(例如(az))时,程序会占用CPU和内存,并且PC变得没有响应。 那么我怎么能以更有效的方式做到这一点呢?
I want to make a list of elements where each element starts with 4 numbers and ends with 4 letters with every possible combination. This is my code
import itertools def char_range(c1, c2): """Generates the characters from `c1` to `c2`""" for c in range(ord(c1), ord(c2)+1): yield chr(c) chars =list() nums =list() for combination in itertools.product(char_range('a','b'),repeat=4): chars.append(''.join(map(str, combination))) for combination in itertools.product(range(10),repeat=4): nums.append(''.join(map(str, combination))) c = [str(x)+y for x,y in itertools.product(nums,chars)] for dd in c: print(dd)
This runs fine but when I use a bigger range of characters, such as (a-z) the program hogs the CPU and memory, and the PC becomes unresponsive. So how can I do this in a more efficient way?
原文:https://stackoverflow.com/questions/47971684
最满意答案
尝试这个。
int userScore1 = peepsScores.getInt("userScore1",0);
当您使用getInt时,如果您的值未在共享首选项中设置,则必须返回整数值。
Try this.
int userScore1 = peepsScores.getInt("userScore1",0);
When you use getInt then you have to return integer value if your value is not set in shared preference.
相关问答
更多-
除非你在同一个应用程序中需要不同的首选项(不太可能),我建议你使用 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); (当然, this需要是一个Context ,例如Activity或您的Application ) Unless you need different sets of preferences in the same application (unlikely) I suggest y ...
-
理论上remove比put(null)更好,因为它删除了键和值(一旦提交),而不是将键映射(并保持)为空值。 但从Android 5.1.1实现来看,它们是等价的: ... String k = e.getKey(); Object v = e.getValue(); // "this" is the magic value for a removal mutation. In addition, // setting a value to "null" for a ...
-
getInt()SharedPreferences android错误[关闭](getInt() SharedPreferences android error [closed])[2023-06-10]
尝试这个。 int userScore1 = peepsScores.getInt("userScore1",0); 当您使用getInt时,如果您的值未在共享首选项中设置,则必须返回整数值。 Try this. int userScore1 = peepsScores.getInt("userScore1",0); When you use getInt then you have to return integer value if your value is not set in shared p ... -
你正在设置两次主场比分 txthomeScore.setText(Home); <---- txtvisitorScore.setText(Visitor); txthomeScore.setText(Inning); <---- 用。。。来代替 txthomeScore.setText(Home); txtvisitorScore.setText(Visitor); txtinningNumber.setText(Inning); 由于您是Android的新手,作为一般标准使用camelCase作 ...
-
我刚刚进行了重置(擦除缓存并擦除数据,并从引导加载程序恢复出厂设置),现在它又重新运行了 我认为内存已损坏而且失败了。 I just made a Reset ( Wipe cache and wipe data, and factory reset from bootloader) and now It is working again I think that the memory got corrupted and It was failing.
-
从您的例外情况来看,您似乎确实为字符串首选项设置了此名称,而不是此首选项不存在: 05-12 16:45:50.489: ERROR/AndroidRuntime(1767): Caused by: java.lang.ClassCastException: java.lang.String 05-12 16:45:50.489: ERROR/AndroidRuntime(1767): at android.app.ContextImpl$SharedPreferencesImpl.getInt(C ...
-
从这里改变 txtNumber.setText(infor.getInt("number", 032)); 至 txtNumber.setText(infor.getInt("number", 032)+""); 要么 int my = infor.getInt("number", 032); txtNumber.setText(my+""); 这会给你一个字符串。 如果你想使用String类将int转换为String,那么你可以使用这种方式。 int my = infor.getInt("numbe ...
-
您应该禁用库的proguard: -keep class com.loopj.android.** { *; } -keep interface com.loopj.android.** { *; } 或者你只能保持你遇到问题的课程。 (PersistentCookieStore) You should disable proguard for the library: -keep class com.loopj.android.** { *; } -keep interface com.loopj.an ...
-
getSharedPreferences方法是Context对象的一个方法,所以只需从Fragment调用getSharedPreferences就不会工作,因为它不是Context! (Activity是Context的扩展,所以我们可以从它调用getSharedPreferences)。 所以你必须得到你的应用程序上下文 // this = your fragment SharedPreferences preferences = this.getActivity().getSharedPreferen ...
-
到目前为止,如果数据仅限于某些值,是的,您可以使用SharedPreferences。 您可以在SharedPreference中轻松更新/更改/清除值。有关共享首选项的使用,请参阅这些 Android共享首选项示例 http://developer.android.com/guide/topics/data/data-storage.html#pref http://www.tutorialspoint.com/android/android_shared_preferences.htm 但是,如果您的数 ...