首页 \ 问答 \ 有效地连接大列表元素(Concatenate Big List Elements Efficiently)

有效地连接大列表元素(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
更新时间:2023-05-29 10:05

最满意答案

尝试这个。

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 ...
  • 尝试这个。 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 但是,如果您的数 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)