list1中每个元素的列表总和,其中all为list2

时间:2015-10-17 07:53:44

标签: python

我想要make脚本从文件中读取行,而不是从每行中获取切片,将来自1行的所有切片与来自2行的所有切片组合,然后将前一步骤中的所有切片与第3行组合。

例如,我们有

Stackoverflow (4)
python (3)
question (3)

我得到第一个带有(数字)字母片的列表。

lst = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']

然后我需要将它与第二个列表结合起来:

lst = ['pyt', 'yth', 'tho', 'hon']

期望的输出:

finallist = ['Stacpyt', 'tackpyt', 'ackopyt', 'ckovpyt', 'kovepyt', 'overpyt', 'verfpyt', 'erflpyt', 'rflopyt', 'flowpyt' 'Stacyth', 'tackyth', 'ackoyth', 'ckovyth', 'koveyth', 'overyth', 'verfyth', 'erflyth', 'rfloyth', 'flowyth', ..... ,  'erflhon', 'rflohon', 'flowhon']

然后是第3个清单:

lst = ['que', 'ues', 'est', 'sti', 'tio', 'ion']

finallist = ['Stacpytque', 'tackpytque', 'ackopytque', 'ckovpytque', 'kovepytque', 'overpytque', 'verfpytque', 'erflpytque', 'rflopytque', .... 'erflhonion', 'rflohonion', 'flowhonion']

我坚持在需要使用综合结果制作finallist的地方。

我正在尝试这样的代码片段,但它错了:

for i in lst:
    for y in finallist:
        finallist.append(i + y)

因此,如果finallist为空 - 它应该在第一次循环迭代中复制lst,如果finallist不为空,它应该将每个元素与lst组合,依此类推。

3 个答案:

答案 0 :(得分:0)

使用ittertools

import itertools
list1 = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']
list2 = ['pyt', 'yth', 'tho', 'hon']
list3 = ['que', 'ues', 'est', 'sti', 'tio', 'ion']

final_list = list(itertools.product(list(itertools.product(list1,list2)),list3))

这将为您提供所有组合,然后您可以加入所有组合来获取您的字符串。

答案 1 :(得分:0)

我使用re.match()来获取文件中的单词和整数值。

然后,我计算所有切片的子词并将它们添加到列表中,然后将其添加到全局列表中。

最后,我计算了你要寻找的所有可能性,感谢itertools.product(),它的行为类似于嵌套的for循环。

然后,.join()获得元组并获得您想要的最终列表。

from itertools import product
from re import match

the_lists = []

with open("filename.txt", "r") as file:
    for line in file:
        m = match(r'(.*) \((\d+)\)', line)
        word = m.group(1)
        num = int(m.group(2))
        the_list = [word[i:i+num] for i in range(len(word) - num + 1)]
        the_lists.append(the_list)

combinaisons = product(*the_lists)

final_list = ["".join(c) for c in combinaisons]

答案 2 :(得分:0)

int th_id, nthreads;
#pragma omp parallel private(th_id) shared(nthreads) num_threads(3)
{
    th_id = omp_get_thread_num();
    #pragma omp critical
    {
        cout << "Hello World from thread " << th_id << '\n';
    }
    #pragma omp barrier

    #pragma omp master
    {
        nthreads = omp_get_num_threads();
        cout << "There are " << nthreads << " threads" << '\n';
    }
}

将所有候选列表附加到public class MainActivity extends Activity { private Button btn; /** * help to toggle between play and pause. */ private boolean playPause; private MediaPlayer mediaPlayer; /** * remain false till media is not completed, inside OnCompletionListener make it true. */ private boolean intialStage = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button1); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); btn.setOnClickListener(pausePlay); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private OnClickListener pausePlay = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // TODO Auto-generated method stub if (!playPause) { btn.setBackgroundResource(R.drawable.button_pause); if (intialStage) new Player() .execute("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3"); else { if (!mediaPlayer.isPlaying()) mediaPlayer.start(); } playPause = true; } else { btn.setBackgroundResource(R.drawable.button_play); if (mediaPlayer.isPlaying()) mediaPlayer.pause(); playPause = false; } } }; /** * preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread. * @author piyush * */ class Player extends AsyncTask<String, Void, Boolean> { private ProgressDialog progress; @Override protected Boolean doInBackground(String... params) { // TODO Auto-generated method stub Boolean prepared; try { mediaPlayer.setDataSource(params[0]); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub intialStage = true; playPause=false; btn.setBackgroundResource(R.drawable.button_play); mediaPlayer.stop(); mediaPlayer.reset(); } }); mediaPlayer.prepare(); prepared = true; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block Log.d("IllegarArgument", e.getMessage()); prepared = false; e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } return prepared; } @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub super.onPostExecute(result); if (progress.isShowing()) { progress.cancel(); } Log.d("Prepared", "//" + result); mediaPlayer.start(); intialStage = false; } public Player() { progress = new ProgressDialog(MainActivity.this); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); this.progress.setMessage("Buffering..."); this.progress.show(); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (mediaPlayer != null) { mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; } } import itertools def combine(lst): result = list(itertools.product(*lst)) result = [''.join(item) for item in result] return result list1 = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow'] list2 = ['pyt', 'yth', 'tho', 'hon'] list3 = ['que', 'ues', 'est', 'sti', 'tio', 'ion'] lst = [list1, list2, list3] # append more list to lst, then pass lst to combination print combine(lst) 函数将生成各种组合,然后将结果作为列表返回。